CMake包含和源路径 - 基本设置 [英] CMake with include and source paths - basic setup
问题描述
我想设置一个测试项目,看起来像我自己的项目只是为了让事情工作第一,它看起来像这样:
I'm trying to set up a test project looking like my own project just to get things working first and it looks like this:
/MainProject/inc/main.h
/MainProject/src/main.cpp
/LibProject/inc/test.h
/LibProject/src/test.cpp
我发现了一些教程,但我不知道如何设置这个, src文件夹? CMakeLists.txt文件将如何显示?我在/中有一个,每个项目文件夹中有一个?
I've found some tutorials, but I cant find out how to set up this when I have the inc and src folder? How would the CMakeLists.txt files look? Would I have one in /, one in each of the project folders? It seems like I dont need to have one in the inc and src folders?
推荐答案
你需要一个 CMakeLists.txt
为每个源子目录。您的结构如下:
You need a CMakeLists.txt
for each source subdirectory. Your structure then looks like this:
root
|-MainProject
| |-inc
| | '-main.h
| |-src
| | |-main.cpp
| | '-CMakeLists.txt
| '-CMakeLists.txt
|-LibProject
| |-inc
| | '-test.h
| |-src
| | |-test.cpp
| | '-CMakeLists.txt
| '-CMakeLists.txt
'-CMakeLists.txt
内容 root / CMakeLists.txt
:
project(MyProject)
subdirs(MainProject LibProject)
内容 LibProject / CMakeLists.txt
和 MainProject / CMakeLists.txt
:
subdirs(src)
内容LibProject / src / CMakeLists.txt
:
# Notice name prefix of this variable, set by CMake according
# to value given with "project()" in the root CMakeLists.txt.
include_directories(${MyProject_SOURCE_DIR}/LibProject/inc)
add_library(LibProject test.cpp)
主体专案/ src / CMakeLists.txt的内容
:
include_directories(${MyProject_SOURCE_DIR}/MainProject/inc)
# I assume you want to use LibProject as a library in MainProject.
include_directories(${MyProject_SOURCE_DIR}/LibProject/inc)
link_directories(${MyProject_SOURCE_DIR}/LibProject/src)
add_executable(MainProject main.cpp)
target_link_libraries(MainProject LibProject)
然后使用以下配置和构建:
Then configure and build with:
$ cd root
$ mkdir build
$ cd build
$ cmake ..
$ make
这篇关于CMake包含和源路径 - 基本设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!