我们想组织一个这样的C++项目:
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 -"-)如果连CMakeLists.txt都应该使用link1,或者lib2应该是可移植的(至少Win32、Linux.),那么如何编写这些link1呢?
:如果一些CMakeList.txt文件不在它们的位置上,请假定是这样的。我可能忘了。
发布于 2011-10-05 16:12:21
整个理念是从一个中央CMakeLists.txt开始为您的整个项目。在这个级别上,所有目标(lib、可执行文件)都将被聚合,因此从lib1链接到lib2没有问题。如果lib2要链接到lib1,首先需要构建lib1。
特定于平台的源文件应该有条件地设置为某些变量。(如果需要在子目录中设置变量,并在上面的目录中使用变量,则必须使用缓存强制等方法将其设置为缓存--参见set手册)
这就是您如何正确地进行源外构建--正如CMake所打算的那样:
cd project-build
cmake ../project每个库都有单独的构建目录并不太像CMake‘’ish(如果我可以这么说的话),而且可能需要一些黑客。
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 -"-)https://stackoverflow.com/questions/7662583
复制相似问题