CMake add -ldl at end of link stage of add_library

删除回忆录丶 提交于 2020-05-26 04:47:06

问题


I wrote/maintain a small unit test library on github https://github.com/acgreek/ExtremeCUnit built using cmake. You can checkout and run some tests via 'cmake test' after checking out. It was working great on Cygwin and Ubuntu (my only home systems). I recent upgrade to ubuntu 13.10 and the library stopped linking with the test binary because the ExtremeUnitC library now needs to be linked with -ldl at the link stage (via add_library) and additionally the -ldl needs to be add to the end of the link line (some change to gcc it seems). In prior version of Ubuntu, the add_library target didn't need the -ldl until the test object was linked with the ExtremeUnitC library

Basically what I need is for the results of make VERBOSE=10, when it get to the following stage

Linking C shared library libExtremeCUnit.so
/usr/bin/cmake -E cmake_link_script CMakeFiles/ExtremeCUnit.dir/link.txt --verbose=10
/usr/bin/gcc  -fPIC -Wall -Wextra -ggdb3 -fPIC  -ldl   -shared -Wl,-    soname,libExtremeCUnit.so -o libExtremeCUnit.so CMakeFiles/ExtremeCUnit.dir/main.c.o CMakeFiles/ExtremeCUnit.dir/runner.c.o CMakeFiles/ExtremeCUnit.dir/util.c.o CMakeFiles/ExtremeCUnit.dir/findtest_names.c.o CMakeFiles/ExtremeCUnit.dir/assert_support.c.o 

I need it to be,

Linking C shared library libExtremeCUnit.so
/usr/bin/cmake -E cmake_link_script CMakeFiles/ExtremeCUnit.dir/link.txt --verbose=10
/usr/bin/gcc  -fPIC -Wall -Wextra -ggdb3 -fPIC  -ldl   -shared -Wl,-    soname,libExtremeCUnit.so -o libExtremeCUnit.so CMakeFiles/ExtremeCUnit.dir/main.c.o CMakeFiles/ExtremeCUnit.dir/runner.c.o CMakeFiles/ExtremeCUnit.dir/util.c.o CMakeFiles/ExtremeCUnit.dir/findtest_names.c.o CMakeFiles/ExtremeCUnit.dir/assert_support.c.o -ldl

how should I edit my CMakeList.txt to that in a clean/portable way?

You can also send me a pull request so you can get the credit of fixing it.


回答1:


I just needed to add

target_link_libraries(ExtremeCUnit dl)



回答2:


if(CMAKE_DL_LIBS)
    target_link_libraries(ExtremeCUnit ${CMAKE_DL_LIBS})
endif()


来源:https://stackoverflow.com/questions/20131138/cmake-add-ldl-at-end-of-link-stage-of-add-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!