在mac上通过gcc编译iOS Objective-C命令行应用程序

发布于 2024-12-05 03:29:15 字数 623 浏览 2 评论 0原文

这是一个非常简单的 Objective-C 控制台应用程序:

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    NSLog(@"Hello world!");

    [pool drain];

    return 0;
}

我在 Mac 上使用 gcc main.m -o main -ObjC -framework Foundation -framework CoreLocation 编译它。

我的 Mac 上还安装了 iOS SDK。如何修改此命令以在我的计算机上编译相同的代码,以便在(越狱的)iOS 设备上使用

然后我可以通过 ssh 传输可执行文件并使用 ldid 对其进行签名。

Here is a very simple Objective-C console app:

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    CLLocationManager *locationManager = [[CLLocationManager alloc] init];
    NSLog(@"Hello world!");

    [pool drain];

    return 0;
}

I compile it with gcc main.m -o main -ObjC -framework Foundation -framework CoreLocation on my Mac.

I also have the iOS SDK installed on my mac. How can I modify this command to compile the same code, on my computer, for use on a (jailbroken) iOS device ?

I could then transfer the executable through ssh and sign it with ldid.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野侃 2024-12-12 03:29:15

对于简单的情况,这可能已经足够了:

gcc -o output -Wall -std=c99 source.m -framework Foundation -lobjc 

更新:

此命令不适用于 iOS 命令行应用程序 - 必须正确设置拱门。我目前手头没有iOS开发环境;也许 -march 标志会有所帮助。

查看已接受的答案。

For simple cases this might be good enough:

gcc -o output -Wall -std=c99 source.m -framework Foundation -lobjc 

Update:

This command does not work for an iOS command line application - the arch must be set correctly. I have no iOS dev environment at hand currently; probably the -march flag would help.

See the accepted answer.

永不分离 2024-12-12 03:29:15

假设您不习惯陈旧的旧 GCC,故意在我的项目中触发错误,以便进入 Xcode 将显示使用的命令行显示以下编译内容的位置:

/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang -x
Objective-c -arch armv6 -fmessage-length = 0
-fdiagnostics-print-source-range-info -fdiagnostics-show-category=id
-fdiagnostics-parseable-fixits -std=gnu99 -Wno-trigraphs
-fpascal-strings -O0 -Wmissing-prototypes -Wreturn-type -W括号
-Wswitch -Wno-unused-parameter -Wunused-variable -Wunused-value
-DDEBUG=1 -isysroot
/开发者/平台/iPhoneOS.platform/开发者/SDKs/iPhoneOS4.3.sdk
-gdwarf-2 -mthumb "-DIBOutlet=属性((iboutlet))"
"-DIBOutletCollection(ClassName)=属性((iboutletcollection(ClassName)))"
"-DIBAction=void)属性((ibaction)" -miphoneos-version-min=3.2
-iquote [簿记文件] -I[标题列表] -iquote [更多
headers] -I[包含路径] -fpch-preprocess -F[指向目录的指针
对于调试文件] -include [我的前缀标头] -c AppDelegate.m -o
AppDelegate.o

如果您拒绝使用 Xcode,那么我想您至少可以删除有关 Interface Builder 出口的内容。在我的项目中,为 armv6 而不是 v7 构建可能是一个错误。

然后,链接:

/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang -arch
ARMv6-isysroot
/开发者/平台/iPhoneOS.platform/开发者/SDKs/iPhoneOS4.3.sdk
-L[适当的东西] -F[适当的东西] -filelist [a
.LinkFileList] -dead_strip -miphoneos-version-min=3.2 -framework
系统配置-framework UIKit-framework 基础-framework
CoreGraphics -o [适当的东西]

.LinkedFileList 文件似乎只是一个目标文件列表,每行一个。 Xcode 已将每个路径的完整路径放在那里,但我猜相对路径是可以接受的。

我知道这不是完整的答案,但希望它有帮助吗?

Assuming you're not wedded to fusty old GCC, deliberately triggering errors in my project so as to get into a position where Xcode will reveal the command line used revealed the following for compilation:

/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang -x
objective-c -arch armv6 -fmessage-length=0
-fdiagnostics-print-source-range-info -fdiagnostics-show-category=id
-fdiagnostics-parseable-fixits -std=gnu99 -Wno-trigraphs
-fpascal-strings -O0 -Wmissing-prototypes -Wreturn-type -Wparentheses
-Wswitch -Wno-unused-parameter -Wunused-variable -Wunused-value
-DDEBUG=1 -isysroot
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk
-gdwarf-2 -mthumb "-DIBOutlet=attribute((iboutlet))"
"-DIBOutletCollection(ClassName)=attribute((iboutletcollection(ClassName)))"
"-DIBAction=void)attribute((ibaction)" -miphoneos-version-min=3.2
-iquote [a bookkeeping file] -I[a list of headers] -iquote [more
headers] -I[an include path] -fpch-preprocess -F[pointer to directory
for debug files] -include [my prefix header] -c AppDelegate.m -o
AppDelegate.o

If you're declining the use of Xcode then I guess you can cut the stuff about Interface Builder outlets at the very least. And building for armv6 rather than v7 is probably an error in my project.

And then, to link:

/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/clang -arch
armv6 -isysroot
/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.3.sdk
-L[something appropriate] -F[something appropriate] -filelist [a
.LinkFileList] -dead_strip -miphoneos-version-min=3.2 -framework
SystemConfiguration -framework UIKit -framework Foundation -framework
CoreGraphics -o [something appropriate]

The .LinkedFileList file seems just to be a list of object files, one per line. Xcode has put the full path to each in there, though I'd guess relative paths to be acceptable.

I appreciate this isn't a full answer, but hopefully it helps?

风启觞 2024-12-12 03:29:15

要在Mac上编译iOS代码,您需要使用交叉编译器,即运行在Intel平台上并且能够生成ARM二进制代码的编译器。

Xcode确实提供了这样的交叉编译器。它位于(假设您使用的是从 App Store 分发的 Xcode 4.3)

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-llvm-gcc-4.2

对于一个简单的“hello world”类型的程序,我正在使用

alias c="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-llvm-gcc-4.2"
c -o hello hello.c -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/

To compile iOS code on your mac, you need to use cross compiler, i.e. an compiler running on an Intel platform and able to generate ARM binary code.

Xcode does provide such a cross compiler. It is located at (assuming you are using Xcode 4.3 distributed from the App Store)

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-llvm-gcc-4.2

For a simple, "hello world" kind of program, I am compiling using

alias c="/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/arm-apple-darwin10-llvm-gcc-4.2"
c -o hello hello.c -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk/
诺曦 2024-12-12 03:29:15

为什么不使用xcodebuild而不是直接调用gcc

Why not using xcodebuild instead of directly call gcc?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文