这里记录一下cmake 的 rpath问题。
注意这里要用分号。
这里说明一下link_directories跟 BUILD_RPATH的区别。自己再这里以前也困惑了很久。
link_directories是在编译时设置搜索库文件的路径,以便能找到库文件。类似于gcc命令里面的 -L(大写的l)。注意这里多路径采用分号间隔。
link_directories("/home/wzy/cpp-code/demo/shared;/home/wzy/cpp-code/demo")
而BUILD_RPATH 是写进可执行二进制文件中的,可执行二进制文件执行时,会去RPATH中找到库文件然后执行。类似于gcc命令里面的-Wl,-rpath=
SET_TARGET_PROPERTIES(hello_binary
PROPERTIES BUILD_RPATH "/home/wzy/cpp-code/demo/shared;/home/wzy/cpp-code/demo")
注意这里的BUILD_RPATH路径设置时需要使用分号";"不能使用冒号":"
这里我贴出实验用的代码,以便清晰,首先是项目结构。这里可忽略include/shared/Hello.h和src/Hello.cpp
首先是main.cpp
#include "test.h"
#include <iostream>
int main(int argc, char *argv[])
{
std::cout<<" test mymath add : "<<add(3,4)<<std::endl;
return 0;
}
接下来是CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(hello_library)
############################################################
# Create a library
############################################################
#Generate the shared library from the library sources
# add_library(hello_library SHARED
# src/Hello.cpp
# )
# add_library(hello::library ALIAS hello_library)
#compiler with the -I flag eg. -I/directory/path
# target_include_directories(hello_library
# PUBLIC
# ${PROJECT_SOURCE_DIR}/include
# /home/wzy/cpp-code/demo/include
# )
link_directories("/home/wzy/cpp-code/demo/shared;/home/wzy/cpp-code/demo")
# link_directories("/home/wzy/cpp-code/demo")
############################################################
# Create an executable
############################################################
# Add an executable with the above sources
add_executable(hello_binary
src/main.cpp
)
#compiler with the -I flag eg. -I/directory/path
target_include_directories(hello_binary PUBLIC /home/wzy/cpp-code/demo/include)
# set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
# set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# set(CMAKE_SKIP_BUILD_RPATH FALSE)
# set(CMAKE_BUILD_RPATH "/home/wzy/cpp-code/demo/shared/;${CMAKE_BUILD_RPATH}")
# message(STATUS "cmake rpath : " ${CMAKE_BUILD_RPATH})
SET_TARGET_PROPERTIES(hello_binary
PROPERTIES BUILD_WITH_INSTALL_RPATH "/home/wzy/cpp-code/demo/shared;/home/wzy/cpp-code/demo")
SET_TARGET_PROPERTIES(hello_binary
PROPERTIES BUILD_RPATH "/home/wzy/cpp-code/demo/shared;/home/wzy/cpp-code/demo")
#find libmymath.so library
# find_library(MYMATH
# NAMES
# mymath
# "/home/wzy/cpp-code/demo/shared" )
# IF (NOT MYMATH)
# MESSGAGE(FATAL_ERROR "libmymath.so not found")
# ENDIF(NOT MYMATH)
# message(STATUS " mymath path : " ${MYMATH})
# link the new hello_library target with the hello_binary target
# cmake to link the hello_library against the hello_binary
target_link_libraries( hello_binary
PRIVATE
# hello::library
# ${MYMATH}
mymath
)
这里的动态链接库可以查看前文,用的那里的libmymath.so 路径在$HOME/cpp-code/demo下
传送门:一次性弄懂gcc编译、链接、动态链接、静态链接_笑傲江湖的小白的博客-CSDN博客
接下来进行构建,编译,运行
cmake -B build
cmake --build build
./build/hello_binary
这里还可以详细查看到编译的详细过程
mkdir -b build && cd build
cmake ..
#查看详细日志
make VERBOSE=1
这里贴出详细编译日志信息,重点是红色框出来的部分,详细内容就是gcc进行手动编译的过程。
然后看一下二进制文件链接的动态链接库 ,可以看到这里链接到指定位置。
ldd hello_binary
然后可以详细查看一下二进制文件内容:
readelf -d hello_binary
这里就可以详细看到共享库有libmymath.so, library runpath的路径信息。