捆绑应用程序中的dylib和框架

时间:2013-01-12 08:29:45

标签: c++ macos deployment homebrew

昨天我正在阅读有关部署MacOSX应用程序的很多内容,但我仍有一些疑问。在过去的几年里,我一直在使用 macdeployqt 为MacOSX部署Qt4应用程序。现在,mi应用程序使用的库不属于Qt框架:Poppler。

我使用 Homebrew 安装了poppler-qt4:

brew install poppler --with-qt4 --enable-xpdf-headers

使用 macdeployqt 之后,我知道我必须使用 install_name_tool 来改变相对路径的绝对路径。一些dylib也有依赖:

MyApp.app/Contents/MacOS/MyApp:
    /usr/local/lib/libpoppler-qt4.4.dylib

/usr/local/lib/libpoppler-qt4.4.dylib:
  /usr/local/Cellar/poppler/0.20.5/lib/libpoppler.28.dylib
  /usr/local/lib/libfontconfig.1.dylib
  /usr/local/lib/QtCore.framework/Versions/4/QtCore
  /usr/local/lib/QtGui.framework/Versions/4/QtGui
  /usr/local/lib/QtXml.framework/Versions/4/QtXml

使用 macdeployqt 之后我知道Qt框架已经被复制到应用程序包中,如何更改 /usr/local/lib/QtCore.framework/Versions/4/QtCore < / em>使用 install_name_tool

的相对路径

使用 -headerpad_max_install_names 编译自制的dylib?

是否有使用brew自动执行此操作的方法?

install_name_tool 应该使用什么? @executable_path或@loader_path?

编辑:似乎macdeployqt足够聪明,可以部署第三方dylib,poppler-qt4及其所有依赖项被复制到Applicaciont.app/Frameworks文件夹,并自动使用install_name_tool。但是,现在我正在遭遇这个BUG:macdeployqt not copying plugins我想问题是poppler-qt4.4的名字是“qt”。

1 个答案:

答案 0 :(得分:1)

您可以手动更改所有路径,但这样做容易出错,冗长而痛苦。 我建议使用一个工具为你做:cpack包含cpack。

无论如何,你应该使用:@loader_path。请参阅Dylib

对于MacOS X,以下代码即使使用root权限也会创建一个包并正确复制homebrew dylib。 但是,您必须检查使用自制软件编译的每个lib都具有正确的参数。

通常,对于deployement,最好手动编译libs。我在使用自制库(ffmpeg)时遇到了一些问题。但不是Qt也不是Boost: - )。

if( USE_QT5 )
     qt5_use_modules( MyApp Core OpenGL Sql Multimedia Concurrent )
endif()

# Install stuff
set( plugin_dest_dir bin )
set( qtconf_dest_dir bin )
set( APPS "\${CMAKE_INSTALL_PREFIX}/bin/MyApp" )
if( APPLE )
    set( plugin_dest_dir MyApp.app/Contents/ )
    set( qtconf_dest_dir MyApp.app/Contents/Resources )
    set( APPS "\${CMAKE_INSTALL_PREFIX}/MyApp.app" )
endif( APPLE )
if( WIN32 )
    set( APPS "\${CMAKE_INSTALL_PREFIX}/bin/MyApp.exe" )
endif( WIN32 )

#--------------------------------------------------------------------------------
# Install the MyApp application, on Apple, the bundle is at the root of the
# install tree, and on other platforms it'll go into the bin directory.
install( TARGETS MyApp
    BUNDLE DESTINATION . COMPONENT Runtime
    RUNTIME DESTINATION bin COMPONENT Runtime
)

#--------------------------------------------------------------------------------
# Install needed Qt plugins by copying directories from the qt installation
# One can cull what gets copied by using 'REGEX "..." EXCLUDE'
install( DIRECTORY "${QT_PLUGINS_DIR}/imageformats"
    "${QT_PLUGINS_DIR}/codecs"
    "${QT_PLUGINS_DIR}/phonon_backend"
    "${QT_PLUGINS_DIR}/sqldrivers"
    "${QT_PLUGINS_DIR}/accessible"
    "${QT_PLUGINS_DIR}/bearer"
    "${QT_PLUGINS_DIR}/graphicssystems"
    DESTINATION ${plugin_dest_dir}/PlugIns
    COMPONENT Runtime
    FILES_MATCHING
    PATTERN "*.dylib"
    PATTERN "*_debug.dylib" EXCLUDE
)

#--------------------------------------------------------------------------------
# install a qt.conf file
# this inserts some cmake code into the install script to write the file
install( CODE "
    file(WRITE \"\${CMAKE_INSTALL_PREFIX}/${qtconf_dest_dir}/qt.conf\" \"\")
    " COMPONENT Runtime
)


#--------------------------------------------------------------------------------
# Use BundleUtilities to get all other dependencies for the application to work.
# It takes a bundle or executable along with possible plugins and inspects it
# for dependencies.  If they are not system dependencies, they are copied.

# directories to look for dependencies
set( DIRS ${QT_LIBRARY_DIRS} ${MYAPP_LIBRARIES} )

# Now the work of copying dependencies into the bundle/package
# The quotes are escaped and variables to use at install time have their $ escaped
# An alternative is the do a configure_file() on a script and use install(SCRIPT  ...).
# Note that the image plugins depend on QtSvg and QtXml, and it got those copied
# over.

install( CODE "
    file(GLOB_RECURSE QTPLUGINS
      \"\${CMAKE_INSTALL_PREFIX}/${plugin_dest_dir}/plugins/*${CMAKE_SHARED_LIBRARY_SUFFIX}\")
    set(BU_CHMOD_BUNDLE_ITEMS ON)
    include(BundleUtilities)
    fixup_bundle(\"${APPS}\" \"\${QTPLUGINS}\" \"${DIRS}\")
    " COMPONENT Runtime
)

# To Create a package, one can run "cpack -G DragNDrop CPackConfig.cmake" on Mac OS X
# where CPackConfig.cmake is created by including CPack
# And then there's ways to customize this as well
set( CPACK_BINARY_DRAGNDROP ON )
include( CPack )