Re: Cocoa Newbie Question.



On Jun 11, 5:14 pm, Reinder Verlinde <rein...@xxxxxxxxxxxxxxxx> wrote:
In article
<fe79dd66-0263-429a-b443-a7a28ef08...@xxxxxxxxxxxxxxxxxxxxxxxxxxx>,



 piscesboy <oraclmas...@xxxxxxxxx> wrote:
On Jun 11, 1:51 pm, piscesboy <oraclmas...@xxxxxxxxx> wrote:
This is my first time compiling objective C and C files together for
one program. All of my files are C files except the main file which is
in objective C called main.m

Here is the code for my boilerplate main implementation file file:

#import <Cocoa/Cocoa.h>
#include "defs.h"
#include "data.h"
#include "treenode.h"
#include "fileinout.h"

int main(int argc, const char* argv[]){

        return 0;

}

All the other .h files in my #includes are C files containing my
functions.

Here is the error I get when I compile:

gcc -Wno-import -ansi -pedantic -Wall -O2 -c treenode.c
gcc -Wno-import -ansi -pedantic -Wall -O2 -c main.m

This is not the cause of your error, but -Wno-import very, very likely
is not something that you will want to compile .m files with. At the
least, that would get rid of the warning

main.m:1:2: warning: #import is a GCC extension

I also guess that you are not using Xcode. Reason is that, on my system
(10.5.3), Xcode compiles a simple main.m as

CompileC /[...]/main.o /[...]/main.m normal ppc objective-c
com.apple.compilers.gcc.4_0

which eventually calls gcc passing a few zillion flags, including

    -fpascal-strings
    -x objective-c
    -arch i386

If you are not using Xcode, start doing that, if only to check what
command lines it produces.

In file included from /System/Library/Frameworks/AppKit.framework/
Headers/AppKit.h:53,
                 from /System/Library/Frameworks/Cocoa.framework/
Headers/Cocoa.h:13,
                 from main.m:1:
/System/Library/Frameworks/AppKit.framework/Headers/NSEvent.h:72:
error: syntax error before 'unsigned'
make: *** [main.o] Error 1

This is a fairly cryptic error for me but it appears to be associated
with the file #import 'ed from the Cocoa framework. What is going on
here?

I am an absolute beginner in using Objective C and I just want to
learn to get this to compile for now.

I guess that you are not telling the compiler that it is compiling
Objective-C code. Hence, it interprets Cocoa/Cocoa.h as a C header,
which in turn leads to your error message because some line in that
header is not legal C. Producing good error messages for C code, and
even more so for C++ code, in incredibly hard, and often impossible. The
actual reason for the error could be any earlier line.

New development: I tracked down and looked at the file NSEvent.h and
looked at line 72 and found this:

static inline unsigned int NSEventMaskFromType(NSEventType type)
{ return (1 << type); }

gcc says there is a syntax error before "unsigned".

See my remark about producing good error messages for C/C++.

This appears to be the offending line of code for when I include
anything else it compiles fine. My worry now is that NSEvent.h is
busted and as I am a newbie I don't want to touch the code. I just
want to get it to work.

I never touched NSEvent.h in my life. What is going on here?

On my system (10.5.3, with XCode 3.0), that line reads:

NS_INLINE NSUInteger NSEventMaskFromType(NSEventType type) { return (1
<< type); }

I guess your "tracking down" included preprocessing the code. If so,
this is nothing to worry about. If not, can you check the actual file?

If this does not help, start compiling a main.m that only contains
"import <Cocoa/Cocoa.h>". If that succeeds, the problem likely is with
some of your own files or with the commands you issue to compile your
code.

Reinder

Thanks for the help.

I tried something different albeit much simpler. This might clear
things up. Here is the makefile I am using to compile this newer, much
simpler program of mine that mixes Objective-C and regular C code:

TARGET=test
DEBUG=debugtest
GCC=gcc -ansi -pedantic -Wall -O2 -o
GCCOP=gcc -ansi -pedantic -Wall -O2 -c
GCCMOP=gcc -x objective-c -framework Foundation -c
OBJECTS=main.o Person.o treenode.o
SOURCE=main.m Person.m treenode.c
TRASH=*.o *.c~ *.h~ $(TARGET) $(DEBUG) Makefile~

$(TARGET): $(OBJECTS) $(SOURCE)
$(GCC) $(TARGET) $(OBJECTS)

main.o: main.m defs.h
$(GCCMOP) main.m

Person.o: Person.m Person.h
$(GCCMOP) Person.m

treenode.o: treenode.c treenode.h
$(GCCMOP) treenode.c

clean:
rm -f $(TRASH)

My target is called test and depends on the objective-C files main.m,
and Person.m and the regular C file treenode.c
This is the error produced when I type 'make':

gcc -x objective-c -framework Foundation -c main.m
powerpc-apple-darwin8-gcc-4.0.1: -framework: linker input file unused
because linking not done
powerpc-apple-darwin8-gcc-4.0.1: Foundation: linker input file unused
because linking not done
gcc -x objective-c -framework Foundation -c Person.m
powerpc-apple-darwin8-gcc-4.0.1: -framework: linker input file unused
because linking not done
powerpc-apple-darwin8-gcc-4.0.1: Foundation: linker input file unused
because linking not done
gcc -x objective-c -framework Foundation -c treenode.c
powerpc-apple-darwin8-gcc-4.0.1: -framework: linker input file unused
because linking not done
powerpc-apple-darwin8-gcc-4.0.1: Foundation: linker input file unused
because linking not done
gcc -ansi -pedantic -Wall -O2 -o test main.o Person.o treenode.o
/usr/bin/ld: Undefined symbols:
.objc_class_name_NSConstantString
.objc_class_name_NSObject
.objc_class_name_NSString
__NSConstantStringClassReference
_objc_msgSend
_objc_msgSendSuper
collect2: ld returned 1 exit status
make: *** [test] Error 1


I think I know what is going on: that I am not linking the generated
object files properly. I do not know how to properly invoke the linker
to link the generated objective-c files mixed with C files.

Could you help with that?

Thanks.





.



Relevant Pages

  • Re: Cocoa Newbie Question.
    ... is not something that you will want to compile .m files with. ... I also guess that you are not using Xcode. ... Objective-C code. ... See my remark about producing good error messages for C/C++. ...
    (comp.sys.mac.programmer.help)
  • Re: Managing the JIT
    ... decide what to jit, as in do you just jit function by function, or a ... in the case of my assembler and linker, I am currently using COFF between ... when linking code into, it allocates ... I usually compile code a "module" at a time. ...
    (comp.compilers)
  • Re: Links2 in graphics mode in X?
    ... the linking process with runtime shared libraries, ... so you need to go get libpng.a and libpng.so (depending ... de-modernizing C++ codes, or de-ansi99ing C codes. ... impossible to compile without linking against something, ...
    (comp.os.linux.misc)
  • Re: Cocoa Newbie Question.
    ... simpler program of mine that mixes Objective-C and regular C code: ... My target is called test and depends on the objective-C files main.m, ... because linking not done ...
    (comp.sys.mac.programmer.help)
  • Re: REPOST: extremely long link times
    ... and it's linking that's the problem - compiling's generally very ... somebodies old code that had ALOT of this stuff, and to compile it took ... only 1 OBJ will be recreated. ... This does leave me with a lot of Test projects all over the place, ...
    (microsoft.public.dotnet.languages.vc)