views:

1096

answers:

3

Is it possible to build objective c code correctly with GCC under cygwin.

I have the following application that should work on a Mac environment, but can't get the most basic stuff to work with gcc. Are there more libraries I need to have.

#import "HelloWorldApp.h"

int main(int argc, char *argv[]) {
    return 0;
} // End of the // 

@interface Car
{
    int test;
}

//The registration is a read-only field, set by copy
@property int (readonly, copy) test;

//the driver is a weak reference (no retain), and can be modified
//@property Person* (assign) driver;

@end


CC=gcc
CXX=gcc-g++
LD=$(CC)

CFLAGS=

LDFLAGS=-lobjc

all: HelloWorld

HelloWorld: HelloWorld.o
    $(LD) $(LDFLAGS) -o $@ $^

%.o: %.m
    $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@

clean:
    rm -rvf *.o HelloWorld HelloWorld.exe

Error: gcc -c HelloWorld.m -o HelloWorld.o In file included from HelloWorld.m:6: HelloWorldApp.h:19: error: stray '@' in program HelloWorldApp.h:19: error: parse error before "int" make: *** [HelloWorld.o] Error 1

A: 

Have you tried running the gcc commands directly from command line rather than via makefile?

Found this reference:

Taken directly from http://www.cs.indiana.edu/classes/c304/ObjCompile.html

Compiling Objective-C

Objective-C code can be compiled using the GNU C compiler gcc. Objective-C interface files usually marked by the extension .h as in List.h. Implementation files are usually marked by the extension .m as in List.m. The implementation file should #import its corresponding interface file. Your main program should also #import the interface files of all of the classes it uses. Usually, you will want to compile the files which implement your classes separately. For instance, to compile List.m, use the following command:

gcc -c -Wno-import List.m

The -c switch tells the compiler to produce an object file, List.o, which can then later be linked into your program. Do this for each of your implementation files, and your main program.

The -Wno-import switch tells the compiler not to give a warning if you have a #import statement in your code. For some reason, Richard Stallman (the GNU Project founder) doesn't like the #import construction.

When you are ready to compile your program, you can link all of the implementations of your classes using gcc again. For example, to compile the files List.o, and main.o you could use the following command:

gcc -o prog -Wno-import List.o main.o -lobjc

The -o prog tells gcc to create an executable program with the name prog (if you do not specify this, it will default to a.out.

Note that when linking Objective-C with gcc, you need to specify the Objective-C library by using the -lobjc switch. If gcc reports an error, contact your system administrator to make sure that the Objective-C library and header files (objc/Object.h) were installed when gcc was built.

Jesse
I'd like to add that you should pay attention to the gcc version. I had to run gcc version 4 (cygwin had installed 3 as my main gcc version) to get objective c to compile.
Malaxeur
I googled that too and that is what is in my Makefile.But, the code still doesn't compile. I am guessing because objective-c is not the true objective.c
Berlin Brown
+6  A: 

I think it's because you're using Objective-C 2.0, which is a private extension that Apple developed and that they did not contribute back to the "standard", FSF GCC. Thus, your mingw compiler (which is not based on Apple's, but on the FSF's) does not understand new syntax like properties.

FX
There is no standard for Objective-C. There is basically GNU's version and Apple's version (and perhaps a few other rarely used ones). GNU's version is getting outdated and source is riddled with "this is a hack" comments.
dreamlax
+1  A: 

@ Malaxeur - you hit the nail right on the head. I've just been bashing about with ObjC on a PC and had that exact problem - my path was pointing to a v3.x gcc compiler. I switched to the 4.x version and voila!

It's also worth noting that the "stray '@'" error also pops up if you spell 'implementation' wrong, which is a little misleading.

Anyway, thanks for the help.

NewToObjC