26 January 2008

Compile Objective-C Programs Using gcc

Probably 99% of all Objective-C programmers out there are compiling their programs with XCode very happily. However that doesn't stop the other 1%, who are brave enough or simply don't have choice, from compiling Objective-C programs using gcc under command line.

Being one of that 1%, I had hard time trying compiling my program. I finally had my first program compiled after days of struggling due to lack of information and experience. I'd like to share the lesson I learned so that maybe someone sees this and save a little effort. The process of compiling a very simple objective-c program on several platforms are explained below.

Here is the objective-c program file I used - hello.m (download).
/***************** hello.m *********************/
#import <Foundation/Foundation.h> @interface HelloWorld : NSObject - (void) hello; @end @implementation HelloWorld - (void) hello { NSLog(@"hello world!"); } @end int main(void) { HelloWorld *hw = [[HelloWorld alloc] init]; [hw hello]; [hw release]; } /******************* end ***********************/

1. To Compile Objective-C Programs on Mac OS X

Compiling on Mac OS X is the simplest, just cd to the directory where hello.m resides and type in the following command.
$ gcc -o hello hello.m -framework Foundation

The compile goes happily and produces an executable file "hello".
$ ./hello
2008-01-26 23:10:32.983 hello[381:10b] hello world!

2. To Compile Objective-C Programs on Linux
Well, to compile Objective-C programs, GNUstep needs to be installed. Please visit www.gnustep.org to find out how to install GNUstep on your Linux version; or use "apt-get" or "synaptic" if you are running Debian-based distributions (Ubuntu for example).
Once GNUstep is installed, assuming its installation directory is /usr/lib/GNUstep, the following command compiles hello.m.

gcc -o hello hello.m \
-I `gnustep-config --variable=GNUSTEP_SYSTEM_HEADERS` \
-L `gnustep-config --variable=GNUSTEP_SYSTEM_LIBRARIES` \
-lgnustep-base -fconstant-string-class=NSConstantString \
-D_NATIVE_OBJC_EXCEPTIONS


$ ./hello
2008-01-27 00:02:17.321 hello[7067] hello world!

The compiler may complain about NXConstantString not declared if the last switch "-fconstant-string-class=NSConstantString" is not included.
This process was tested on Ubuntu 7.10 and should work for other linux distributions as well.

Thanks to commenter digreamon, I updated the above command with -I `gnustep-config --variable=GNUSTEP_SYSTEM_HEADERS`  and -L `gnustep-config --variable=GNUSTEP_SYSTEM_LIBRARIES`. It saves you the trouble to manually locate the locations of headers and libraries. I have not verified this on Windows, but they might work too.

Also note that if you did not include -D_NATIVE_OBJC_EXCEPTIONS, you may run into the following error:

/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.

3. To Compile Objective-C Programs on Windows
This is the most tricky one. In order to compile, GNUstep for Windows needs to be installed first. Point your browser to http://www.gnustep.org/experience/Windows.html and download the three necessary packages: GNUstep MSYS System, GNUstep Core and GNUStep Devel. Install them in the order as they were mentioned.

Once everything is setup, use the following command to compile your hello.m program. The compiler emits some linking information and that means compilation is successful.
$ gcc -o hello hello.m -I /GNUstep/System/Library/Headers \
-L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base


Info: resolving ___objc_class_name_NXConstantString by linking to __imp____objc_class_name_NXConstantString (auto-import)
Info: resolving ___objc_class_name_NSObject by linking to __imp____objc_class_name_NSObject (auto-import)

$ ./hello
2008-01-27 00:27:14.630 hello[3724] hello world!

Be very careful about the -lobjc and -lgnustep-base switch, they must appear after file name "hello.m", otherwise linking will fail.
I tried to compile with the following command, and very weird linking errors came up:

$ gcc -o hello -I /GNUstep/System/Library/Headers \
-L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base hello.m

