Skip to content

Instantly share code, notes, and snippets.

@hewumars
Last active November 16, 2020 03:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hewumars/80217537add7839dd2f849dca0ce376d to your computer and use it in GitHub Desktop.
Save hewumars/80217537add7839dd2f849dca0ce376d to your computer and use it in GitHub Desktop.

-L指定的是链接时的库路径,生成的可执行文件在运行时库的路径由LD_LIBRARY_PATH环境变量指定。

-rpath-link:这个也是用于“链接”的时候的,例如你显示指定的需要 libcert.so,但是 libcert.so 本身是依赖 libssl.so库 的,后者你并没有指定,而是 libcert.so 引用到它,这个时候,会先从 -rpath-link 给的路径里找。(注意动态库是运行时加载的哦) target_link_libraries(aa,-Wl,-rpath-link,/usr/lib:/lib)

-rpath: “运行”的时候,去找的目录。运行的时候,要找 .so 文件,会从这个选项里指定的地方去找。对于交叉编译,交叉编译链接器需已经配置 –with-sysroot 选项才能起作用。也就是说,-rpath指定的路径会被记录在生成的可执行程序中,用于运行时查找需要加载的动态库。-rpath-link 则只用于链接时查找

-fPIC 作用于编译阶段,告诉编译器产生与位置无关代码(Position-Independent Code)

readelf -d executable来查看相关的RPATH路径。

nm -Du BINARY 列出使用到但是未定义的symbol

undefined reference to `dlopen'问题:TARGET_LINK_LIBRARIES(so_name dl) -ldl的顺序要放在g++所有so库的后面

Debug程序正常,Release异常,可能由于未初始化变量导致,编译时加"-Weffc++"来调试

#兼容新旧gcc版本,添加宏定义
if(CONAN_LIBCXX STREQUAL "libstdc++11")
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1)
elseif(CONAN_LIBCXX STREQUAL "libstdc++")
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)#gcc版本高于5时需要,才能链接opencv库
endif()
#添加编译选项
add_compile_options(-fPIC)
#目录下所有cpp各自生成可执行文件
file(GLOB srcs "${CMAKE_CURRENT_SOURCE_DIR}/src/va/*.cpp")
foreach(source ${srcs})
get_filename_component(name ${source} NAME_WE)
add_executable(${name} ${source})
target_link_libraries(${name} ${PLUGIN_LIBRARIES} dl pthread)
endforeach(source)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment