xcode 目标 C 项目的 CMakeLists.txt
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20962869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
CMakeLists.txt for an Objective C Project
提问by bhawley
I am working on simply creating a hello world executable on OSX using Objective-C and CMake. I'm new to both so I'd appreciate any advice. I understand that CMake does not natively support building Objective-C. You must first add the
我正在使用 Objective-C 和 CMake 在 OSX 上简单地创建一个 hello world 可执行文件。我对两者都是新手,所以我很感激任何建议。我知道 CMake 本身不支持构建 Objective-C。您必须先添加
set(CMAKE_C_FLAGS "-x objective-c")
or
或者
set(CMAKE_CXX_FLAGS "-x objective-c++")
for the Makefile to even be created. The part where I'm getting stuck is how to appropriately link the existing frameworks. I've followed both the instructions in this post as well as the tutorial it's based off of: Can't link MacOS frameworks with CMakeI've also tried linking the libraries with:
甚至可以创建 Makefile。我陷入困境的部分是如何适当地链接现有框架。我遵循了这篇文章中的说明以及它基于的教程:无法将 MacOS 框架与 CMake 链接我还尝试将库链接到:
set(CMAKE_EXE_LINKER_FLAGS "-framework fm1-framework fm2…")
I can now get a successful CMake generated Xcode project, but when I open the project, none of the frameworks I specified in the CMakeLists.txt file are in the 'resources' folder. Using this Xcode project, I can get the project to compile, but when I run the executable I get the error:
我现在可以获得一个成功的 CMake 生成的 Xcode 项目,但是当我打开该项目时,我在 CMakeLists.txt 文件中指定的框架都不在“资源”文件夹中。使用这个 Xcode 项目,我可以编译项目,但是当我运行可执行文件时出现错误:
"2014-01-06 18:02:35.859 HelloWorld[62641:303] No Info.plist file in application bundle or no NSPrincipalClass in the Info.plist file, exiting Program ended with exit code: 1"
“2014-01-06 18:02:35.859 HelloWorld[62641:303] 应用程序包中没有 Info.plist 文件或 Info.plist 文件中没有 NSPrincipalClass,退出程序以退出代码结束:1”
I also tried manually running the makefile made by CMake in the terminal and got many many warnings (during linking) all saying:
我还尝试在终端中手动运行 CMake 制作的 makefile 并收到许多警告(在链接期间),所有警告都说:
CMakeFiles/HelloWorld.dir/AppDelegate.m.o:3:2252: warning: null character ignored [-Wnull-character] ...
CMakeFiles/HelloWorld.dir/AppDelegate.m.o:3:2263: warning: null character ignored [-Wnull-character] ...
CMakeFiles/HelloWorld.dir/AppDelegate.mo:3:2252: 警告:忽略空字符 [-Wnull-character] ...
CMakeFiles/HelloWorld.dir/AppDelegate.mo:3:2263: 警告:忽略空字符 [-Wnull-character] ...
And 7 errors (during linking):
和 7 个错误(链接期间):
CMakeFiles/HelloWorld.dir/AppDelegate.m.o:1:1: error: expected unqualified-id
CMakeFiles/HelloWorld.dir/AppDelegate.m.o:3:90: error: expected unqualified-id ...H<89>uH<89>U]fffff.<84>
CMakeFiles/HelloWorld.dir/AppDelegate.m.o:3:121: error: extraneous closing brace ('}') ...}H<89>uH<8B>uH<8B>=
CMakeFiles/HelloWorld.dir/AppDelegate.m.o:3:122: error: expected unqualified-id ...H<89>uH<8B>uH<8B>=
CMakeFiles/HelloWorld.dir/AppDelegate.m.o:3:169: error: extraneous closing brace ('}') ...}H<89>uH<89>UH<8B>uH<8B>=
CMakeFiles/HelloWorld.dir/AppDelegate.m.o:3:170: error: expected unqualified-id ...H<89>uH<89>UH<8B>uH<8B>=
CMakeFiles/HelloWorld.dir/AppDelegate.m.o:3:2246: error: expected unqualified-id ...16@0:8
CMakeFiles/HelloWorld.dir/AppDelegate.mo:1:1: 错误:预期的不合格 ID
CMakeFiles/HelloWorld.dir/AppDelegate.mo:3:90: 错误:预期不合格的 id ...H<89>uH<89>U]fffff.<84>
CMakeFiles/HelloWorld.dir/AppDelegate.mo:3:121: 错误:多余的右大括号 ('}') ...}H<89>uH<8B>uH<8B>=
CMakeFiles/HelloWorld.dir/AppDelegate.mo:3:122: 错误:预期的不合格 ID ...H<89>uH<8B>uH<8B>=
CMakeFiles/HelloWorld.dir/AppDelegate.mo:3:169: 错误:多余的右大括号 ('}') ...}H<89>uH<89>UH<8B>uH<8B>=
CMakeFiles/HelloWorld.dir/AppDelegate.mo:3:170: 错误:预期的不合格 ID ...H<89>uH<89>UH<8B>uH<8B>=
CMakeFiles/HelloWorld.dir/AppDelegate.mo:3:2246: 错误:预期不合格 ID ...16@0:8
Any ideas on what I'm during wrong? I've spent the better portion of the day looking for answers and the post I referenced had the most useful information, but it still didn't fix my problem. Is it because the original program is also written in Xcode? And somehow that's messing up the CMake?
关于我在错误期间的任何想法?我花了一天的大部分时间寻找答案,我引用的帖子提供了最有用的信息,但它仍然没有解决我的问题。是不是因为原来的程序也是用Xcode写的?不知何故,这弄乱了 CMake?
Here's the code for my HelloWorld app:
这是我的 HelloWorld 应用程序的代码:
#import <Cocoa/Cocoa.h>
int main(int argc, const char * argv[])
{
return NSApplicationMain(argc, argv);
}
And the code for the CMakeLists.txt file:
以及 CMakeLists.txt 文件的代码:
cmake_minimum_required(VERSION 2.8)
project(HelloWorld)
set(src_files
AppDelegate.h
AppDelegate.m
main.m
)
set(CMAKE_C_FLAGS "-x objective-c")
set(CMAKE_CXX_FLAGS "-x objective-c++")
set(CMAKE_EXE_LINKER_FLAGS "-framework Cocoa -framework AppKit -framework CoreData - framework Foundation")
add_executable(HelloWorld
${src_files}
)
Thanks in advance!
提前致谢!
回答by bhawley
Indirectly thanks to ruslo and the source code located here: https://github.com/forexample/cocoa-appI figured out the problem. It appears for CMake to work with a plist generated by Xcode, one MUSTuse the command line instructions specified in the README.md in the above mentioned repo. The GUI implementation of CMake will NOTwork because the GUI has trouble interpreting the plist file. For clarity, I'll list them here:
间接感谢 ruslo 和位于此处的源代码:https: //github.com/forexample/cocoa-app我找到了问题所在。CMake 似乎可以使用由 Xcode 生成的 plist,必须使用上述 repo 中 README.md 中指定的命令行指令。cmake的GUI实现将不工作,因为GUI有麻烦解释plist文件。为清楚起见,我将在此处列出它们:
cd to the directory containing the CMakeLists.txt file.
cd 到包含 CMakeLists.txt 文件的目录。
cmake -H. -B_OutputDirectory -GXcode
cmake -H。-B_OutputDirectory -GXcode
cmake --build _OutputDirectory/
cmake --build _OutputDirectory/
open _OutputDirectory/HelloWorld.xcodeproj/
打开_OutputDirectory/HelloWorld.xcodeproj/
The other issue I encountered was that my plist and MainWindow.xib weren't included in my original CMake file. After adding the MainWindow.xib and the HelloWorld-info.plist necessary code, I was able to generate a usable Xcode project. Here's the code for reference:
我遇到的另一个问题是我的 plist 和 MainWindow.xib 没有包含在我的原始 CMake 文件中。添加 MainWindow.xib 和 HelloWorld-info.plist 必要代码后,我能够生成一个可用的 Xcode 项目。下面是代码供参考:
cmake_minimum_required(VERSION 2.8)
project(HelloWorld)
set(NAME HelloWorld)
set(CMAKE_C_FLAGS "-x objective-c")
set(HEADER_FILES
./uhdplayerengine/HelloWorld/HelloWorld/AppDelegate.h
)
set(SOURCE_FILES
./uhdplayerengine/HelloWorld/HelloWorld/AppDelegate.m
./uhdplayerengine/HelloWorld/HelloWorld/main.m
)
set(XIB_FILE
./uhdplayerengine/HelloWorld/HelloWorld/Base.lproj/MainMenu.xib
)
add_executable(
${NAME}
MACOSX_BUNDLE
${HEADER_FILES}
${SOURCE_FILES}
${XIB_FILE}
)
set_source_files_properties(
${XIB_FILE}
PROPERTIES
MACOSX_PACKAGE_LOCATION
Resources
)
set_target_properties(
${NAME}
PROPERTIES
MACOSX_BUNDLE_INFO_PLIST
./uhdplayerengine/HelloWorld/HelloWorld/HelloWorld-Info.plist
)
target_link_libraries(${NAME}
"-framework Cocoa"
"-framework AppKit"
"-framework CoreData"
"-framework Foundation"
)
I hope this saves someone else a headache! Thanks ruslo!
我希望这可以避免其他人头痛!谢谢鲁鲁!