I got a smal example project where I'm trying to link a dynamic library in to an executeable.
File structure as follows:
-project
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)
--exe
----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)
This is what I do to run it:
mkdir build
cd build
cmake ..
make install
../cmakefun/bin/exe
The Error I get:
dyld: Library not loaded: @rpath/libengine.dylib
Referenced from: *absolute path*/cmakefun/bin/exe
Reason: image not found
Trace/BPT trap: 5
The generated file inside of the build folder however works. As far as I know I need to set the relative RPath if I'm using 'make install' and I've tried this:
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_INSTALL_RPATH "${CMAKE_INSTALL_PREFIX}/lib")
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
Inside of the CMakteLists.txt (exe folder), but with the same results.
I've tried several other ways of setting the RPath, but to no avail.
Any help is greatly appreciated.
Best Regards