问题
Our company uses CMake currently to build our executables for Windows. I'm working on making our application work on Mac. So far the application builds fine on the Mac. However, when I try to open the Executable that CMake creates for the Mac, I get the following error in a terminal window:
Last login: Tue Apr 16 14:34:58 on ttys001
Locals-MacBook-Pro:~ auser$ /Users/auser/Documents/Projects/CodeMonkey/bin/CmDeveloperKit ; exit;
dyld: Library not loaded: libAbcSupport.dylib
Referenced from: /Users/auser/Documents/Projects/CodeMonkey/bin/CmDeveloperKit
Reason: image not found
Trace/BPT trap: 5
logout
[Process completed]
I'm thinking that the CMakeLists.txt for the project might not be setup correctly to build the executable for the Mac. I've included it below:
# Includes the common stuff for CodeMonkey
include(CmConfig)
# Set the file description
set(CMDEVELOPERKIT_FILE_DESCRIPTION "CodeMonkey Application")
# Configures this CodeMonkey module
CmModuleConfig(CmDeveloperKit FIND CodeMonkey CodeMonkeyGui)
# Get source files for CodeMonkeyGui
set(PROJECT_SOURCES ${PROJECT_SOURCES} Main.cpp)
# Only add resource files on Windows
if(WIN32)
# Get header files for CodeMonkeyGui
set(PROJECT_HEADERS ${PROJECT_HEADERS} CmIcon.h)
# Get source files for CodeMonkeyGui
set(PROJECT_RESOURCES ${PROJECT_RESOURCES} CmIcon.rc)
endif(WIN32)
# Add additional include directories
include_directories(${CODEMONKEY_INCLUDE_DIR} ${CODEMONKEYGUI_INCLUDE_DIR} ${ABC_INCLUDE_DIR})
# Add additional link directories
link_directories("${ABC_LIBRARY_DIR}")
# Creates the executable
if(WIN32)
add_executable(${PROJECT_NAME} WIN32 ${PROJECT_SOURCES} ${PROJECT_HEADERS} ${PROJECT_RESOURCES})
# Sets entry point to main
set_target_properties(${PROJECT_NAME} PROPERTIES LINK_FLAGS "/ENTRY:\"mainCRTStartup\"")
else()
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES} ${PROJECT_HEADERS} ${PROJECT_RESOURCES})
endif(WIN32)
# Add the d in debug
set_target_properties(${PROJECT_NAME} PROPERTIES DEBUG_POSTFIX d)
# Links to the other required libs
target_link_libraries(${PROJECT_NAME} ${CODEMONKEY_LIBRARY} ${CODEMONKEYGUI_LIBRARY}
${ABC_ARASUPPORT_LIBRARY} ${ABC_ARAGUI_LIBRARY})
# Sets the appropriate dependencies
add_dependencies(${PROJECT_NAME} ${CODEMONKEY_NAME} ${CODEMONKEYGUI_NAME})
# Configure the install procedures
CmModuleInstall()
Could someone please let me know what I'm missing or have wrong in the above file? If this file is not the issue can you point me in the right direction for a fix?
回答1:
You should add a path of the directory containing libAbcSupport.dylib
to the DYLD_LIBRARY_PATH
environment variable before starting the application.
For the reference, here is the dyld(1) man-page of OS X Manual. Extract:
DYLD_LIBRARY_PATH
This is a colon separated list of directories that contain libraries. The dynamic linker searches these directories before it searches the default locations for libraries. It allows you to test new versions of existing libraries.
For each library that a program uses, the dynamic linker looks for it in each directory in
DYLD_LIBRARY_PATH
in turn. If it still can't find the library, it then searchesDYLD_FALLBACK_FRAMEWORK_PATH
andDYLD_FALLBACK_LIBRARY_PATH
in turn.
If you want this to be out-of-the-box, i.e. without needing to set this variable manually, then, for example, you should simply add proper installation process into CMakeLists
. By default, DYLD_LIBRARY_PATH
probably contains some directories, the system ones, and the user ones. Simply check it by:
echo $DYLD_LIBRARY_PATH
and consult the documentation on which directory is preferable (or sort of conventional) to deploy libraries for 3rd party applications on Mac OS X. Then all you'd have to do is to program CMakeLists
so that on running make install
it deploys libAbcSupport.dylib
into such directory.
NOTE: You don't experience this problem on Windows because while Windows searches for the PATH
environment variable to find DLLs too, it also searches in the current directory of the application (which is not the case for both Mac OS X and Linux). In other words, on Windows you most likely deploy AbcSupport.dll
in the same directory as your application, and therefore don't have to worry about this.
NOTE: Linux is similar to Mac OS X in this regard. So if you ever have to port your application to Linux too, don't forget that you'll need LD_LIBRARY_PATH
there. Here is LD.SO(8) man-page of Linux Programmer's Manual, and a relevant extract:
The
LD_LIBRARY_PATH
environment variable contains a colon-separated list of directories that are searched by the dynamic linker when looking for a shared library to load.The directories are searched in the order they are mentioned in.
If not specified, the linker uses the default, which is
/lib:/usr/lib:/usr/local/lib
.
来源:https://stackoverflow.com/questions/16045885/cmake-build-mac-app