C++ 带有包含和源路径的 CMake - 基本设置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8304190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CMake with include and source paths - basic setup
提问by bitgregor
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
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?
我找到了一些教程,但是当我有 inc 和 src 文件夹时,我无法找到如何设置它?CMakeLists.txt 文件看起来如何?我会在 / 中放一个,在每个项目文件夹中放一个吗?好像我不需要在 inc 和 src 文件夹中有一个?
回答by Good Night Nerd Pride
You need a CMakeLists.txt
for each source subdirectory. Your structure should look something like this:
CMakeLists.txt
每个源子目录都需要一个。您的结构应如下所示:
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
Content of root/CMakeLists.txt
:
内容root/CMakeLists.txt
:
project(MyProject)
add_subdirectory(MainProject)
add_subdirectory(LibProject)
Content of LibProject/CMakeLists.txt
and MainProject/CMakeLists.txt
:
LibProject/CMakeLists.txt
和的内容MainProject/CMakeLists.txt
:
add_subdirectory(src)
Content of LibProject/src/CMakeLists.txt
:
内容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)
Content of MainProject/src/CMakeLists.txt
:
内容MainProject/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
回答by Beginner
You could do it like following.
你可以像下面那样做。
CMakeLists.txt in your MainProject directory:
project(MainProject) add_subdirectory(LibProject/src) add_subdirectory(MainProject/src)
CMakeLists.txt in your LibProject/src directory:
include_directories(${PROJECT_SOURCE_DIR}/LibProject/inc/) add_library(LibProject test.cpp)
CMakeLists.txt in your MainProject/src directory:
include_directories(${PROJECT_SOURCE_DIR}/MainProject/inc/) add_executable(MainProject main.cpp) target_link_libraries(MainProject LibProject)
MainProject 目录中的 CMakeLists.txt:
project(MainProject) add_subdirectory(LibProject/src) add_subdirectory(MainProject/src)
LibProject/src 目录中的 CMakeLists.txt:
include_directories(${PROJECT_SOURCE_DIR}/LibProject/inc/) add_library(LibProject test.cpp)
CMakeLists.txt 在 MainProject/src 目录中:
include_directories(${PROJECT_SOURCE_DIR}/MainProject/inc/) add_executable(MainProject main.cpp) target_link_libraries(MainProject LibProject)
回答by Agustin Barrachina
In my case I wanted to do it with a single CMakeList. And it worked for me. I add my solution in case it serves to anyone.
在我的情况下,我想用一个CMakeList来做到这一点。它对我有用。我添加我的解决方案,以防它为任何人服务。
This is what I did in my case:
这就是我在我的情况下所做的:
My structure:
Project
|CMakeLists.txt
|-src
| |*.cpp
| |*.c
|-include
| |*.hpp
| |*.h
My CMakeLists.txt have to main parts:
我的 CMakeLists.txt 必须是主要部分:
include_directories(
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/src
)
^ Enables .cpp files to add headers in the include folder.
^ 启用 .cpp 文件以在包含文件夹中添加标题。
file(GLOB all_SRCS
"${PROJECT_SOURCE_DIR}/include/*.h"
"${PROJECT_SOURCE_DIR}/include/*.hpp"
"${PROJECT_SOURCE_DIR}/src/*.cpp"
"${PROJECT_SOURCE_DIR}/src/*.c"
)
^ Just add whatever is in those folders.
^ 只需添加这些文件夹中的任何内容。
PS: if you want to see the full CMakeLists.txt go the the project link NEGU93/ForbiddenDesert.
PS:如果你想看到完整的 CMakeLists.txt 去项目链接NEGU93/ForbiddenDesert。