C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0xe): undefined reference to `NSLog'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0x47): undefined reference to `objc_get_class'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0x59): undefined reference to `objc_msg_lookup'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0x78): undefined reference to `objc_msg_lookup'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0x9b): undefined reference to `objc_msg_lookup'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0xbb): undefined reference to `objc_msg_lookup'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.text+0xdf): undefined reference to `__objc_exec_class'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.data+0xfc): undefined reference to `__objc_class_name_NXConstantString'
C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/ccAFbaaa.o:hello.m:(.data+0x104): undefined reference to `__objc_class_name_NSObject'
collect2: ld returned 1 exit status
make: *** [build] Error 1
It took me a few days to realized that the switches need to be following hello.m. I'm still not able to understand the reason behind, though. If anybody knows, please be generous enough to put down a comment.

UPDATE
According to commenter "teo", people who still encounter errors with Windows+GNUStep may use the following command. I have not personally verified this, but I assume it does help according to some other commenters feedback.
gcc `gnustep-config --objc-flags` -o hello2 hello2.m \
-L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base
Note that the ` symbol is not the same as a single quote ( ' ).



Now, happy compiling your first Objective-C program on whichever environment you are in.

97 comments:

MisterCaustic said...

I'm using GNUstep on Ubuntu too. I have a tip: use makefiles! To compile your commandline app, just include a simple textfile called GNUmakefile with the following contents:

include $(GNUSTEP_MAKEFILES)/common.make
TOOL_NAME = HelloHello
HelloHello_OBJC_FILES = hello.m
include $(GNUSTEP_MAKEFILES)/tool.make

Then just type `make` at the command line. More info is at this guide: Writing GNUstep Makefiles.

LYx said...

that's a great tip. I will sure use that next time I compile :-D Thanks a lot!
However including everything in a command shows a better picture on how the file is compiled, specially under windows. Just to clear some doubts.

marcio said...

where is the hello.m file saved to?

LYx said...

The content of 'hello.m' can be found at the beginning of the article. A download link is also provided. It should be saved to your working folder, save '~/hello/'. And you need to do 'cd ~/hello' before start working. cheers.

For U Only said...

I want to compile Objective-C program on windows that should run on Mac OS. How can I do that?
thanks

lyxite said...

I'm afraid that is not possible, at least to my knowledge.

Serge said...

Thanks! I'm waiting for mid-June Mac Mini line refresh, so that I can buy a used Intel Mac Mini for a reasonable price. Thanks to you I'm not wasting the time. I'm learning objective C on my Windows box.

lyxite said...

You are most welcome. I'm glad that I helped.

d11wtq said...

Just a quick note, on OS X you can compile with an even shorted command:

gcc -o hello hello.m -framework Foundation

(i.e drop the -L /path/to/Foundation and just use -framework Foundation)

lyxite said...

thx for the tip!
:-)

nekin said...

gr8 job .. really help full thanks a lot :)

ഷണ്ഡന്‍ said...

Hi lyx,

i used your directions to run objective-c on my xp machine.

but the result i got was

Owner@MAX11 ~/hello
$ gcc -o hello hello.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base
hello.m: In function `-[HelloWorld hello]':
hello.m:10: error: cannot find interface declaration for `NXConstantString'

what could be the probable cause ?

thanks
sunil

lyxite said...

Since this is a compilation error, you probably want to start trouble shooting by looking at if your header file is properly included. Check whether GNUStep is properly installed to the directory I assume in my blog post.
Make sure the command typed in your command line is exactly the same as the one I provided and see if the same error comes up.

ഷണ്ഡന്‍ said...

hi lyxite,

As you can see (from the commandline-log i pasted in the last post), i tested with the same commands as you mentioned. Also, the include files are there in the specified include path. I am using your hello.m file for testing.

thanks

bala said...

$ gcc -o hello hello.m -I/GNUstep/System/Library/Headers\
> -L/GNUstep/System/Library/Libraries -lobjc -lgnustep-base Info:resolving ___objc_class_name_N
XconstantString by linking to __imp___objc_class_name_NXConstantString (auto-import) Info:resol
ving ___objc_class_name_NSObject by linking to __imp___objc_class_name_NSObject (auto-import)

i used in the above code in my MINGW32 editor but it give the error as

