如何在ubuntu中编译Objective C代码

sub*_*ngh 5 iphone ubuntu gnustep

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

#import <Foundation/Foundation.h>

int main (void)
 {
  NSLog (@"Executing");
  return 0;
 }
Run Code Online (Sandbox Code Playgroud)

我编译了它但得到了跟随错误

   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.
Run Code Online (Sandbox Code Playgroud)

我按照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
Run Code Online (Sandbox Code Playgroud)

Fah*_*tha 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.
Run Code Online (Sandbox Code Playgroud)

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

请注意,shalki 的回答也可能解决该问题。如果那里引用的链接消失了,那么相关的博客文章《 在 Linux 上编译 Objective-C 程序》 是中文或日文或类似的语言,所以我不知道它到底在说什么,但我认为结果是经过

`gnustep-config --objc-flags`
Run Code Online (Sandbox Code Playgroud)

作为 gcc 的一个论点。该帖子有

gcc `gnustep-config --objc-flags` hello.m -o hello -I /usr/include/GNUstep/ -L /usr/lib/GNUstep/ -lgnustep-base
Run Code Online (Sandbox Code Playgroud)

在最后。现在,在我的机器上,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
Run Code Online (Sandbox Code Playgroud)

尤扎。请注意,此标志列表确实包含-D_NATIVE_OBJC_EXCEPTIONS以及许多其他内容。根据记录,我的机器正在运行 Debian squeeze。这可能是 Debian/Ubuntu 特有的问题。我不知道。