PROBLEM
I am trying to use irrKlang to play music. The irrKlang files are in my project directory. Building was fine but when I run it it reports dyld: Library not loaded: /usr/local/lib/libikpFLAC.dylib
(A lib that should've been in my project directory) and reason: image not found
.
I don't understand why it looks for dylibs under /usr/local/lib/
and how I can change that in CMake (not using otool and install_name_tool)
CODES
Here is a simple project to make this happen:
main.cpp
:
#include <iostream>
#include "irrKlang.h"
int main() {
std::cout << "Hello, World!" << std::endl;
irrklang::ISoundEngine* engine = irrklang::createIrrKlangDevice();
if (!engine)
std::cout << "Failed to initialize the engine" << std::endl; // error starting up the engine
engine->play2D("irrKlang/media/ophelia.mp3", true); // Path referencing error, but this is not the issue here, as the program didn't even make it to Hello World
return 0;
}
CMakeLists.txt
:
cmake_minimum_required(VERSION 3.17)
project(Program)
set(CMAKE_CXX_STANDARD 17)
message("${PROJECT_SOURCE_DIR}")
file(GLOB IRRKLANG_LIBS "irrKlang/bin/macosx-gcc/*.dylib")
link_directories("${PROJECT_SOURCE_DIR}/irrKlang/bin/macosx-gcc")
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} irrKlang/bin/macosx-gcc)
set(IRRKLANG_INCLUDE irrKlang/include)
file(GLOB IRRKLANG_INCLUDE_FILES "${IRRKLANG_INCLUDE}/*.h")
include_directories(${IRRKLANG_INCLUDE})
add_executable(Program main.cpp "${IRRKLANG_INCLUDE_FILES}")
target_link_libraries(Program ${IRRKLANG_LIBS})
target_link_directories(Program PUBLIC "${PROJECT_SOURCE_DIR}/irrKlang/bin/macosx-gcc")
My project has directory irrKlang
with directory tree:
irrKlang
├── bin
│ ├── dotnet-4-64
│ ├── linux-gcc-64
│ ├── macosx-gcc (with .dylib files in it)
│ └── winx64-visualStudio
├── doc (...)
├── examples (...)
├── examples.net (...)
├── include
├── lib
│ └── Winx64-visualStudio
├── media (with some audio files in it)
└── plugins
└── ikpMP3
└── decoder
Error:
dyld: Library not loaded: /usr/local/lib/libikpFLAC.dylib
Referenced from: /Users/mac/Documents/VSCProjects/untitled/cmake-build-debug/Program
Reason: image not found
OTool
(Libs should have been in my project directory and doesn't have lib
prefix):
/usr/local/lib/libikpFLAC.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/local/lib/libikpMP3.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/local/lib/libirrklang.dylib (compatibility version 1.0.0, current version 1.0.0)
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 904.4.0)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1292.0.0)
This problem is different than this. The solution there was to use otool and install_name_tool, but then I have to use it everytime I build it. I am wondering if there's a way to use CorrectDirectory/foo.dylib
instead of /usr/local/lib/foo.dylib
inside CMake.
I've also tried RPath handling but this does not work, and I need to make it work by not installing but building and running it.
CMakeLists.txt
:
cmake_minimum_required(VERSION 3.17)
project(Program)
set(CMAKE_CXX_STANDARD 17)
# 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)
# the RPATH to be used when installing
set(CMAKE_INSTALL_RPATH "${PROJECT_SOURCE_DIR}/irrKlang/bin/macosx-gcc")
set(CMAKE_MACOSX_RPATH "${PROJECT_SOURCE_DIR}/irrKlang/bin/macosx-gcc")
# don't 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 FALSE)
message("${PROJECT_SOURCE_DIR}")
file(GLOB IRRKLANG_LIBS "irrKlang/bin/macosx-gcc/*.dylib")
message("${IRRKLANG_LIBS}")
link_directories("${PROJECT_SOURCE_DIR}/irrKlang/bin/macosx-gcc")
set(CMAKE_LIBRARY_PATH ${CMAKE_LIBRARY_PATH} irrKlang/bin/macosx-gcc)
set(IRRKLANG_INCLUDE irrKlang/include)
file(GLOB IRRKLANG_INCLUDE_FILES "${IRRKLANG_INCLUDE}/*.h")
include_directories("${IRRKLANG_INCLUDE}")
add_executable(Program main.cpp "${IRRKLANG_INCLUDE_FILES}")
target_link_libraries(Program "${IRRKLANG_LIBS}")
target_link_directories(Program PUBLIC "${PROJECT_SOURCE_DIR}/irrKlang/bin/macosx-gcc")
UPDATE
Right now what I use to solve this problem is by appending the following lines into CMakeLists.txt
from here:
add_custom_command(TARGET Program POST_BUILD
COMMAND install_name_tool -change /usr/local/lib/libikpFLAC.dylib ${IRRKLANG_LIBDIR}/ikpFLAC.dylib cmake-build-debug/Program
COMMAND install_name_tool -change /usr/local/lib/libikpMP3.dylib ${IRRKLANG_LIBDIR}/ikpMP3.dylib cmake-build-debug/Program
COMMAND install_name_tool -change /usr/local/lib/libirrklang.dylib ${IRRKLANG_LIBDIR}/libirrklang.dylib cmake-build-debug/Program
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMENT "Changing lib paths"
)
But still, I wonder if I can systematically solve this without having to repeat them, since there can be a difference between the name of lib referenced in executable and the name of where it actually is (e.g. .../libikpMP3.dylib
to .../ikpMP3.dylib
but .../libirrklang.dylib
stays the same name.