问题描述
我们想组织一个 C ++
项目:
project /
lib1 /(第一个库)
CMakeList.txt
src /
lib1.c
foo1.h
build /
测试/(测试)
CMakeList.txt
test1.c
test2.c
lib2 /(第二个库)
CMakeList.txt
src /
CMakeList.txt
os /(操作系统相关代码)
CMakeList.txt
win32 /
xxx.c(win32实现)
linux /
xxx.c(linux实现)
lib2.c
foo2.h
build /
include /(shared / public headers)
lib1 /
lib.h(共享库头包括在应用程序中)
lib2 /
lib.h(共享库头 - - )
请在
lib2
时写下CMakeLists.txt
使用link1
和例如lib2
应该是可移植的(至少Win32,Linux ...)?
>:如果一些
CMakeList.txt
文件不在他们的地方,请假设如此。我可能忘了。解决方案整个哲学是从整个项目的中心CMakeLists.txt开始。在这个级别,所有的目标(libs,可执行文件)将被聚合,所以没有问题从lib1到lib2链接。如果lib2要链接到lib1,那么需要首先构建lib1。
平台特定的源文件应该有条件地设置为某个变量。
(如果你需要在子目录中设置变量并在上面的目录中使用它,你必须使用CACHE FORCE等设置它到缓存中 - 参见set
)
这是你如何做正确的源代码build - CMake打算:
cd project-build
cmake ../project
每个库有单独的构建目录不是很CMake的(如果我可以这样说),并可能需要
一些黑客。project-build /
project /
CMakeLists.txt(整个项目CMakeLists.txt)
[
项目(MyAwesomeProject)
include_directory(include)#allow lib1和lib2包含lib1 / lib.h和lib2 / lib.h
add_subdirectory(lib1)#这将添加目标lib1
add_subdirectory(lib2)#this添加目标lib2
]
lib1 /(第一个库)
CMakeList.txt
[
add_library(lib1 ...)
add_subdirectory测试)
]
src /
lib1.c
foo1.h
test /(tests)
CMakeList.txt
test1.c
test2.c
lib2 /(第二个库)
CMakeList.txt
[
add_subdirectory(src)
]
src /
CMakeList.txt
[
if(WIN32)
set(lib2_os_sources os / win32 / xxx.c)
elsif(LINUX)
set(lib2_os_sources os / linux / xxx.c)
else()
message(FATAL_ERRORUnsupported OS)
endif()
add_library(lib2 SHARED lib2.c $ {lib2_os_sources})
]
os /(操作系统相关代码)
win32 /
xxx.c(win32实现)
linux /
xxx.c $ b lib2.c
foo2.h
include /(shared / public headers)
lib1 /
lib.h(应用程序中包含共享库头文件)
lib2 /
lib.h(shared library header - - )
We would like to organize a
C++
project like this:project/ lib1/ (first library) CMakeList.txt src/ lib1.c foo1.h build/ test/ (tests) CMakeList.txt test1.c test2.c lib2/ (second library) CMakeList.txt src/ CMakeList.txt os/ (OS dependent code) CMakeList.txt win32/ xxx.c (win32 implementation) linux/ xxx.c (linux implementation) lib2.c foo2.h build/ include/ (shared/public headers) lib1/ lib.h (shared library header included from apps) lib2/ lib.h (shared library header -"-)
Please, how to write those
CMakeLists.txt
when evenlib2
should uselink1
and when e.g.lib2
should be portable (at least Win32, Linux...)?Correction: If some
CMakeList.txt
files are not on their places, please assume so. I probably forgot.解决方案The whole philosophy is to start with a central CMakeLists.txt for your whole project. At this level all the targets (libs, executables) are gonna be aggregated so there will be no problem linking from lib1 to lib2 for example. If lib2 is gonna be linking to lib1, lib1 needs to be built first.
Platform specific source files should be set conditionally to some variable.(If you need to set variable in a subdirectory and use it in a directory above, you have to set it to the cache, using CACHE FORCE etc. - see manual for
set
)This is how you do proper out of source build - as CMake intends:
cd project-build cmake ../project
Having separate build directories per library is not very CMake'ish (if I may say so) and would probably requiresome hacks.
project-build/ project/ CMakeLists.txt (whole project CMakeLists.txt) [ project(MyAwesomeProject) include_directories(include) # allow lib1 and lib2 to include lib1/lib.h and lib2/lib.h add_subdirectory(lib1) # this adds target lib1 add_subdirectory(lib2) # this adds target lib2 ] lib1/ (first library) CMakeList.txt [ add_library(lib1...) add_subdirectory(test) ] src/ lib1.c foo1.h test/ (tests) CMakeList.txt test1.c test2.c lib2/ (second library) CMakeList.txt [ add_subdirectory(src) ] src/ CMakeList.txt [ if(WIN32) set(lib2_os_sources os/win32/xxx.c) elsif(LINUX) set(lib2_os_sources os/linux/xxx.c) else() message(FATAL_ERROR "Unsupported OS") endif() add_library(lib2 SHARED lib2.c ${lib2_os_sources}) ] os/ (OS dependent code) win32/ xxx.c (win32 implementation) linux/ xxx.c (linux implementation) lib2.c foo2.h include/ (shared/public headers) lib1/ lib.h (shared library header included from apps) lib2/ lib.h (shared library header -"-)
这篇关于Cmake多库方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!