CMake:将库链接到库 [英] CMake: Link a library to library

查看:267
本文介绍了CMake:将库链接到库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对cmake有问题。可以说,我有CMakeLists1,其中有一个子目录CMakeLists2。

I have a problem with cmake. I have, lets say, CMakeLists1 which has a subdirectory where CMakeLists2 is.

在CMakeLists2中,我的目标是一个静态库。我想将其链接到外部库。
我就是这样:

In CMakeLists2 my target is a static library. And I want to link it to external library. I've made it just like that:

link_directories ("path_to_library")
add_library (project2 ${sources})
target_link_libraries (project2 "name_of_external_lib")

然后,我想在我的project1中使用该project2中的类。我就是这样的:

Then, I want to use a class from this project2 in my project1. I've made it that way:

add_executable (project1 ${sources})
target_link_libraries (project1 project2)

但这根本不起作用。首先,project2没有链接到外部库。为了进行检查,我通过vs10项目属性添加了该库,并且大小有所不同。第二件事,project1以某种方式看到外部库(它在该项目的库依赖项中),当然找不到它。

But that doesn't work at all. First of all, project2 didn't link to external library. Just for checking, I've added this library through vs10 project properties, and the sizes were different. And the second thing, somehow project1 sees that external library (it is in library dependencies of this project) and of course can't find it.

问题是什么?

推荐答案

我认为不将project2链接到外部库
而是将两个库都链接到CMake是CMake的默认行为可执行文件。
来自 Mastering CMake一书。

I think it's CMake's default behavior to not link project2 to the external library, but to link both libraries to the executable. From the book "Mastering CMake".


由于静态库未链接到它们所依赖的库,因此
对于CMake跟踪库很重要,因此可以在要创建的可执行文件的
链接行上指定它们。

Since static libraries do not link to the libraries on which they depend, it is important for CMake to keep track of the libraries so they can be specified on the link line of the executable being created.

您可以尝试在CMakeLists2中使用绝对路径:

You could try to use an absolute path in your CMakeLists2:

add_library (project2 ${sources})
target_link_libraries (project2 "path to ext lib"/"name of ext lib")

,也可以添加

link_directories ("path_to_library")

到project1的CMakeLists文件。

to the CMakeLists file of project1.

如果您真的想在Visual Studio中做类似的事情,则可以使用 answer 以在CMake中构建custom_command。
可能看起来像这样(我没有测试过)。

If you really want to do something like in Visual Studio, you could probably use the command given in this answer to build a custom_command in CMake. It probably would look something like this (I didn't test it).

set(EXT_LIB "path_to_library/name_of_external_lib") 
set(BIG_LIB "path_to_big_lib/name_of_big_lib")
add_library (project2 ${sources})
get_property(PROJ2_LOC TARGET project2 PROPERTY LOCATION)

add_custom_command(OUTPUT ${BIG_LIB} 
                   DEPENDS ${EXT_LIB} project2
                   COMMAND "lib.exe /OUT:${BIG_LIB} ${EXT_LIB} ${PROJ2_LOC} )

然后您可以将可执行文件与 $ {BIG_LIB} 链接。

Then you could link your executable with ${BIG_LIB}.

您需要考虑的一些事情:

Some things you have to consider:


  • 也许您必须使用 LOCATION_CONFIG CMake文档,我在此答案中找到了get_property命令。 )

  • link.exe必须在您的路径中

  • 注意 BIG_LIB 变量的范围能够在其他CMakeLists.txt

  • Maybe you have to use LOCATION_CONFIG (CMake docs, I found the get_property command in this answer )
  • link.exe has to be in your path
  • watch the scope of the BIG_LIB variable if you want to use it in an other CMakeLists.txt

这篇关于CMake:将库链接到库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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