用于多个库和可执行文件的CMakeLists.txt文件 [英] CMakeLists.txt files for multiple libraries and executables
问题描述
我刚刚开始玩CMake。我有类似的:
/ DEV
| - lib1
| CMakeLists.txt
| - lib2
| CMakeLists.txt
| - exe1
| CMakeLists.txt
/ BUILD
| - lib1
| - lib2
| - exe1
/ INSTALL
| - include
| - lib
| - bin
我想:
- 在需要时独立构建每个lib和exe。 (所以我想我必须为每个lib和exe添加一个CMakeLists.txt文件);
- 在构建时,include和lib目录应该引用INSTALL目录; (这是个好主意吗?)
- 在建立时,将依赖项添加到其他库中,如果不是最新的,则重新生成。
我没有任何线索从哪里开始。请帮助...
您不需要单独的CMakeLists.txt来单独构建目标。假设您有一个顶层CMakeLists.txt与:
ADD_LIBRARY(lib1 $ {all_lib1_files})
ADD_LIBRARY $ {all_lib2_files})
ADD_EXECUTABLE(exe1 $ {all_exe1_files})
TARGET_LINK_LIBRARIES(lib2 lib1)#lib2现在取决于lib1
TARGET_LINK_LIBRARIES(exe1 lib2)#exe1现在取决于lib2和lib1
然后你可以通过运行 make lib1来构建lib1
或 msbuild lib1.vcxproj
等。你可以通过为每个目标指定单独的CMakeLists.txt文件来实现相同的功能 - 如果你认为它是值得的话。 p>
如果您的项目使用 FIND_LIBRARY
或 FIND_PACKAGE
那么如果他们不是最新的,他们不会重建。最终,如果你想要过时的依赖关系被自动重建,你需要让CMake知道关于依赖目标的源和规则,即CMakeLists.txt文件需要使用 ADD_LIBRARY
或 ADD_EXECUTABLE
。
您不应该参考INSTALL目录(除了 INSTALL
命令我想象),因为CMake将隐式使用libs / exes构建位置,而不是安装位置时链接目标。
I am just starting playing with CMake. I have something like:
/DEV
|-- lib1
| CMakeLists.txt
|-- lib2
| CMakeLists.txt
|-- exe1
| CMakeLists.txt
/BUILD
|-- lib1
|-- lib2
|-- exe1
/INSTALL
|-- include
|-- lib
|-- bin
I would like to:
- Build each lib and exe independently when needed. (So I suppose I must add a CMakeLists.txt file for each lib and exe);
- When building, include and lib directories should reference INSTALL directory; (is it a good idea?)
- When building, add dependencies to other lib and rebuild them if not up to date.
I dont have any clue where to start. Please help...
You don't need individual CMakeLists.txt to build targets independently. Say you have one top level CMakeLists.txt with:
ADD_LIBRARY(lib1 ${all_lib1_files})
ADD_LIBRARY(lib2 ${all_lib2_files})
ADD_EXECUTABLE(exe1 ${all_exe1_files})
TARGET_LINK_LIBRARIES(lib2 lib1) # lib2 now depends on lib1
TARGET_LINK_LIBRARIES(exe1 lib2) # exe1 now depends on lib2 and lib1
Then you can build just lib1 by running make lib1
or msbuild lib1.vcxproj
, etc. You can achieve the same by having individual CMakeLists.txt files per target - it's up to you if you think it's worth it.
If your project imports these targets using FIND_LIBRARY
or FIND_PACKAGE
, then they won't be rebuilt if they're not up to date. Ultimately, if you want out-of-date dependencies to be automatically rebuilt, you need to let CMake know about the sources and rules for the dependent target, i.e. the CMakeLists.txt file needs to have added the target using ADD_LIBRARY
or ADD_EXECUTABLE
.
You shouldn't then need to reference the INSTALL directory (except in INSTALL
commands I imagine), since CMake will implicitly use libs/exes build locations rather than installed locations when linking targets.
这篇关于用于多个库和可执行文件的CMakeLists.txt文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!