Cmake多库场景
我们希望组织一个像这样的 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 -"-)
请问,当 lib2
应该使用 link1 时,如何编写这些
以及什么时候例如 CMakeLists.txt
lib2
应该是可移植的(至少 Win32、Linux...)?
更正:如果某些 CMakeList.txt
文件不在其位置,请假设如此。我可能忘记了。
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 even lib2
should use link1
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
整个理念是从整个项目的中央 CMakeLists.txt 开始。在这个级别,所有目标(库、可执行文件)都将被聚合,因此从 lib1 链接到 lib2 不会有问题。如果 lib2 要链接到 lib1,则需要首先构建 lib1。
平台特定的源文件应有条件地设置为某个变量。
(如果您需要在子目录中设置变量并在上面的目录中使用它,则必须使用 CACHE FORCE 等将其设置到缓存中 - 请参阅
set
手册)这就是您的做法正确的源代码构建 - 正如 CMake 的意图:
每个库都有单独的构建目录并不是很 CMake 风格(如果我可以这么说),并且可能需要
一些技巧。
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:
Having separate build directories per library is not very CMake'ish (if I may say so) and would probably require
some hacks.