问题描述
我正在尝试使用cmake(在具有GNU make和g ++的Linux上)构建具有两个子目录的项目:MyLib和MyApp. MyLib包含静态库的源; MyApp需要链接到该库.我正在尝试使用以下CMakeLists.txt在Linux上使用生成的生成文件进行构建:
I'm trying to use cmake (on Linux with GNU make and g++) to build a project with two sub-directories: MyLib and MyApp. MyLib contains source for a static library; MyApp needs to link against that library. I'm trying to build on Linux with generated makefiles using the following CMakeLists.txt:
cmake_minimum_required (VERSION 2.6)
project (MyProj)
include_directories (MyLib)
file(GLOB MyLibSrc MyLib/*.cpp)
add_library(MyLibrary STATIC ${MyLibSrc})
file(GLOB MyAppSrc MyApp/*.cpp)
add_executable(MyApplication ${MyAppSrc})
target_link_libraries(MyApplication MyLibrary)
此几乎"有效.它在链接时失败,因为在生成libMyLibrary.a时-它位于根目录中.当我添加时:
This 'almost' works. It fails at link time because while it generates libMyLibrary.a - it is in the root. When I add:
link_directories(${MyProj_BINARY_DIR})
没有区别.
我有几个(相互关联的)问题:
I've got a few (inter-linked) questions:
- 强制cmake将我的库和可执行文件构建到登台目录"中的最佳方法是什么-—说MyStage—保持目标与来源分离?
- 我如何说服cmake将应用程序与库链接?
- 如果我想构建一个调试版和一个发行版,那么扩展我的cmake脚本来做到这一点的最佳方法是什么?确保调试应用程序链接到调试库,发布应用程序链接到发布库?
我是cmake的新手.我已经阅读了可以在网上找到的内容,但发现自己很难使我的库与可执行文件链接.在我看来,这种配置应该很常见.婴儿床的示例非常有帮助,但我还没有找到.
I'm a relative newcomer to cmake. I've read what I can find on the web, but find myself struggling to get my library to link with my executable. This sort of a configuration, to my mind, should be quite common. An example from which to crib would be very helpful, but I've not found one.
推荐答案
-
使用超出源版本".制作一个仅用于构建的目录,然后在其中调用
Use "out of the source build". Make a directory used only for build and while in it, call
cmake <path to the sources, it may be relative>
可以使用
Either use
link_directories(${MyProj_BINARY_DIR}/MyLib)
或在每个子目录中创建CMakeLists.txt-对于较大的项目,要比很小的项目要好.
or make CMakeLists.txt in each subdirectory - that would be better for project larger than very small.
这有点棘手,请在文档中查看CMAKE_BUILD_TYPE(您可以通过它设置和/或如果"设置).您也可以从命令行进行设置:
This is a bit tricky, check out CMAKE_BUILD_TYPE in the docs (you can set it and/or "if" by it). You can also set it from command line:
cmake -DCMAKE_BUILD_TYPE=Debug
这篇关于CMAKE:建立库并链接到它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!