5

我是 Iphone 编程的初学者。我正在尝试编译(ubuntu)。

#import <Foundation/Foundation.h>

int main (void)
 {
  NSLog (@"Executing");
  return 0;
 }

我编译了它,但出现以下错误

   subhash@subhash-Lenovo-G570:~/grit/iphone/mac$ gcc -lgnustep-base -lpthread -lob
  jc -lm -I/usr/local/include/GNUstep -I/usr/include/GNUstep -fconstant-string-cla
  ss=NSConstantString hello.m -o hello
  In file included from /usr/include/GNUstep/Foundation/NSClassDescription.h:30:0,
                  from /usr/include/GNUstep/Foundation/Foundation.h:50, 
                  from hello.m:1:
  /usr/include/GNUstep/Foundation/NSException.h:42:2: error: #error The current se
  tting for native-objc-exceptions does not match that of gnustep-base ... please 
  correct this.

我按照http://ubuntuforums.org/showthread.php?p=5593608作为参考。

我评论了 NSException.h 的#error 指令,问题就解决了。现在我得到了新的错误。

/tmp/ccQlI9wJ.o: In function `main':
        hello.m:(.text+0x11): undefined reference to `NSLog'
        /tmp/ccQlI9wJ.o: In function `__objc_gnu_init':
        hello.m:(.text+0x2a): undefined reference to `__objc_exec_class'
        /tmp/ccQlI9wJ.o:(.data+0x40): undefined reference to `__objc_class_name_NSConsta
        ntString'
        collect2: ld returned 1 exit status
4

2 回答 2

3

使用 gcc 编译 Objective-C 程序 中有以下内容

另请注意,如果您没有包含 -D_NATIVE_OBJC_EXCEPTIONS,您可能会遇到以下错误:

/usr/include/GNUstep/Foundation/NSException.h:42:2: error: #error The
current setting for native-objc-exceptions does not match that of
gnustep-base ... please correct this.

我有与原始海报相同的错误,传递-D_NATIVE_OBJC_EXCEPTIONS标志为我解决了问题。我试图做一些非常不标准的事情,所以它可能并不适合所有人。

请注意,shalki 的回答也可以解决问题。如果那里引用的链接消失了,有问题的博客文章 Compile Objective-C Programs on Linux is in Chinese or Japanese or like that,所以我不知道它到底在说什么,但我认为结果是经过

`gnustep-config --objc-flags`

作为 gcc 的参数。帖子有

gcc `gnustep-config --objc-flags` hello.m -o hello -I /usr/include/GNUstep/ -L /usr/lib/GNUstep/ -lgnustep-base

在最后。现在,在我的机器上,gnustep-config --objc-flags扩展到

-MMD -MP -DGNUSTEP -DGNUSTEP_BASE_LIBRARY=1 -DGNU_GUI_LIBRARY=1 -DGNU_RUNTIME=1 -DGNUSTEP_BASE_LIBRARY=1 -D_REENTRANT -fPIC -Wall -DGSWARN -DGSDIAGNOSE -Wno-import -g -O2 -fno-strict-aliasing -fexceptions -fobjc-exceptions -D_NATIVE_OBJC_EXCEPTIONS -fgnu-runtime -fconstant-string-class=NSConstantString -I. -I/home/faheem/GNUstep/Library/Headers -I/usr/local/include/GNUstep -I/usr/include/GNUstep

约扎。请注意,这个标志列表确实包含-D_NATIVE_OBJC_EXCEPTIONS,以及许多其他内容。作为记录,我的机器正在运行 Debian Squeeze。这可能是 Debian/Ubuntu 特有的问题。我不确定。

于 2013-02-03T19:12:23.500 回答
0

最好写一个 GNUmakefile。

http://www.gnustep.it/nicola/Tutorials/WritingMakefiles/index.html

最好停止使用 gcc 并切换到 clang。

于 2013-02-04T23:20:49.440 回答