简体   繁体   English

使用 CMake 对“shm_open”的未定义引用

[英]undefined reference to `shm_open' using CMake

I am using CMake under Ubuntu 14.04 to configure my project.我在 Ubuntu 14.04 下使用 CMake 来配置我的项目。 I need to use a 3rd party library (say stuff.so).我需要使用第 3 方库(比如 stuff.so)。 In the CMakeLists.txt, I use TARGET_LINK_LIBRARIES to link the stuff library.在 CMakeLists.txt 中,我使用 TARGET_LINK_LIBRARIES 链接素材库。 However, I got an error:但是,我收到一个错误:

DIR_TO_LIB/stuff.so:-1: error: undefined reference to `shm_open' DIR_TO_LIB/stuff.so:-1: 错误:未定义的对“shm_open”的引用

I tried to put these flag in the CMakeLists.txt but it didn't work:我试图将这些标志放在 CMakeLists.txt 中,但没有奏效:

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lrt")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lrt")

A post ( link ) saying that -lrt should be put as the last argument of g++.一个帖子( 链接)说 -lrt 应该作为 g++ 的最后一个参数。 In my case where CMake is used, how shall I do this?在我使用 CMake 的情况下,我该怎么做?

UPDATE : I added更新:我添加了

SET (CMAKE_VERBOSE_MAKEFILE 1)

and I found that -lrt is not the last (even though I put it at the end of the target link).我发现 -lrt 不是最后一个(即使我把它放在目标链接的末尾)。 Please see this link for compile output.请参阅此链接以获取编译输出。

As you can see from the compile output, there are some linking flags for the opencv.从编译输出中可以看出,opencv 有一些链接标志。 I don't understand how could this happen as I link the OpenCV library first in the TARGET_LINK_LIBRARIES.我不明白这是怎么发生的,因为我首先在 TARGET_LINK_LIBRARIES 中链接了 OpenCV 库。 How does CMake handle these linking order? CMake 如何处理这些链接顺序?

Please also see my CMakeLists.txt .另请参阅我的CMakeLists.txt

Thank you.谢谢你。

You need to add rt in TARGET_LINK_LIBRARIES as a last one, for example:您需要在TARGET_LINK_LIBRARIES中添加rt作为最后一个,例如:

TARGET_LINK_LIBRARIES(my_app ${Boost_LIBRARIES} rt)

You can verify position of rt by enabling verbose build output:您可以通过启用详细构建输出来验证rt的位置:

SET (CMAKE_VERBOSE_MAKEFILE 1)

First, the answer is: Append -lrt to the end of your macro target_link_libraries for your target my_app , ie,首先,答案是:将target_link_libraries附加到目标my_app的宏-lrt的末尾,即

target_link_libraries(my_app ${Boost_LIBRARIES} -lrt)

That achieves the same effect in solving the missing library to be linked against by using eg gcc compiler:这在使用例如 gcc 编译器解决要链接的缺失库时达到相同的效果:

gcc my_app.c -o my_app -lrt

The reason behind, as you may have already figured out, is the missing required ("realtime") library.正如您可能已经发现的那样,背后的原因是缺少必需的(“实时”)库。 For that, you can check by typing in the command为此,您可以通过输入命令进行检查

man shm_open

and then finding the key snippet explaining the reason of adding -lrt , ie, "These functions are provided in glibc 2.2 and later. Programs using these functions must specify the -lrt flag to cc in order to link against the required ("realtime") library."然后找到解释添加-lrt原因的关键片段,即“这些功能在 glibc 2.2 及更高版本中提供。使用这些功能的程序必须将 -lrt 标志指定给 cc 以便链接所需的(“实时” ) 图书馆。”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM