0

Here is my project structure:

➜  helloWorld ls                                                                                                                                                                                                           
[18/11/29|11:19AM]
CMakeLists.txt    cmake-build-debug main.cpp          third_parties
➜  helloWorld
➜  helloWorld ls third_parties/say                                                                                                                                                                                         
[18/11/29|11:19AM]
compilesaylib.sh libsaylib.dylib  saylib.cpp       saylib.h
➜  helloWorld

CMakeLists.txt looks like this:

cmake_minimum_required(VERSION 3.12)
cmake_policy(SET CMP0015 NEW)
SET(CMAKE_SYSTEM_NAME Darwin)
project (myproject)

include_directories(${CMAKE_SOURCE_DIR}/third_parties/say)

file(GLOB LIBRARIES "third_parties/say/*.dylib")
message("LIBRARIES = ${LIBRARIES}")

add_executable(myproject main.cpp)
target_link_libraries(myproject ${LIBRARIES})

main.cpp:

#include <iostream>
#include "saylib.h"

int main() {
    say("Hi there!");
    return 0;
}

The error that I am getting is next:

➜  cmake-build-debug ./helloWorld                                                                                                                                                                                          
[18/11/29|10:56AM]
dyld: Library not loaded: libsaylib.dylib
  Referenced from: /Users/oleg/CLionProjects/helloWorld/cmake-build-debug/./helloWorld
  Reason: image not found
[1]    17995 abort      ./helloWorld
➜  cmake-build-debug

When I am using otool on executable I am getting this:

➜  cmake-build-debug otool -L helloWorld                                                                                                                                                                                   
[18/11/29|10:56AM]
helloWorld:
    libsaylib.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.4)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.200.5)
➜  cmake-build-debug

To resolve this issue I need to make one of two things:

  1. set DYLD_LIBRARY_PATH to point to my lib
  2. copy my lib to the executable directory

Are there any other options? Is there a way to set this path via cmake to run executable without this error?

Maybe I can set somehow not just a lib name libsaylib.dylib as a dependency to the executable file but with an absolute path as for /usr/lib/libc++.1.dylib using cmake and $CMAKE_CURRENT_SOURCE_DIR?

Oleg
  • 1,027
  • 1
  • 8
  • 18
  • You can automate the process of copying the shared library to the executable location using cmake see the first answer to this [question](https://stackoverflow.com/questions/10671916/how-to-copy-dll-files-into-the-same-folder-as-the-executable-using-cmake) – Mohit Nov 29 '18 at 12:44

1 Answers1

1

The third option is using the RPATH variables to adjust the locations searched for the library. https://gitlab.kitware.com/cmake/community/wikis/doc/cmake/RPATH-handling

CMAKE RPATH not working - could not find shared object file

fdk1342
  • 3,274
  • 1
  • 16
  • 17