sh: syntax error near unexpected token `(a'

i need help .pls

teo said...

for GNUStep on Windows, you need to include `gnustep-config --objc-flags` in the command line:

gcc `gnustep-config --objc-flags` -o hello2 hello2.m -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base

Hemen said...

I'm using GNUstep on Windows and i have also followed all the step of installation and run the file
from Command :::
$ gcc -o main main.m -I /GNUstep/System/Library/Headers \
-L /GNUstep/System/Library/Libraries/ -lobjc -lgnustep-base

But it generates the error :::
gcc.exe: -L: No such file or directory

So please help me for this

Dan said...

Hi Hemen,

Remove that backslash between your '-I /GNUstep/System/Library/Headers' and '-L /GNUstep/System/Library/Libraries' and then it should work. I think it was a typo.

lyxite - thanks so much for this post, I've been trying for days to compile, and had no idea about including these four switches.

lyxite said...

the back slash is used to separate command into different lines. if you type the entire command in the same line, they are not needed.

Marius said...

Hi LYx,
I have followed your instructions in order to compile your program on my XP machine.
Still, I am receiving the following error:
hello.m:10: error: cannot find interface declaration for `NXConstantString'
All I can say is that after installing the GNUstep I ended up with two nested GNUstep folders.
I have choosen the defautl instalation and for both packages (system and core) I have accepted the default installation path, which is: c:\GNUstep.
Do you have any ideea of what could be wrong?
Thank you very much in advance,
Marius

lyxite said...

you may try this switch for luck.
-fconstant-string-class=NSConstantString

LVS said...

that works perfectly! many thanks!
however, is there some way i can avoid doing it everytime i compile a program?

lyxite said...

'make' is the way.

Arok said...

Hi lyx,

i used your directions to run objective-c on my ubuntu 8.10.

but the result i got was

gcc -o source source.m -I /usr/lib/GNUstep/System/Library/Headers \-L /usr/lib/GNUstep/System/Library/Libraries/ -lgnustep-base \-fconstant-string-class=NSConstantString
source.m:2:34: error: Foundation/Foundation.h: No such file or directory
source.m:5: error: cannot find interface declaration for ‘NSObject’, superclass of ‘HelloWorld’
source.m: In function ‘-[HelloWorld hello]’:
source.m:10: error: cannot find interface declaration for ‘NSConstantString’
source.m: In function ‘main’:
source.m:15: warning: ‘HelloWorld’ may not respond to ‘+alloc’
source.m:15: warning: (Messages without a matching method signature
source.m:15: warning: will be assumed to return ‘id’ and accept
source.m:15: warning: ‘...’ as arguments.)

what could be the probable cause ?

thanks
tegez

lyxite said...

check out the update in the original post, highlighted in red.

Arok said...

that works perfectly! many thanks! my brother...:D

best regardsss

tegez

MikeL said...

Great post on an obscure topic. I had to go by what teo showed above:

gcc `gnustep-config --objc-flags` -o hello2 hello2.m -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base

This works for me on Windows XP. Make sure that you have the right directories for the library as was also stated. For me I ultimately used:

gcc `gnustep-config --objc-flags` -o hello hello.m -L C:/GNUstep/GNUstep/System/Library/Libraries -lobjc -lgnustep-base

and it worked.

Cesar said...

works smoothly thanks. used the -fsconstant you recommended. i'm looking forward to getting started :)

Unati said...

Thanks a ton! I had been trying to compile my program on Windows XP for so long and was getting linking errors... Your tips solved the problem immediately!

T.J. said...

This command worked for me:
gcc -o hello hello.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString

Kiran Vemula said...

I am getting below while compiling an example

