CMAKEL列出了其他子目录之间的依赖关系

CMAKEL列出了其他子目录之间的依赖关系,cmake,Cmake,这是我的项目的层次结构。b的文件依赖于a的文件,c的文件依赖于a和b的文件 我想从test.c生成一个可执行文件。我应该如何编写CMakeLists.txt project -a -- a1.c a1.h a2.c a2.h -b -- b1.c b1.h b2.c b2.h -c -- c1.c c1.h c2.c c2.h -test.c 我会这样构造它: cmake_minimum_required(VERSION 3.3) project(Example) # Add the fil

这是我的项目的层次结构。b的文件依赖于a的文件,c的文件依赖于a和b的文件

我想从test.c生成一个可执行文件。我应该如何编写CMakeLists.txt

project
-a
-- a1.c a1.h a2.c a2.h
-b
-- b1.c b1.h b2.c b2.h
-c
-- c1.c c1.h c2.c c2.h
-test.c

我会这样构造它:

cmake_minimum_required(VERSION 3.3)
project(Example)

# Add the files in /a as a library.  The headers don't strictly need to be listed in this
# command, but if you do, they show up in IDEs like Visual Studio and Xcode.
add_library(alpha a/a1.c a/a1.h a/a2.c a/a2.h)

# Add the folder "a" as an includes search path.  Make it `PUBLIC` so that not only does
# it get exposed to this library, but also to any targets linking to this one.
target_include_directories(alpha PUBLIC a)



# Add the files in /b as a library
add_library(bravo b/b1.c b/b1.h b/b2.c b/b2.h)
target_include_directories(bravo PUBLIC b)

# Now specify that this library depends upon "alpha"
target_link_libraries(bravo alpha)


# Same for /c
add_library(charlie c/c1.c c/c1.h c/c2.c c/c2.h)
target_include_directories(charlie PUBLIC c)

# We only need to depend on "bravo" here since CMake tales care to also link "alpha" as
# it's specified as a dependency of "bravo".  It doesn't do any harm though if you do
# list "alpha" here.
target_link_libraries(charlie bravo)


# Add the test as an executable
add_executable(the_test test.c)

# And link it to "charlie" (which also means "bravo" and "alpha" get linked)
target_link_libraries(the_test charlie)