Macos CMake、OSX、@rpath和框架链接

Macos CMake、OSX、@rpath和框架链接,macos,hyperlink,cmake,frameworks,rpath,Macos,Hyperlink,Cmake,Frameworks,Rpath,我花了一个星期的时间试图让我在Linux上得到的东西在OSX上工作,但是没有用。我试着阅读rpath上的CMake文章和其他各种关于它的SO帖子,并用我的CMakeLists.txt使它工作,但我失败了,我希望有人能在这里给我一些提示,说明我做错了什么 # CMake 2.8.11 and above required for linking to qtmain.lib library on Windows # CMake 2.8.12 and above required for MACOSX

我花了一个星期的时间试图让我在Linux上得到的东西在OSX上工作,但是没有用。我试着阅读rpath上的CMake文章和其他各种关于它的SO帖子,并用我的CMakeLists.txt使它工作,但我失败了,我希望有人能在这里给我一些提示,说明我做错了什么

# CMake 2.8.11 and above required for linking to qtmain.lib library on Windows
# CMake 2.8.12 and above required for MACOSX_RPATH property
cmake_minimum_required(VERSION 2.8.12)
set(PROJECT_NAME "maya_standalone_test")

if(APPLE)
    message(STATUS "Setting MacOSX SDK...")
    set(CMAKE_OSX_SYSROOT "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk")
endif(APPLE)

project(${PROJECT_NAME})

set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/modules)

# Attempt to find existing installation of Maya and define variables
find_package(Maya REQUIRED)

# Add include search paths
include_directories(
  ${MAYA_INCLUDE_DIR}
  ${PROJECT_SOURCE_DIR}/include
)

if(APPLE)
    message(STATUS "Setting OSX Framework path to: ${MAYA_LOCATION}/Maya.app/Contents/Frameworks")
    set(CMAKE_FIND_FRAMEWORK FIRST)
    set(CMAKE_FRAMEWORK_PATH "${MAYA_LOCATION}/Maya.app/Contents/Frameworks")
endif(APPLE)

# Set compiler flags for each platform
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
    message(STATUS "Detected compiler as Clang...")
    # Using Clang 
    set(CMAKE_CXX_FLAGS "-stdlib=libstdc++ \
                        -std=c++0x \
                        -fno-gnu-keywords \
                        -fpascal-strings \
                        -O3 \
                        -dynamic")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -ggdb -glldb")
    set(CMAKE_EXE_LINKER_FLAGS "-stdlib=libstdc++ \
                                -std=c++0x \
                                -fno-gnu-keywords \
                                -fpascal-strings \
                                -O3 \
                                -Wl, \
                                -headerpad_max_install_names \
                                -dynamic")

elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    # using GCC on Linux
    set(CMAKE_C_FLAGS "-DBits64_ \
                      -m64 \
                      -DUNIX \
                      -D_BOOL \
                      -DLINUX \
                      -DFUNCPROTO \
                      -D_GNU_SOURCE \
                      -DLINUX_64 \
                      -fPIC \
                      -fno-strict-aliasing \
                      -DREQUIRE_IOSTREAM \
                      -O3 \
                      -Wall \
                      -Wno-multichar \
                      -Wno-comment \
                      -Wno-sign-compare \
                      -funsigned-char \
                      -pthread")
    set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} \
                        -Wno-deprecated \
                        -Wno-reorder \
                        -ftemplate-depth-25 \
                        -fno-gnu-keywords")
    set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -ggdb")

elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
    # using Visual Studio C++
    # TODO: Need to find the compiler flags 
    set(CMAKE_CXX_FLAGS "/MP")
    set(CMAKE_CXX_FLAGS_DEBUG "/MP /ZI /EHsc")
endif()

# Add project sources
add_subdirectory(src)

# Don't skip the full RPATH for the build/install tree
set(CMAKE_SKIP_RPATH FALSE)
set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)

if(APPLE)
    message (STATUS "Setting OSX-specific settings...")
    set(CMAKE_INSTALL_RPATH "${MAYA_LIBRARY_DIR}")
    set(CMAKE_INSTALL_NAME_DIR "@rpath")
else()
    # Disable RPATH stripping during installation so that the binary will link correctly
    # to the Maya libs
    set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
endif(APPLE)

set(EXECUTABLE_NAME ${PROJECT_NAME})
add_executable (${EXECUTABLE_NAME} ${PROJECT_SOURCES})

# Set preprocessor definitions
set_target_properties(${EXECUTABLE_NAME} PROPERTIES
    COMPILE_DEFINITIONS "${MAYA_COMPILE_DEFINITIONS}")

# Link the executable to the libraries
message(STATUS "Linking to Maya libraries at: ${MAYA_LIBRARY_DIR}")
target_link_libraries(${EXECUTABLE_NAME} ${MAYA_LIBRARIES})

install(TARGETS ${EXECUTABLE_NAME} DESTINATION ${PROJECT_SOURCE_DIR}/bin)
当我使用otool进行检查时,生成的可执行文件会给出:

Load command 41
          cmd LC_RPATH
      cmdsize 72
         path /Applications/Autodesk/maya2016/Maya.app/Contents/MacOS (offset 12)
这是正确的,但库路径是:

../bin/maya_standalone_test:
    @executable_path/libOpenMaya.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libOpenMayaAnim.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libOpenMayaFX.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libOpenMayaRender.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libOpenMayaUI.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libFoundation.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libclew.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libAnimSlice.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libDeformSlice.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libModifiers.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libDynSlice.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libKinSlice.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libModelSlice.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libNurbsSlice.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libPolySlice.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libProjectSlice.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libImage.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libShared.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libTranslators.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libDataModel.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libRenderModel.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libNurbsEngine.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libDependEngine.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libCommandEngine.dylib (compatibility version 0.0.0, current version 0.0.0)
    @executable_path/libIMFbase.dylib (compatibility version 0.0.0, current version 0.0.0)
    /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 104.1.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1226.10.1)
当然,当我运行应用程序时,我会得到:

dyld: Library not loaded: @executable_path/libOpenMaya.dylib
  Referenced from: /Users/sonictk/Git/experiments/maya_standalone_app_example/build/../bin/maya_standalone_test
  Reason: image not found
[1]    16909 trace trap  ../bin/maya_standalone_test
我尝试使用后期生成命令将每个库的@executable\u路径改为@rpath,但在找到库的同时,我遇到了可执行文件无法找到框架的问题(位于
${MAYA\u LOCATION}/MAYA.app/Contents/frameworks
)以及链接到同一文件夹中其他动态库的其他问题(如Maya的QtCore等)

当然,如果我将
Frameworks/
文件夹复制到可执行文件的父目录中,并将
${MAYA\u LIBRARY\u DIR}
中的所有文件复制到与可执行文件相同的文件夹中,这一切都会起作用,或者,如果我在启动可执行文件之前将
DYLD\u LIBARY\u路径
DYLD\u FRAMEWORK\u路径
设置为指向库和框架目录。但我希望OSX上的这些库和框架能够动态链接,而无需修改环境,就像我在Linux上所做的那样

我做错了什么