我正在尝试设置一个看起来像我自己的项目的测试项目,只是为了让事情先行,看起来像这样:
/MainProject/inc/main.h
/MainProject/src/main.cpp
/LibProject/inc/test.h
/LibProject/src/test.cpp
我找到了一些教程,但是当我有inc和src文件夹时,我无法找到如何设置它? CMakeLists.txt文件看起来如何?我会在每个项目文件夹中有一个/,一个吗?好像我不需要在inc和src文件夹中有一个?
答案 0 :(得分:74)
每个源子目录都需要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
root/CMakeLists.txt
的内容:
project(MyProject)
add_subdirectory(MainProject)
add_subdirectory(LibProject)
LibProject/CMakeLists.txt
和MainProject/CMakeLists.txt
的内容:
add_subdirectory(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)
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)
然后使用:
进行配置和构建$ cd root
$ mkdir build
$ cd build
$ cmake ..
$ make
答案 1 :(得分:20)
你可以这样做。
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)
MainProject / src目录中的CMakeLists.txt:
include_directories(${PROJECT_SOURCE_DIR}/MainProject/inc/)
add_executable(MainProject main.cpp)
target_link_libraries(MainProject LibProject)
答案 2 :(得分:6)
就我而言,我想使用单个CMakeList 来实现。它为我工作。我添加了我的解决方案,以防它对任何人有用。
这是我的情况:
My structure:
Project
|CMakeLists.txt
|-src
| |*.cpp
| |*.c
|-include
| |*.hpp
| |*.h
我的CMakeLists.txt必须包含主要部分:
include_directories(
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/src
)
^使.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"
)
^只需添加这些文件夹中的内容即可。
PS:如果要查看完整的CMakeLists.txt,请转到项目链接NEGU93/ForbiddenDesert。