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

2022-01-23 00:00:00 g++ cmake c++ librt

我在 Ubuntu 14.04 下使用 CMake 来配置我的项目.我需要使用第 3 方库(比如 stuff.so).在 CMakeLists.txt 中,我使用 TARGET_LINK_LIBRARIES 链接素材库.但是,我得到了一个错误:

I am using CMake under Ubuntu 14.04 to configure my project. I need to use a 3rd party library (say stuff.so). In the CMakeLists.txt, I use TARGET_LINK_LIBRARIES to link the stuff library. However, I got an error:

DIR_TO_LIB/stuff.so:-1: 错误:未定义对 `shm_open' 的引用

DIR_TO_LIB/stuff.so:-1: error: undefined reference to `shm_open'

我尝试将这些标志放在 CMakeLists.txt 中,但没有成功:

I tried to put these flag in the CMakeLists.txt but it didn't work:

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

一个帖子(链接)说-lrt 应该作为 g++ 的最后一个参数.在我使用 CMake 的情况下,我该怎么做?

A post (link) saying that -lrt should be put as the last argument of g++. In my case where CMake is used, how shall I do this?

更新:我加了

SET (CMAKE_VERBOSE_MAKEFILE 1)

我发现 -lrt 不是最后一个(即使我把它放在目标链接的末尾).有关编译输出,请参阅此链接.

and I found that -lrt is not the last (even though I put it at the end of the target link). Please see this link for compile output.

从编译输出中可以看出,opencv 有一些链接标志.我不明白这是怎么发生的,因为我首先在 TARGET_LINK_LIBRARIES 中链接了 OpenCV 库.CMake 如何处理这些链接顺序?

As you can see from the compile output, there are some linking flags for the opencv. I don't understand how could this happen as I link the OpenCV library first in the TARGET_LINK_LIBRARIES. How does CMake handle these linking order?

另请参阅我的 CMakeLists.txt.

谢谢.

推荐答案

需要在TARGET_LINK_LIBRARIES中添加rt作为最后一个,例如:

You need to add rt in TARGET_LINK_LIBRARIES as a last one, for example:

TARGET_LINK_LIBRARIES(my_app ${Boost_LIBRARIES} rt)

您可以通过启用详细构建输出来验证 rt 的位置:

You can verify position of rt by enabling verbose build output:

SET (CMAKE_VERBOSE_MAKEFILE 1)

相关文章