手机版
你好,游客 登录 注册
背景:
阅读新闻

多目录工程的CmakeLists.txt编写(自动添加多目录下的文件)

[日期:2019-01-06] 来源:CSDN   作者:沧海飞帆 [字体: ]

实现类似于vs中工程的CMakeLists.txt的编写。功能为main.cpp调用hello.cpp 的hello()函数,world.cpp的world()函数。使用自动添加多目录下的文件。

1.工程目录如下

这里写图片描述

2.主函数main.cpp

#include <stdio.h>
#include "hello.h"
#include "world.h"
int main()
{
    hello();
    world();
    return 0;
}

3.顶层CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(helloworld)

# Add the source in project root directory
aux_source_directory(. DIRSRCS)
# Add header file include directories
include_directories(./ ./hello ./world)
# Add block directories
add_subdirectory(hello)
add_subdirectory(world)
# Target
add_executable(helloworld ${DIRSRCS})
target_link_libraries(helloworld hello world)

4.hello文件夹

CMakeLists.txt

aux_source_directory(. DIR_HELLO_SRCS)
add_library(hello ${DIR_HELLO_SRCS})12

hello.cpp

#include "hello.h"
void hello()
{
    printf("hello\n");
}

hello.h

#ifndef HELLO_H
#define HELLO_H
#include <stdio.h>
void hello();
#endif12345

5.world文件夹

CMakeLists.txt

aux_source_directory(. DIR_WORLD_SRCS)
add_library(world ${DIR_WORLD_SRCS})12

world.cpp

#include "world.h"
void world()
{
    printf("world\n");
}

world.h

#ifndef WORLD_H
#define WORLD_H
#include <stdio.h>
void world();
#endif

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址https://www.linuxidc.com/Linux/2019-01/156197.htm

linux
相关资讯       CmakeLists.txt编写  CmakeLists.txt 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款