我花了一个星期的时间试图获得一些我在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

我尝试使用post build命令将@executable_path更改为@rpath而不是每个库,但是当找到库时,我遇到了可执行文件无法找到框架(位于 ${MAYA_LOCATION}/Maya.app/Contents/Frameworks )和其他问题的问题链接到同一文件夹中的其他动态库(例如Maya的QtCore等)

当然,如果我将 Frameworks/ 文件夹复制到我的可执行文件的父目录,并将 ${MAYA_LIBRARY_DIR} 中的所有文件复制到与可执行文件相同的文件夹中,或者如果我将 DYLD_LIBARY_PATHDYLD_FRAMEWORK_PATH 设置为指向库和框架指南之前,这一切都有效 . 启动我的可执行文件 . 但我希望OSX上的这些库和框架能够动态链接,而不必像在Linux上那样修改环境 .

我在这做错了什么?