CMake Build Mac应用程序 [英] CMake Build Mac App

查看:301
本文介绍了CMake Build Mac应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们公司目前使用CMake来为Windows构建我们的可执行文件.我正在努力使我们的应用程序在Mac上运行.到目前为止,该应用程序在Mac上均可正常运行.但是,当我尝试打开CMake为Mac创建的可执行文件时,在终端窗口中出现以下错误:

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]

我认为该项目的CMakeLists.txt可能无法正确设置以生成Mac可执行文件.我将其包含在下面:

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?

推荐答案

在启动应用程序之前,应将包含libAbcSupport.dylib的目录的路径添加到DYLD_LIBRARY_PATH环境变量.

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.

对于程序使用的每个库,动态链接器依次在DYLD_LIBRARY_PATH中的每个目录中查找它.如果仍然找不到该库,则会依次搜索DYLD_FALLBACK_FRAMEWORK_PATHDYLD_FALLBACK_LIBRARY_PATH.

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 searches DYLD_FALLBACK_FRAMEWORK_PATH and DYLD_FALLBACK_LIBRARY_PATH in turn.

如果您希望立即使用此功能,即无需手动设置此变量,那么例如,您只需将适当的安装过程添加到CMakeLists中即可.默认情况下,DYLD_LIBRARY_PATH可能包含一些目录,系统目录和用户目录.只需通过以下方法进行检查:

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

,并参考文档的哪个目录(在常规目录中更可取)在Mac OS X上为第三方应用程序部署库.然后您要做的就是对CMakeLists进行编程,以便在运行它将libAbcSupport.dylib部署到该目录中.

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.

注意::在Windows上您不会遇到此问题,因为Windows也在搜索PATH环境变量以查找DLL时,也会在应用程序的当前目录(即Mac OS X和Linux都不是如此).换句话说,在Windows上,您很可能将AbcSupport.dll部署在与应用程序相同的目录中,因此不必为此担心.

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.

注意:在这方面,Linux与Mac OS X相似.因此,如果您也必须将应用程序移植到Linux,请不要忘记在那里需要LD_LIBRARY_PATH.这是 LD.SO(8)手册页《 Linux程序员手册》以及相关摘录:

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:

LD_LIBRARY_PATH环境变量包含用冒号分隔的目录列表,这些目录由动态链接程序在寻找要加载的共享库时.

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.

如果未指定,则链接器将使用默认值,即/lib:/usr/lib:/usr/local/lib.

If not specified, the linker uses the default, which is /lib:/usr/lib:/usr/local/lib.

这篇关于CMake Build Mac应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