Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

c++ - undefined reference to `shm_open' using CMake

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: error: undefined reference to `shm_open'

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")

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?

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). Please see this link for compile output.

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?

Please also see my CMakeLists.txt.

Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

TARGET_LINK_LIBRARIES(my_app ${Boost_LIBRARIES} rt)

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

SET (CMAKE_VERBOSE_MAKEFILE 1)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...