使用target_link_libraries以绝对路径作为库路径来扩展平台库名称? [英] Platform library name expansion using target_link_libraries with absolute path as library path?

查看:1291
本文介绍了使用target_link_libraries以绝对路径作为库路径来扩展平台库名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

仅使用库名称在CMake中使用target_link_libraries,例如

Using target_link_libraries in CMake with just the library name, e.g.

target_link_library( myProject SomeLibrary )

会根据平台将SomeLibrary扩展为SomeLibrary.lib,libSomeLibrary.so等。但是,如果指定了完整路径,则不会基于平台扩展库名,例如

will expand SomeLibrary to SomeLibrary.lib, libSomeLibrary.so, etc. depending on platform. However, if a full path is specified then the library name is not expanded based on platform, e.g.

target_link_library( myProject ${myProject_SOURCE_DIR}/libs/SomeLibrary )

如何获取要基于平台扩展的库名?目前,我正在脚本中检测平台并自己调整库名称,感觉有点难看。

How can I get the library name to be expanded based on platform? Currently, I'm detecting platform in the script and adjusting the library names myself which feels a little ugly.

(背景:在此问题建议我在指定库时使用绝对路径,而不要使用链接目录)

(Background: over on this question I'm advised to use absolute paths when specifying libraries rather than using link_directories)

推荐答案

使用 find_library

而不是硬编码在完整路径下,您只应提供库的名称以及可能会在其中找到的位置(可能是可配置的)列表,然后由 find_library 进行其余操作。如果成功,则可以将 find_library 调用的结果直接输入到 target_link_libraries

Instead of hardcoding the full path, you should only give the name of the library and a list of (possibly configurable) locations where it might be found and have find_library do the rest. If successful, the result of the find_library call can be fed right into target_link_libraries.

这篇关于使用target_link_libraries以绝对路径作为库路径来扩展平台库名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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