gpt4 book ai didi

objective-c - 为什么使用cocoa库的HelloWorld代码无法构建?

转载 作者:行者123 更新时间:2023-12-03 17:52:22 24 4
gpt4 key购买 nike

代码如下:

#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
int main(int argc, const char * argv[])
{

@autoreleasepool {

// insert code here...

NSLog(@"Hello, World!");
}
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Hi there."];
[alert runModal];


return 0;
}

我想要的是:当从命令行调用时,当我关闭警报框时,该程序会弹出一个警报框。程序退出。

但是在构建时,它会这样提示:

Ld /Users/hanfei/Library/Developer/Xcode/DerivedData/KeyCatcher-hijnrqhwiafuxtdjmdubtsijyhwh/Build/Products/Debug/KeyCatcher normal x86_64
cd /Users/hanfei/Desktop/KeyCatcher
setenv MACOSX_DEPLOYMENT_TARGET 10.9
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -L/Users/hanfei/Library/Developer/Xcode/DerivedData/KeyCatcher-hijnrqhwiafuxtdjmdubtsijyhwh/Build/Products/Debug -F/Users/hanfei/Library/Developer/Xcode/DerivedData/KeyCatcher-hijnrqhwiafuxtdjmdubtsijyhwh/Build/Products/Debug -filelist /Users/hanfei/Library/Developer/Xcode/DerivedData/KeyCatcher-hijnrqhwiafuxtdjmdubtsijyhwh/Build/Intermediates/KeyCatcher.build/Debug/KeyCatcher.build/Objects-normal/x86_64/KeyCatcher.LinkFileList -mmacosx-version-min=10.9 -fobjc-arc -fobjc-link-runtime -framework Foundation -Xlinker -dependency_info -Xlinker /Users/hanfei/Library/Developer/Xcode/DerivedData/KeyCatcher-hijnrqhwiafuxtdjmdubtsijyhwh/Build/Intermediates/KeyCatcher.build/Debug/KeyCatcher.build/Objects-normal/x86_64/KeyCatcher_dependency_info.dat -o /Users/hanfei/Library/Developer/Xcode/DerivedData/KeyCatcher-hijnrqhwiafuxtdjmdubtsijyhwh/Build/Products/Debug/KeyCatcher

Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_NSAlert", referenced from:
objc-class-ref in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

当我为新项目选择模板时,我选择命令行工具而不是CoCoa Application,因为我只需要CoCoa来显示警报框。有没有人对此有想法..

最佳答案

那是因为 NSAlert 不是#imported

AppKit.h当您创建命令行工具时,默认情况下不包含此内容,正如您在链接中看到的,AppKit 导入 NSAlert.h

编辑:
首先,要编译,您需要在项目中添加 Cocoa 框架。

其次,所有代码都应包含在 @autoreleasepool 部分

@autoreleasepool {

// insert code here...
NSLog(@"Hello, World!");
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Hi there."];
[alert runModal];
}

您的代码将编译并运行,但我认为您还会遇到一些其他运行时错误。

关于objective-c - 为什么使用cocoa库的HelloWorld代码无法构建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22059968/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com