我有一个小示例项目,在这里我试图将动态库链接到可执行文件。
文件结构如下:
-项目
cmake_minimum_required (VERSION 3.3.2)
# Project
# --------------
project (cmakefun)
# Subdirectories
# --------------
add_subdirectory(dlibs)
add_subdirectory(exes)
# Target
# --------------
set(CMAKE_INSTALL_PREFIX "${PROJECT_SOURCE_DIR}/cmakefun")
--dlibs
---- engine.h
#pragma once
#include <iostream>
class Engine {
public:
Engine();
void Function();
};
---- engine.cpp
#include "engine.h"
using namespace std;
Engine::Engine(){}
void Engine::Function() {
cout << "Function" << endl;
}
---- CMakeLists.txt
# Project
# --------------
project(engine)
# Headers and Sources
# --------------
set(${PROJECT_NAME}_headers engine.h)
set(${PROJECT_NAME}_sources engine.cpp)
# Create Binaries
# --------------
add_library (${PROJECT_NAME} SHARED ${${PROJECT_NAME}_headers} ${${PROJECT_NAME}_sources})
# Target
# --------------
install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static)
- 可执行程序
---- main.h
#pragma once
#include <iostream>
---- main.cpp
#include "main.h"
#include "../dlibs/engine.h"
int main( int argc, char* args[] ) {
Engine a;
a.Function();
return 0;
}
---- CMakeLists.txt
# Project
# --------------
project(exe)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# Headers and Sources
# --------------
set(${PROJECT_NAME}_headers "")
set(${PROJECT_NAME}_sources main.cpp)
# Create Binaries
# --------------
add_executable (${PROJECT_NAME} ${${PROJECT_NAME}_headers} ${${PROJECT_NAME}_sources})
# Linker
# --------------
target_link_libraries(${PROJECT_NAME} engine)
# Target
# --------------
install(TARGETS ${PROJECT_NAME}
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION lib/static)
这是我要做的:
mkdir build
cd build
cmake ..
make install
../cmakefun/bin/exe
我得到的错误:
dyld: Library not loaded: @rpath/libengine.dylib
Referenced from: *absolute path*/cmakefun/bin/exe
Reason: image not found
Trace/BPT trap: 5
但是,build文件夹内的生成文件起作用。据我所知,如果我正在使用“make install”,我需要设置相对的RPath,并且我已经尝试过:
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
在CMakteLists.txt(exe文件夹)内部,但结果相同。
我尝试了其他几种设置RPath的方法,但均无济于事。
任何帮助是极大的赞赏。
最好的祝福
最佳答案
我只是遇到了同样的问题,最后我可以使用该主题解决问题
CMAKE RPATH with packaging
和维基
https://cmake.org/Wiki/CMake_RPATH_handling
。
在可执行文件CMakeLists.txt中,您需要
# Project
projet(exe)
# set all you waft to set...
# get your sources...
# set RPATH according to the wiki
# use, i.e. don't skip the full RPATH for the build tree
SET(CMAKE_SKIP_BUILD_RPATH FALSE)
# when building, don't use the install RPATH already
# (but later on when installing)
SET(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
SET(CMAKE_INSTALL_RPATH "${my_install_path}")
# add the automatically determined parts of the RPATH
# which point to directories outside the build tree to the install RPATH
SET(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
# Create binaries
add_executable(my_exe ${my_sources})
# add your libraries...
# Install target
install(TARGET my_exe ${my_options})
关于c++ - RPath无法与从CMake生成的Make Install一起使用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36407894/