运行环境:
系统:ubuntu14
IDE:Clion
c++ 调用 .so文件时出现如下错误:
/home/user/CLionProjects/untitled/main.cpp:10: undefined reference to `dlopen'
/home/user/CLionProjects/untitled/main.cpp:15: undefined reference to `dlerror'
/home/user/CLionProjects/untitled/main.cpp:23: undefined reference to `dlsym'
/home/user/CLionProjects/untitled/main.cpp:26: undefined reference to `dlerror'
/home/user/CLionProjects/untitled/main.cpp:27: undefined reference to `dlclose'
解决办法:
1. 添加头文件
#include <dlfcn.h>
2.在CmakeLists.txt 文件末尾添加 :target_link_libraries(untitled ${CMAKE_DL_LIBS})
修改后文件如下:
cmake_minimum_required(VERSION 3.14)
project(untitled)
set(CMAKE_CXX_STANDARD 11)
add_executable(untitled main.cpp)
# 添加 target_link_libraries , 要写在 add_executable 后面
target_link_libraries(untitled ${CMAKE_DL_LIBS})
参考:
https://stackoverflow.com/questions/29125402/clion-ide-issue-while-using-dlopen-libdl-co-ldl-flags
https://blog.youkuaiyun.com/qq_22122811/article/details/52738134