$ gcc -o MyDelegate MyDelegate.m -I C:/GNUstep/GNUstep/System/Library/Headers -
L C:/GNUstep/GNUstep/System/Library/Libraries -lobjc -lgnustep-base
Info: resolving ___objc_class_name_NSObject by linking to __imp____objc_class_na
me_NSObject (auto-import)
c:\gnustep\mingw\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: wa
rning: auto-importing has been activated without --enable-auto-import specified
on the command line.
This should work unless it involves constant data structures referencing symbols
from auto-imported DLLs.C:/DOCUME~1/user/LOCALS~1/Temp/ccJzWOHC.o:MyDelegate.m:
(.data+0x414): undefined reference to `___objc_class_name_NSApplication'
collect2: ld returned 1 exit status

hyderali said...

I am trying to build a make file GNUStep for windows every time I run this gives Foundation: no such file or directory found.
Can any one help...!

lyxite said...

make sure you have windows version of gnustep properly installed. see more details here
http://www.gnustep.org/experience/Windows.html

EQ said...

Fantastic blog, thanks a lot, I was really getting desperate, unable to compile a relatively simple program under WinXP. In the end this command worked for me:

gcc `gnustep-config --objc-flags` -o prog prog.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base

EQ said...

Better yet for WinXP, because there are no resolve messages and compile warnings:

gcc `gnustep-config --objc-flags` -o prog prog.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -enable-auto-import

Dave said...

Thanks SO much.

Sakhawat said...

Pretty useful post. That saved me a lot... Thanks!

tasnia said...

Hi,
I am using GNUSTEP on windows XP.
I followed the copilation instructions. But I faced some problem.
Here is the code:

#import
#import
@interface SumClass:NSObject{
int firstNumber;
int secondNumber;
int result;
}
-(void)sumNumber:(int)x:(int)y;
-(int)getSum;
-(void)printResult;
@end

#import"SumClass.h"
#import
@implementation SumClass
-(void)sumNumber:(int)x:(int)y
{
firstNumber = x;
secondNumber = y;
result = firstNumber + secondNumber;
}
-(int)getSum
{
return result;
}
-(void)printResult
{
printf("%i",result);
}
@end

#import
#import"SumClass.h"
int main(int argc, const char *argv[])
{
SumClass *sumobj = [[SumClass alloc] init];

//set the values
[sumobj sumNumber:1:2];
[sumobj getsum];
[sumobj printResult];
[sumobj release];
return 0;
}


There are 3 errors:

error1: Foundation.h: No such file or directory
error2: NSObject: No such file or directory
error3: cannot find interface declaration for `NSObject', superclass of `Hello'

Can anyone give me the solution plz???

lyxite said...

Looks like u did have the -I argument properly set.
Try find where the header files (for Foundation) are and then place that folder path after -I

sindhushiva said...

very useful post. thanks

Piyush said...

C:/DOCUME~1/hb52/LOCALS~1 Temp/ccqNwxZc.o:main.m:(.data+0x74):undefined reference to `___objc_class_name_Fraction'

C:/DOCUME~1/hb52/LOCALS~1 Temp/ccRktthc.o:FractionB.m:.data+0xc4): undefined reference to `___objc_class_name_Fraction'collect2: ld returned 1 exit status

Ansgar Kröger said...

Thank you very much for this article. Helped a lot!!!

Samar Mahajan said...

I'm using GNUstep on Windows and i have also followed all the step of installation and run the file
from Command :::
$ gcc -o main main.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries/ -lobjc -lgnustep-base

But it generates the warning :::
gcc.exe: -L: No such file or directory

and my Libraries contains gnustep-base but not objc

So please help me for this

lyxite said...

new versions of GNUStep might take difference directories as default installation destination. Please try to manually locate those files and put that directory after -L accordingly.

Tyler said...

Hello there. I have a question regarding the GNUstep runtime. Is it absolutely required that the end user have this runtime installed? Or is there some way I can include the needed libraries at compile time? Along the same lines does Obj-c compile to machine code or is it an interpreted language?

Thanks!

SilverCord said...

