如何链接从cmake中的add_subdirectory创建的静态库? [英] how to link static library that was created from add_subdirectory in cmake?

查看:837
本文介绍了如何链接从cmake中的add_subdirectory创建的静态库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用cmake构建依赖于第三方库的程序。这个第三方库包含一个CMakeLists.txt文件,因此我要做的是将第三方库的源代码保留在我的项目目录中,并使用add_subdirectory(path / to / lib)进行构建,然后将我的目标链接到第三方库生成的静态库。

I am trying to build a program using cmake that depends on a third party library. This third party library contains a CMakeLists.txt file so what I want to do is keep the source code of the third party library within my project directory, and build it using add_subdirectory(path/to/lib), and then link my target against the static library that the third party library generated.

我的CMakeLists.txt:

my CMakeLists.txt:

cmake_minimum_version(VERSION 3.10)
project(my_project)

add_subdirectory("${CMAKE_SOURCE_DIR}/extern/somelib")

# my-code:
# somelib CMakeLists.txt file has a project name: SOMELIB
# which lets me access the directory where the files are built
# on windows it builds to /Release, on mac and linux it just builds
# to the binary dir
set(SOMELIB_LIBS "${SOMELIB_BINARY_DIR}/Release")
add_executable(my_program my_main.cpp)
target_link_libraries(my_program "${SOMELIB_LIBS}/SOMELIB.lib" "${SOMELIB_LIBS}/SOMELIBmain.lib")

然后,我生成目录部门,并从该目录执行操作:

I then make a build directory and from that directory I do:

cmake -G "Visual Studio 15 2017" ..
cmake --build .

build命令失败,并带有 LINK:致命错误LNK1181:无法打开输入文件'extern / somelib /Release/SOMELIBmain.lib'...

The build command fails with a "LINK : fatal error LNK1181: cannot open input file 'extern/somelib/Release/SOMELIBmain.lib' ..."

我目前的解决方法是注释掉#my-code部分,建立somelib依赖项首先会生成静态库,然后取消注释我的代码并再次构建即可正常工作。

My workaround for now has been to comment out the part that says "#my-code", build the somelib dependency first which generates the static libraries, and then uncomment out my-code and build again which then works correctly.

我如何告诉CMake首先构建子目录,然后链接到它生成的静态库?

推荐答案

简短答案:告诉CMake存在依赖性

Short answer: tell CMake that there is dependency between its targets.

target_link_libraries(my_program PRIVATE SOMELIB SOMELIBmain)

CMake将为您评估SOMELIB的位置,并将 my_program SOMELIB SOMELIBmain [1]

CMake will evaluate SOMELIBs locations for you and link my_program against SOMELIB and SOMELIBmain[1]. Works for both Debug and Release configurations and also for Linux.

您不必担心CMake在哪里构建文件 [2] 和这就是所谓的现代CMake正在试图实现的目标。我将在这里仅作简短描述,但请查看答案底部的链接 [3]

You shouldn't have to worry where CMake places build files[2] and this is what so called "modern CMake" is trying to achieve. I will leave here just brief description but check link on the bottom of answer[3].

无论如何,关键概念是无论何时您创建库/可执行文件(目标),则列出其用法+构建要求以及对其他目标的依赖关系。您可以使用 target_link_libraries(< your_lib>关键字< dependencies>)声明依赖项。这样的行不仅会针对列出的依赖项建立< you_lib> 链接,还将继承其使用要求(通常是public include目录),并命令CMake在< your_lib>

Anyway, the key concept is that whenever you create library/executable (target), you list its usage+build requirements and dependencies to other targets. You declare dependencies using target_link_libraries(<your_lib> KEYWORD <dependencies>). Such line will not only make <you_lib> link against listed dependencies, it will inherit their usage requirements (usually, public include directories) and order CMake to build dependent libraries before <your_lib>.

它的优点是即使 SOMELIB 不遵循现代CMake的想法(没有声明构建/使用要求),您仍然应该可以仅用这一行来完成。

The beauty of it is that even if SOMELIB does not follow modern CMake ideas (does not declare build/usage requirements), you still should be able to do just with this single line.

[1] 我假设CMake目标的名称与输出库名称相同,但这不是强制性的。对于OP的情况,原来静态库目标后缀有 -static ,因此他不得不编写 SOMELIB-static SOMELIBmain-static

[1] I assumed that CMake targets are named same as output library names, but it is not mandatory. For OP's case it turned out that static library targets are suffixed with -static, so he had to write SOMELIB-static SOMELIBmain-static

[2] 不幸的是,使用Windows共享库(DLL)并不是那么容易

[2] Unfortunately, it is not so easy with shared libraries on Windows (DLLs)

[3] 我将从此博客条目开始: https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/

[3] I'd start with this blog entry: https://pabloariasal.github.io/2018/02/19/its-time-to-do-cmake-right/

这篇关于如何链接从cmake中的add_subdirectory创建的静态库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