CMAKE如何获取目标文件位置 [英] CMAKE How to get Target File Location

查看:863
本文介绍了CMAKE如何获取目标文件位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取另一个项目的静态库的位置。我尝试了

I'm trying to get the location of a static library of another project. I tried

get_property(目标名称目标测试位置),但是CMAKE给出了以下错误

get_property(target_name TARGET Test PROPERTY LOCATION) but CMAKE gives the following error

CMake Error at project.cmake:6 (get_property):
The LOCATION property may not be read from target "A".
Use the target name directly with add_custom_command, or use the generator
expression $<TARGET_FILE>, as appropriate.

我尝试使用错误消息中提到的生成器表达式,但没有成功。

I tried to use the generator expression mentioned in the error message with no success.

MESSAGE($<TARGET_FILE:A>)

仅输出完全相同的字符串,因此似乎完全不对生成器表达式求值:

just outputs the exact same string, so the generator expression doesn't seem to be evaluated at all:

$<TARGET_FILE:A>

我阅读了文档。它在第一行中提到:

I read the Documentation. In the first lines it mentions:


在生成系统生成期间,对生成器表达式进行求值 以生成
每个构建配置特有的信息。

Generator expressions are evaluated during build system generation to produce information specific to each build configuration.

如果我正确理解了这一点,那么在评估消息功能时,生成器表达式为不再评估了吗?那么在这种情况下我应该怎么做?

If I'm understanding this correctly then at the time the message function is evaluated the generator expressions are not evaluated anymore? So what am I supposed to be doing in this case?

我在 GitHub

编辑:

对不起在没有明确解释我的意图的情况下以这种round回的方式提出问题:

I'm sorry to have asked the question in such a roundabout way without a clear explanation of my intentions:

我的目标是让CMake构建一个单个 (!)我的项目的静态库,其他人(不使用CMake)可以使用。我仍然会为项目使用正常的依赖关系解析,但是另一个人(不使用CMake)将不得不手动将多个库链接到他的项目,这有些不便。一个库就可以解决这个问题。

My Aim is to get CMake to build a single (!) static library for my project, which someone else (who doesn't use CMake) can use. I would still use the "normal" resolution of dependencies for my project, but the other person - who doesn't use CMake - would have to manualy link multiple libraries to his project, which is somewhat inconvenient. A single library would solve this.

在尝试让CMake静态链接两个静态库的方式上,我读到了至少在使用Visual时读到的内容(对不起,我没有保存链接) Studio作为编译器,如果我将静态库的完整路径附加到要链接到静态链接器标志的位置上,则可以获得我想要的结果:

On my way of trying to get CMake to staticly link two static libraries, I read somewhere (sorry, I haven't saved a link) that at least when using Visual Studio as a compiler you can get the result I want if I append the full path of the static library to be linked to the static linker flags like that:

set_target_properties(B PROPERTIES STATIC_LIBRARY_FLAGS >>>INSERT_PATH_HERE<<<)

实际上起作用。但是现在我不得不手动将变量的完全限定路径插入到那里

which does in fact work. But now I'd have to manualy insert the fully qualified path to the variable there

set_target_properties(B PROPERTIES STATIC_LIBRARY_FLAGS "/path/to/library.lib")

这似乎不是好方法给我因此,我尝试了生成器表达式,并提出了以下表达式:

which doesn't seem to be a "good" way to do it to me. So I experimented with generator expressions and came up with the following one:

set_target_properties(B PROPERTIES STATIC_LIBRARY_FLAGS $<TARGET_FILE:A>)

这是行不通的,原因是我不完全了解。我猜set_target_properties不支持生成器表达式。尝试使我的项目正常运行时,我尝试了

which doesn't work, for reasons I don't fully understand. I guess set_target_properties just doesn't support generator expressions. While trying to get my project to work I tried

MESSAGE($<TARGET_FILE:A>)

(如本演讲上文所述),并认为如果我能使该语句生效,我就能解决我的真正问题。这就是我问上面发布的问题的原因。我没有意识到这会给试图回答我的人们带来困惑。

(as stated above this explenation) and thought that if I'd get that statement to work I could solve my real problem. So thats what I asked the question posted above. I didn't realise that this would lead to confusion for the people trying to answer me.

推荐答案

如果您想使用另一个库中,您应该使用target_link_libraries函数:

If you want to use a library in another one you should use target_link_libraries function:

target_link_libraries(B A) 

target_link_libraries 的签名会自动传播 A 放入使用 B 的所有非静态库和可执行文件(直接和间接)。

The signature of target_link_libraries automatically propagates A into all non-static libraries and executables which use B (both directly and indirectly).

使用 B 的静态库知道它们对 A ,但是它们中不会包含 A 中的目标文件的副本。依赖项信息存储在库的 INTERFACE_LINK_LIBRARIES LINK_LIBRARIES 目标属性中。

Static libraries which use B are aware of their dependency on A but they aren't going to have a copy of the objects files from A within them. The dependency information is carried in INTERFACE_LINK_LIBRARIES and LINK_LIBRARIES target properties of the libraries.

根据设计,静态库只是对象文件的存档。如果您只想在CMake项目中使用库,则无需手动将来自不同库的目标文件混合在一起。 CMake将指导您的链接器提供需要链接到的库列表以及库的正确顺序(实际上是从依赖关系图生成的)。链接器将依次解析所提供的静态库中包含的目标文件中的所有符号。

By design, a static library is just an archive of object files. There's no need in mixing object files from different libraries together manually if you only want to use the libraries within your CMake project. CMake will instruct your linker with the list of libraries which needs to be linked into and proper order of the libraries (which indeed is produced out of the dependency graph). The linker, in turn, will resolve all symbols from the object files which are carried in the supplied static libraries.

如果仍然需要精确控制混合对象文件,您可能会考虑使用对象库类型

If you still need to have precise control of the blending objects files, you might consider to use OBJECT library type:

# Create plain objects library which essentially is a set of object files
add_library(zipobj OBJECT zip.cpp)
# Archive all objects from zipobj. Recompilation of zip.cpp won't take place.
add_library(zip STATIC $<TARGET_OBJECTS:zipobj>)

add_library(lzmaobj OBJECT lzma.zpp)
add_library(lzma STATIC $<TARGET_OBJECTS:lzmaobj>)

# Create a library which contains object files from both zipobj and lzmaobj
add_library(archive STATIC $<TARGET_OBJECTS:zipobj> $<TARGET_OBJECTS:lzmaobj>)

编辑:
也可以尝试merge_libraries函数:
http://www.mail-archive.com/cmake@cmake.org/msg28670.html

这篇关于CMAKE如何获取目标文件位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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