To those who face
error: cannot find interface declaration for `NXConstantString' you might need this switch "-fconstant-string-class=NSConstantString"

$ gcc -o hello hello.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Li
brary/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString

vasanth said...

Thanks man. Realy Very helpful

micco said...

can anyone here show me how to compile using makefile in windows?

micco said...

can anyone here show me how to compile using makefile in windows?

nmanela said...

Hi, this is a very helpful post. I tried to compile hello.m and got this error: cc1obj.exe error: to generate dependencies you must specify either -M or -MM. I tried many configuration to see that the command line was including the headers and libraries folders but always got the same error. Thanks in advance, Noga

David Burton said...

Using make fails because the GNUStep installer for Windows misses something. GNUSTEP_MAKEFILES is not defined in the shell that it opens.

After banging my head against a wall for a few nights, I figured this out and added an environment variable to Windows for GNUSTEP_MAKEFILES with a value of /GNUstep/System/Library/Makefiles and after adding this and restarting the GNUStep shell the tutorial makefile I was trying to use worked.

mountaineer said...

Thank you very much . Your this post is very helpfull

mit said...
This comment has been removed by the author.
mit said...

Hi, I'm wondering if anyone can help me. Basically I've tried every way given to compile this but I get loads of errors such as: -
hello.m:2:34: error: Foundation/Foundation.h: No such file or directory
hello.m: In function '-[HelloWorld hello]':

and so on and so on... I've put the file on my desktop.

lyxite said...

it will help if you could state more clearly about your
- platform
- library installation directory
- environment setup

mit said...

Hi, thanks for getting back to me but I had another good read through the previous comments and I eventually managed a compile. I used one of your earlier solutions "-fconstant-string-class=NSConstantString" and that sorted everything out. Thanks for the help, good tutorial!

fate said...

I followed the example of"Programming in Objective-C," Copyright © 2004 by Sams Publishing.

Fraction.h
#import
@interface Fraction: NSObject {
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) d;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end


#import "Fraction.h"
#import
@implementation Fraction
-(void) print {
printf( "%i/%i", numerator, denominator );
}
-(void) setNumerator: (int) n {
numerator = n;
}
-(void) setDenominator: (int) d {
denominator = d;
}
-(int) denominator {
return denominator;
}
-(int) numerator {
return numerator;
}
@end

main.m
#import
#import "Fraction.h"
int main( int argc, const char *argv[] ) {
// create a new instance
Fraction *frac = [[Fraction alloc] init];
// set the values
[frac setNumerator: 1];
[frac setDenominator: 3];
// print it
printf( "The fraction is: " );
[frac print];
printf( "\n" );
// free memory
[frac release];
return 0;
}


when i ran gcc.sh main,there was some messages:

$ gcc.sh main
In file included from main.m:1:
Fraction.h:11:5: warning: no newline at end of file
main.m:24:2: warning: no newline at end of file
C:/DOCUME~1/muiy/LOCALS~1/Temp/ccqF8vKH.o: In function `main':
C:/GNUstep/home/muiy/Fraction/main.m:6: undefined reference to `___objc_class_na
me_Fraction'
collect2: ld returned 1 exit status

I don't know why,please help me.since i cant't access you blog directly,so mail me:
qlscbbs@gmail.com

Prashanth said...

try gcc.sh *.m instead of
gcc.sh main. I guess your Fraction.m is not getting compiled.

rabia said...

i am using GNUstep on Windows 7.can u tell me where to save hello.m file .
cz when i write

gcc `gnustep-config --objc-flags` -o hello hello.m -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base

it says:

gcc.exe :hello.m : no such file or directory

arv said...

very nice article "lyxite"...and "teo" very much thank to both of you..

i was panic with windows and gnustep compile problem..
its will be helpful if u tell me is there any way to set flags for all time..

thank you.

amialooser? said...

thanks a lot man it worked finally

is there anyways i can get past without writing the whole complile commad

PS:- i tried the make file but it says
no make file found

lyxite said...

the all purpose of this post is to walk you through the compile commands. If you want to skip this, you may try google for GNUSTEP_MAKEFILES as mentioned in earlier comments.

Drew said...

Thank you for this very helpful article! I am working in Windows and I can get single files to compile correctly. However, I'm working through the same example as the user "fate" (using Fraction.h, Fraction.m, and FractionTest.m [the same as fate's main.m]). I have those three files, and I'm not sure how to compile them correctly. I have tried this statement:
gcc `gnustep-config --objc-flags` -o FractionTest Fraction.m FractionTest.m -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base

Any help would be greatly appreciated!

Drew said...
This comment has been removed by the author.
Drew said...

I take back what I said, I had a spelling mistake (innit instead of init) in my file and now everything works!!

TAPAN said...

thanks Lyx and Teo.... it works fine now on windows XP


...now even i am in the 1% gang..

Thanks once again

MetalBoru said...

Tremendous stuff. Saved me hours.

Thanks!

Kr said...

Tired of getting NSConstantString error.???

I may help you..

include the foundation.h file and then compile

#import


for details..
go to http://kumargcchelp.blogspot.com/

bob42 said...

I downloaded and installed GNUstep on my Vista-64 system a week ago and successfully compiled an Objective-C program. However, I just tried to repeat that compilation and it failed. I used the following command:

gcc `gnustep-config --objc-flags` -o prog1 prog1.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base

This produced the following:

gcc: `gnustep-config: No such file or directory
cc1obj.exe: error: unrecognized command line option "-fobjc-flags`"

What am I doing wrong?

Patrick said...

I'm using your method to compile the "Hello.m" in the iMac but found below errors:

imac:hello patrick$ gcc -o hello hello.m -L /System/Library/Frameworks/Foundation.framework/Foundation

