简体   繁体   English

如何从命令行编译XCode 4.5项目

[英]How can I compile XCode 4.5 project from the command line

I've created simple Command line app from XCode 4.5, however I'd need to compile it from the command line. 我已经从XCode 4.5创建了简单的命令行应用程序,但是我需要从命令行对其进行编译。 If I remove @autoreleasepool block and literal array initialization I can compile it with following command: 如果删除@autoreleasepool块和文字数组初始化 ,则可以使用以下命令对其进行编译:

gcc main.m -o prog -ObjC -framework Foundation

Is it possible to compile it with autoreleasepool and literals? 是否可以使用 autoreleasepool和文字对其进行编译?

main.m (for clarity I've removed irrelevant parts) main.m (为清楚起见,我删除了不相关的部分)

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

    @autoreleasepool
    {
        NSArray *array = @[@"foo", @"bar"];
        NSLog(@"%@", array);
    }
    return 0;
}

Output: 输出:

> gcc main.m -o prog -ObjC -framework Foundation
main.m: In function ‘main’:
main.m:6: error: stray ‘@’ in program
main.m:6: error: ‘autoreleasepool’ undeclared (first use in this function)
main.m:6: error: (Each undeclared identifier is reported only once
main.m:6: error: for each function it appears in.)
main.m:7: error: expected ‘;’ before ‘{’ token
main.m:8: error: stray ‘@’ in program

If you don't mind getting xcodebuild to build it for you (rather than invoking the compiler yourself), you can do this: 如果您不介意让xcodebuild为您构建它(而不是自己调用编译器),则可以执行以下操作:

xcrun xcodebuild -project path/to/project.xcodeproj -target NameOfTarget -configuration Release

To send the build products to a location that you specify, you can add 'CONFIGURATION_BUILD_DIR=path/to/build_output_directory' to the command line arguments. 要将构建产品发送到您指定的位置,您可以在命令行参数中添加'CONFIGURATION_BUILD_DIR=path/to/build_output_directory'

See the xcodebuild help for alternative ways to invoke it. 有关调用它的替代方法,请参见xcodebuild帮助。

clang is the preferred compiler for Objective-C. clang是Objective-C的首选编译器。 New features (autorelease scopes, ARC, literals, etc.) aren't added to gcc, and it'll be removed in a future Xcode release. 新功能(自动发布范围,ARC,文字等)不会添加到gcc中,并且会在以后的Xcode版本中将其删除。

Usage of clang from the commandline is very similar to gcc, so this should work instead: 从命令行使用clang与gcc非常相似,因此应该可以使用:

clang main.m -o prog -ObjC -framework Foundation

You'll need to have the Xcode command line tools (Xcode menu > Open Developer Tool > More Developer Tools) installed. 您需要安装Xcode命令行工具(Xcode菜单>打开开发人员工具>更多开发人员工具)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM