使用带有Apple核心库(如Foundation.h)的OSX上的gcc-4.7编译Objective C文件

时间:2013-01-29 13:13:13

标签: objective-c macos gcc

我无法用GCC 4.7编译包含任何OSX核心库的Objective C文件,例如:

#import <Foundation/Foundation.h>

产生的错误总是会导致奇怪的GCC输出:

gcc -c -pipe -O2 -arch x86_64 -mmacosx-version-min=10.8 -Wall -W  -I/usr/local/Cellar/qt/4.8.4/mkspecs/macx-g++ -I../TerrainFluid -I../TerrainFluid/external -I../TerrainFluid -I. ../TerrainFluid/external/osx_bundle.mm -o osx_bundle.o

In file included from 

/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:123:0,
                 from ../TerrainFluid/external/osx_bundle.mm:24:
/System/Library/Frameworks/Foundation.framework/Headers/NSTask.h:69:24: error: expected unqualified-id before '^' token
/System/Library/Frameworks/Foundation.framework/Headers/NSTask.h:69:24: error: expected ')' before '^' token
In file included from /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:159:0,
                 from ../TerrainFluid/external/osx_bundle.mm:24:
/System/Library/Frameworks/Foundation.framework/Headers/NSUserScriptTask.h:36:15: error: expected unqualified-id before '^' token
/System/Library/Frameworks/Foundation.framework/Headers/NSUserScriptTask.h:36:15: error: expected ')' before '^' token
/System/Library/Frameworks/Foundation.framework/Headers/NSUserScriptTask.h:37:39: error: expected type-specifier before 'NSUserScriptTaskCompletionHandler'
/System/Library/Frameworks/Foundation.framework/Headers/NSUserScriptTask.h:53:15: error: expected unqualified-id before '^' token
/System/Library/Frameworks/Foundation.framework/Headers/NSUserScriptTask.h:53:15: error: expected ')' before '^' token
/System/Library/Frameworks/Foundation.framework/Headers/NSUserScriptTask.h:54:70: error: expected type-specifier before 'NSUserUnixTaskCompletionHandler'
/System/Library/Frameworks/Foundation.framework/Headers/NSUserScriptTask.h:68:15: error: expected unqualified-id before '^' token
/System/Library/Frameworks/Foundation.framework/Headers/NSUserScriptTask.h:68:15: error: expected ')' before '^' token
/System/Library/Frameworks/Foundation.framework/Headers/NSUserScriptTask.h:69:82: error: expected type-specifier before 'NSUserAppleScriptTaskCompletionHandler'
/System/Library/Frameworks/Foundation.framework/Headers/NSUserScriptTask.h:86:15: error: expected unqualified-id before '^' token
/System/Library/Frameworks/Foundation.framework/Headers/NSUserScriptTask.h:86:15: error: expected ')' before '^' token
/System/Library/Frameworks/Foundation.framework/Headers/NSUserScriptTask.h:87:72: error: expected type-specifier before 'NSUserAutomatorTaskCompletionHandler'
In file included from /System/Library/Frameworks/Foundation.framework/Headers/Foundation.h:160:0,
                 from ../TerrainFluid/external/osx_bundle.mm:24:
/System/Library/Frameworks/Foundation.framework/Headers/NSXPCConnection.h:46:11: error: expected unqualified-id before '^' token
/System/Library/Frameworks/Foundation.framework/Headers/NSXPCConnection.h:46:11: error: expected ')' before '^' token
/System/Library/Frameworks/Foundation.framework/Headers/NSXPCConnection.h:46:11: error: expected ';' before '^' token
/System/Library/Frameworks/Foundation.framework/Headers/NSXPCConnection.h:47:11: error: expected unqualified-id before '^' token
/System/Library/Frameworks/Foundation.framework/Headers/NSXPCConnection.h:47:11: error: expected ')' before '^' token
/System/Library/Frameworks/Foundation.framework/Headers/NSXPCConnection.h:47:11: error: expected ';' before '^' token
make: *** [osx_bundle.o] Error 1

解决这个问题的方法是让我只使用Apple编译器作为Objective C文件,使用g ++(4.7)代替其余代码。

gcc -> /usr/bin/llvm-gcc
g++ -> /usr/local/bin/g++-4.7

我会对所有代码使用clang,但它不支持OpenMP,而gcc-4.2不支持C ++ 11。

Here是我使用的示例文件(它是Wildfire Games OSX资源包加载器)。我在blog post中读到Apple引入GCC阻止程序以阻止与GCC编译。

现在有没有办法用非Apple GCC(如GCC 4.7)编译代码?到目前为止,除了使用Apple编译器之外,我没有找到关于此事的文档。

1 个答案:

答案 0 :(得分:0)

第一个错误显示问题。

@property (copy) void (^terminationHandler)(NSTask *) NS_AVAILABLE(10_7, NA);

这是Blocks extension。主线GCC不实现对块的支持。