简体   繁体   中英

Cmake: setting rpath

I'm trying to switch from GNU Make to CMake on a C++ project. My plan is to distribute alongside the source code of my library, also the code of two external libraries that are needed.

The project is structured as follows

├── CMakeLists.txt
├── lib
│   ├── lib1
│   └── lib2
├── main.cpp
└── src
    ├── CMakeLists.txt
    ├── subdir1
    ├── subdir2

Now, under lib1, there is another folder called 'lib' containing the libraries lib1 needs: Boost, Eigen, TBB and Sundials. The main problem is then that I need to set the rpath flag to point to the tbb library.

In my Makefile, things goes like this

LIB1 := lib/lib1
LIB2 := lib/lib2
CXX = g++
CFLAGS = \
    -std=c++1y \
    -MMD \
    -I$(LIB1) \
    -I$(LIB1)/lib/eigen_3.3.3/ \
    -I$(LIB1)/lib/boost_1.69.0/  \
    -I$(LIB1)/lib/sundials_4.1.0/include  \
    -I$(LIB1)/lib/tbb_2019_U8/include  \
    -I$(LIB2) \
    -D_REENTRANT

LDLIBS = \
    $(shell pkg-config --libs protobuf) -lpthread  -L$(LIB1)/lib/tbb \
     -ltbb -Wl,-rpath,"$(LIB1)/lib/tbb"

And things work smootlhy

Now, migrating to CMake, I'm having a hard time in specifying the correct rpath flag. My attempt is as follows

cmake_minimum_required(VERSION 3.13.0)
project(myproj)

find_package(PkgConfig REQUIRED)
pkg_check_modules(PROTO REQUIRED protobuf)

set(CMAKE_BUILD_WITH_INSTALL_RPATH ON)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
SET(CMAKE_EXE_LINKER_FLAGS "-Wl,-rpath,${CMAKE_CURRENT_SOURCE_DIR}/lib/lib1/lib/tbb")

add_executable(bayesmix main.cpp)
add_subdirectory(src)

target_include_directories(bayesmix PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/lib1
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/lib1/lib/boost_1.69.0
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/lib1/lib/eigen_3.3.3
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/lib1/lib/tbb_2019_U8/include)


target_link_libraries(bayesmix 
    -L${CMAKE_CURRENT_SOURCE_DIR}/lib/lib1/lib/tbb
    -ltbb 
    -lpthread
    ${PROTO_LIBRARIES})

target_compile_options(myproj PUBLIC -D_REENTRANT -fPIC)

And when I run make all, I get the following liker error

/usr/bin/ld: cannot find -ltbb

I tried looking at the documentation ( https://gitlab.kitware.com/cmake/community/-/wikis/doc/cmake/RPATH-handling ) but I did not find/understand how to solve my problem!

Try to use target_link_options instead of target_link_libraries .

The commands in cmake are rather literal. Try:

target_link_directories(bayesmix PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/lib/lib1/lib/tbb
)
target_link_libraries(bayesmix PRIVATE
    tbb 
    pthread
    ${PROTO_LIBRARIES}
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM