强制cmake将共享库与静态库链接,而不提及特定目标 [英] Force cmake to link shared library with static library without mentioning a specific target

查看:1465
本文介绍了强制cmake将共享库与静态库链接,而不提及特定目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使一个共享库与一个静态版本的librt链接。目前我正在这样做:

I am trying to make a shared library linked with a static version of librt. Currently I am doing this:

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
ADD_LIBRARY(memtrace SHARED memtrace.c)
ADD_LIBRARY(lib_real_time STATIC IMPORTED)
SET_TARGET_PROPERTIES(lib_real_time PROPERTIES IMPORTED_LOCATION /usr/lib/x86_64-linux-gnu/librt.a)
TARGET_LINK_LIBRARIES(memtrace lib_real_time)

但我不想指定这样的路径。由于librt总是在标准路径,我宁愿cmake找到它。像在gcc我只会指定-lrt。
当我尝试这样使用这个cmake文件:

But I do not want to specify the path like this. Since librt is always in standard paths, I'd rather have cmake find it. Like in gcc I would only specify -lrt. When I try to do this using this cmake file:

CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
ADD_LIBRARY(memtrace SHARED memtrace.c)
TARGET_LINK_LIBRARIES(memtrace rt)

如何链接静态版本的librt而不提及其完整路径?

How can I link with the static version of librt without mentioning its full path?

推荐答案

要链接到库的静态版本,只需在其名称中添加.a扩展名:

To link with the static version of the library, just add ".a" extension to it's name:

TARGET_LINK_LIBRARIES(memtrace rt.a)

这篇关于强制cmake将共享库与静态库链接,而不提及特定目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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