ld: -L must be immediately followed by a directory path (no space)

collect2: ld returned 1 exit status

Then I deleted the space but found more errors:

imac:hello patrick$ gcc -o hello hello.m -L/System/Library/Frameworks/Foundation.framework/Foundation
ld: warning: path '/System/Library/Frameworks/Foundation.framework/Foundation' following -L not a directory
Undefined symbols:
"__objc_empty_vtable", referenced from:
_OBJC_METACLASS_$_HelloWorld in ccqZLB7P.o
_OBJC_CLASS_$_HelloWorld in ccqZLB7P.o
"_objc_msgSend", referenced from:
_main in ccqZLB7P.o
_main in ccqZLB7P.o
(maybe you meant: l_objc_msgSend_fixup_release, l_objc_msgSend_fixup_alloc )
"_objc_msgSend_fixup", referenced from:
l_objc_msgSend_fixup_alloc in ccqZLB7P.o
l_objc_msgSend_fixup_release in ccqZLB7P.o
(maybe you meant: l_objc_msgSend_fixup_release, l_objc_msgSend_fixup_alloc )
"__objc_empty_cache", referenced from:
_OBJC_METACLASS_$_HelloWorld in ccqZLB7P.o
_OBJC_CLASS_$_HelloWorld in ccqZLB7P.o
"_OBJC_CLASS_$_NSObject", referenced from:
_OBJC_CLASS_$_HelloWorld in ccqZLB7P.o
"___CFConstantStringClassReference", referenced from:
cfstring=hello world! in ccqZLB7P.o
"_NSLog", referenced from:
-[HelloWorld hello] in ccqZLB7P.o
"_OBJC_METACLASS_$_NSObject", referenced from:
_OBJC_METACLASS_$_HelloWorld in ccqZLB7P.o
_OBJC_METACLASS_$_HelloWorld in ccqZLB7P.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

elwis said...

Nice post, however on UBuntu 10.04 the compiler still squeals about Foundation.h no matter if I put it up his face.
I guess a mac is the way to go if one's supposed to get serious about objective-c

Kiran said...

i have installed te GNU Setup for windows. When i type gcc command uts giving below error

sh: gcc: command not found

todd said...

your mac solution is wrong... the linker flag is not correct.

You want to use the -framework flag. For example,

-framework Foundation

mothertheressa said...

I saved the make file as GNUmakefile.txt and while I typed as make in GNUstep I get the error as no target specied and no make file found.
and also I get one more error as Undefined reference to __objc_class_name_Fraction error

Its a Interface and implementation process.
Pls help me what to do for this.

Tyler said...

I am trying to figure out how to use the JGrasp IDE on Windows 7, however I keep getting the same error:

pg1.m:1:34: fatal error: Foundation/Foundation.h: No such file or directory

Any suggestions? I have already tried pretty much ever suggestion on here though.

lyxite said...

i'm not familiar with JGrasp, but the error generally means you have not specified a correct header directory for the Foundation framework. please locate it and include it using gcc -I flag.

drmud said...

for objc beginner on windows. here is the working fraction compiler on my machine. thanks for all posting.
$ gcc `gnustep-config --objc-flags` -o main main.m fraction.m -I /GNUstep/Syste
m/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -e
nable-auto-import

Ruben Chokkalingam said...

Hi lyxite..
Am a beginner to Objective-C.
Am using GNUstep in windows.

When using the follwing command

$ gcc -o hello hello.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base


c:/mingw/bin/../lib.gcc/mingw32/4.5.2/../../../../mingw32/bin/ld.exe: cannot find -lgnustep-base
collect2:ld returned 1 exit status

help me out!

sym9 said...

thanks for post, red text solve my error

ab1204 said...

thanx a ton for the post.

Dewang said...

@lyxite
Your description helped a lot!
Thank You Very Much!!!!!!!!!!!

digreamon said...
This comment has been removed by the author.
digreamon said...

Hi, I'm compiling Objective-C code in Ubuntu 11.04. To make things work I had to use
gnustep-config --variable=GNUSTEP_SYSTEM_HEADERS
and
gnustep-config --variable=GNUSTEP_SYSTEM_LIBRARIES
to find out the paths to use with -I and -L options.
Also I've received "The current setting for native-objc-exceptions does not match that of gnustep-base ... please correct this." error...
Fixed it by changing
#define BASE_NATIVE_OBJC_EXCEPTIONS 1
to
#define BASE_NATIVE_OBJC_EXCEPTIONS 0
in /usr/include/GNUstep/GNUstepBase/GSConfig.h

