Linux 上的 Objective-C 编译错误

标签 objective-c linux ubuntu gnustep

关于如何做到这一点的教程似乎有很多,每个都略有不同。我希望有人能够识别我收到的错误消息并为我指明正确的方向。

我的代码,h.m 是:

#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 NSLog (@"hello world");
 [pool drain];
 return 0;
}

编译前,我在控制台输入:

. /usr/share/GNUstep/Makefiles/GNUstep.sh

我尝试编译:

gcc `gnustep-config --objc-flags` -lgnustep-base h.m -o hello

并得到:

/tmp/ccgLOnpY.o: In function `main':
/home/ge/objective-c/h.m:4: undefined reference to `objc_get_class'
/home/ge/objective-c/h.m:4: undefined reference to `objc_msg_lookup'
/home/ge/objective-c/h.m:4: undefined reference to `objc_msg_lookup'
/home/ge/objective-c/h.m:5: undefined reference to `NSLog'
/home/ge/objective-c/h.m:6: undefined reference to `objc_msg_lookup'
/tmp/ccgLOnpY.o: In function `__objc_gnu_init':
/home/ge/objective-c/h.m:8: undefined reference to `__objc_exec_class'
/tmp/ccgLOnpY.o:(.data.rel+0x0): undefined reference to `__objc_class_name_NSConstantString'
/tmp/ccgLOnpY.o:(.data.rel+0x8): undefined reference to `__objc_class_name_NSAutoreleasePool'
collect2: ld returned 1 exit status

有人能指出我正确的方向吗?

TIA

最佳答案

链接错误的原因很可能是由于链接器的行为仅在链接库之前看到编译中的符号后才链接库。由于 h.m 出现在 -lgnustep-base 之后,库未链接,因为尚未遇到库中的符号。您可以指示链接器链接库,即使没有使用 -Wl,--no-as-needed 链接器选项作为

gcc `gnustep-config --objc-flags` -Wl,--no-as-needed -lgnustep-base  h.m -o hello

或者更好的是将源代码移动到编译命令的开头作为

gcc h.m  `gnustep-config --objc-flags` -lgnustep-base -o hello

观察到的链接器行为是不鼓励查找和链接可能不需要但无论如何都在编译中链接的库符号,因此建议使用第二个选项而不是添加 -Wl, --no-as-needed 链接器选项。
希望这对您有所帮助!

关于Linux 上的 Objective-C 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11425120/

相关文章:

ios - 在 iOS 中将视频作为背景的最有效方法

linux - 通过 USB 将 Linux native 文件系统作为 VFAT 或 NTFS 导出到 Windows

linux - '没有那个文件或目录',但可以 'stat' 文件

c++ - 解析位图文件时,如何替换WORD和DWORD数据类型?

python - 帮助安装 Swampy python 模块?

ios - CALayer.contentsScale 是什么意思?

c# - iPhone 应用程序 InputAccessoryView 未出现在 iPad 上

ubuntu - DSBulk 加载程序版本 1.8 : error in loading and connecting to Apache Cassandra

maven - 如何在Ubuntu-14.04中彻底清理Maven

ios - 在 iCloud 中存储 >1MB 数组的最佳方式?