【问题标题】:Linker error while building from source code从源代码构建时出现链接器错误
【发布时间】:2015-06-12 16:06:23
【问题描述】:

我正在尝试从源代码构建应用程序。我可以使用'cmake .'对其进行配置。但是,当我运行“make”时,它给了我这个:

Linking CXX executable ../../bin/lux64/Release/od_batch_launcher
../../bin/lux64/Release/libBasic.so: undefined reference to `dlopen'
../../bin/lux64/Release/libBasic.so: undefined reference to `dlclose'
../../bin/lux64/Release/libBasic.so: undefined reference to `dlerror'
../../bin/lux64/Release/libBasic.so: undefined reference to `dlsym'
../../bin/lux64/Release/libBasic.so: undefined reference to `pthread_sigmask'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/lux64/Release/od_batch_launcher] Error 1
make[1]: *** [src/Basic/CMakeFiles/od_batch_launcher.dir/all] Error 2
make: *** [all] Error 2

我了解它无法动态链接到 c++ 库。我不太清楚如何对 cmake 进行必要的更改。我在 Linux Mint 17 上运行 gcc 版本:4.9.2。我将不胜感激。谢谢!

【问题讨论】:

    标签: c++ cmake linux-mint


    【解决方案1】:

    尝试将 -DCMAKE_EXE_LINKER_FLAGS=-ldl 传递给 CMake 可执行文件。要更改 CMake 构建脚本,请添加以下内容:

    target_link_libraries(target_name dl)
    

    其中target_name 基本上是没有任何扩展名的可执行文件名(例如.exe)。

    编辑:实际上,我只是重读了你的问题,我把它放在了错误的地方。你真的想要:

    target_link_libraries(Basic dl)
    

    显然,还有与 pthread 相关的错误,因此您还必须添加:

    target_compile_options(Basic PUBLIC -pthread)
    

    这两个都放在包含add_library(Basic)(通常是CMakeLists.txt)的文件中。

    编辑 2: 而不是 target_compile_options,尝试:

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
    

    【讨论】:

    • 谢谢。所以我应该在 CMakeLists.txt 中添加 target_link_libraries(target_name dl) 吗?并且 target_name 将是 od_batch_launcher?抱歉,我对 CMake 不太满意。
    • 太棒了!有进步!!与“dlopen”等相关的错误;消失了。但是我无法添加 target_compile_options。我明白了:CMake Error at src/Basic/CMakeLists.txt:116 (target_compile_options): target_compile_options called with invalid arguments
    • 谢谢!添加 PUBLIC 也允许 cmake 通过。但是,当我运行 make 时,我仍然得到:./../bin/lux64/Release/libBasic.so: undefined reference to `pthread_sigmask'。
    • @user2984140 你能试试用set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")代替target_compile_options吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-17
    • 2018-02-03
    • 2021-11-08
    相关资源
    最近更新 更多