Objective-C on Linux Compile Error
Asked Answered
S

4

10

There seem to be quite a few tutorials on how to do this, each slightly different. I'm hoping someone can recognize the error messages I'm getting and point me in the right direction.

My code, h.m is:

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

Before I compile, I enter in the console:

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

I try to compile with:

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

and get:

/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

Can somebody point me in the right direction?

TIA

Sculptress answered 11/7, 2012 at 3:29 Comment(1)
Why don't you build with gnustep-make using GNUmakefile? Here's how to write one gnustep.it/nicola/Tutorials/WritingMakefiles/index.htmlPeril
C
36

The reason for the linking error is most likely due to the behaviour of linker to link the libraries only after seeing the symbols in compilation prior to linking the library. As h.m appears after -lgnustep-base the library is not linked as the symbols in library are not yet encountered. You can either instruct the linker to link the libraries even if the symbols are not encountered using -Wl,--no-as-needed linker option as

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

Or better yet just move the source to the beginning of the compilation command as

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

The observed linker behaviour is to discourage looking up & linking symbols of library which may not be needed but are linked in the compilation anyway, thus the second option would be recommended way of doing rather than added the -Wl, --no-as-needed linker option.
Hope this helps!

Colophon answered 11/7, 2012 at 4:20 Comment(0)
T
12

You'll need to link to libobjc. The fix is pretty simple; just compile with:

gcc h.m `gnustep-config --objc-flags` -lobjc -lgnustep-base -o hello
Tia answered 11/7, 2012 at 3:42 Comment(1)
Thanks Andy. Using your commandline, I still get the same exact error.Sculptress
A
5

You need to also specify the link flags:

gcc h.m `gnustep-config --objc-flags` `gnustep-config --objc-libs` \
-lobjc -lgnustep-base -o hello
Antivenin answered 23/5, 2013 at 5:14 Comment(0)
H
2

This method seem good:

gcc h.m  `gnustep-config --objc-flags` -lgnustep-base -o hello
Hair answered 3/3, 2013 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.