Everything works, thanks for the nice post.

Active Observer said...

Where do I save the hello.m file? To my c:\\ drive? I am using Windows. Thanks.

DrKevG said...

Thanks for this article - it was a great help in getting started...

In case its helpful to anyone else, the following arguments worked for me:

gcc -o hello hello.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries/ -lobjc -lgnustep-base -fconstant-string-class=NSConstantString

Believe that the -lobjc step is required to link to the objective c runtime [Source: 'The definitive buid to gcc' (p34)]. I did try the suggestion in red that uses `gnustep-config --objc-flags` but got the error:

Foundation/Foundation.h: No such file or directory



Steps I followed were:
(a) Default installation of the following GNUstep packages:

GNUStep MSYS System: gnustep-msys-system-0.28.1-setup.exe

GNUstep core: gnustep-core-0.28.0-setup.exe

GNUstep devel: gnustep-devel-1.3.0-setup.exe

(b) Ran the GNUstep shell by opening a windows CMD shell and running: C:\GNUstep\msys\1.0\bin\sh.exe
(c) Within GNUstep shell changed directory to folder containing the hello.m file e.g. cd /c/mytestfolder
(d) Compiled using the command listed above

Hope this might help others starting out like me and thanks once again for the blog - would have taken alot longer without it...

DrKevG said...

Doh! :-) Managed to edit out of my post that it applies to Windows 7...

Rose said...

Hi, Great article. I falls on that 1%. :) Thanks!

uzumaki said...

hello!

if you're using GUI objects add

-lgnustep-gui

for example:(I use that script to compile)

#!/bin/sh

gcc `gnustep-config --objc-flags` -I /usr/lib/GNUstep/System/Library/Headers \
-L /usr/lib/GNUstep/System/Library/Libraries/ -lgnustep-base -lgnustep-gui \
-fconstant-string-class=NSConstantString $1.m -o $1

uzumaki said...

hello!

if you're using GUI objects add

-lgnustep-gui

for example:(I use that script to compile)

#!/bin/sh

gcc `gnustep-config --objc-flags` -I /usr/lib/GNUstep/System/Library/Headers \
-L /usr/lib/GNUstep/System/Library/Libraries/ -lgnustep-base -lgnustep-gui \
-fconstant-string-class=NSConstantString $1.m -o $1

lyli said...

DrkevG, thanks for sharing..., it works on my win2k

That guy you met around the corner said...

When I type in the command, it says "sh: gcc: command not found."

Pierre said...

Thanks!!!

AGNJ said...

The tutorial on the GNUstep web site says to use a GNUmakefile then just type make at on the command line. How can the GNUmakefile be modifed to incorporate the correct build command that works in WinXP?

Xcoder said...

i had same situation as u mentioned up there my laptop processor does not support HARDWARE VISUALIZATION that's why choose that option.
but now o think it's a pretty well utility to program in OBJECTIVE-C using Gnustep (MINGW32 compiler).

so finally thanks a lot in advance dude.

asma rezgui said...

i have followed all the steps below but i have found the following errors
:c:/gnustep/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/bin/ld.exe: cannot find -lgnustep-base
c:/gnustep/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/bin/ld.exe: cannot find -lobjc
collect2: ld returned 1 exit status

i need your help please !!

asma rezgui said...

i have followed all the steps but i found the following errors :


c:/gnustep/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/bin/ld.exe: cannot find -lgnustep-base
c:/gnustep/bin/../lib/gcc/mingw32/4.6.1/../../../../mingw32/bin/ld.exe: cannot find -lobjc
collect2: ld returned 1 exit status

i need your help please !!

Xcoder said...

minGW package is not installed in you system that's why it's happening
so first install gnustep-msys-system-0.30.0 package and then gnustep-devel-1.4.0-setup and finally install gnustep-core-0.31.0-setup package for mingw Remember must be in this sequence and use below given command i hope it might help you..
**********
gcc -o hello hello.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString



*****NOTE*****

-fconstant-string-class=NSConstantString

without this command it consider constant string objects as a class type NXConstantString.
******

have fun.