#import <stdio.h>
int main()
{
printf("hello world\n");
return 0;
}
p.m:1:2: warning: #import is obsolete, use an #ifndef wrapper in the header
file
I thought import was to be used as in java.
Bill
What command are you using to compile this file? Does the file have a
.m extension?
S.
> What command are you using to compile this file? Does the file have a
> .m extension?
Yes it had an .m extension but I just used gcc p.m -o a to create a file
called "a" also an a.out was created. No special gcc switches.
Bill
You thought wrong.
Notably, the problem comes from symbolic and hard links, not
mentionning mere copies, which prevent (or at least would make it much
harder) to unify the imported files.
--
__Pascal Bourguignon__
Weird. I think that should simply work. It does for me actually. With
GCC 4.2.1 or 4.0 on Snow Leopard I don't get that issue.
Import is simply a more fancy include that makes sure that you don't
include files multiple times automatically without the need of the
standard #ifndef FOO_H #define FOO_H stuff.
What gcc version are you using? Is this actually on OS X?
S.
It's on linux. What I have is gcc-3.2.x version. I also have 3.4.6
compiled but I haven't installed it yet.
Bill
> Weird. I think that should simply work. It does for me actually. With
> GCC 4.2.1 or 4.0 on Snow Leopard I don't get that issue.
Some older versions of GCC warn about #import by default. You can
disable the warning with -Wno-import.
sherm--
> Some older versions of GCC warn about #import by default. You can
> disable the warning with -Wno-import.
The code did compile and ran perfectly. So a warning was evidently all I
received. The assembler created an a.out too though which was kind of
strange.
Bill
Objective-C predates java by a long shot.
The #import pre-processor directive was designed as a housekeeping
measure
to ensure header files were not included multiple times in a class/
subclass hierarchy.
This could have caused compiler errors (ie my_func redefined in
blah.h ..from blah.h ..)
What C programmers do is a quick check at the top of the file:
Say the file is stdio.h.
at the beginning of the text file:
#ifndef _STDIO_H_
#define _STDIO_H_
at the end:
#endif //_STDIO_H_
this makes the pre-processor to the compiler skip including stdio.h if
it is already included previously ( it will be defined in the pre-
processor environment by the #define directive).
The Objective-C #import command was designed as a "smart" #include
directive so as not to include a header twice in a compilation cycle.
However, some C programmers (Richard!) objected for their own reasons.
so gcc whines and complains unless you you turn that warning off: -Wno-
import
So you can use #import "header.h" as many times in a set of source
files as you want but header.h will only be added to the compilation
environment once. OR you can use the C convention
#ifndef X /#define X/ #endif //X .
The standard C library and gnu objc headers are built this way.
So here's my recommendation that avoids errors and confusion:
#include <stdio.h>
#include <objc/Object.h>
#include <someClibraryHeader.h>
#import "MyClass.H"
#import "MyOtherClass.h"
...
when compiling:
gcc -gc -Wno-import MyClass.m -o MyClass.o
gcc -g -Wno-import Main.m -o Myapp -lobjc ( -lpthread -thread)
(pthread or thread libs may be needed if you get undefined errors
unless they are put in by gcc automatically these days)
This where make and Makefiles are your friend:
in a file named Makefile:
-----------------------------------------------------
#!/usr/bin/make
#pleas see "man make"
# compilation options
CC=gcc
CFLAGS= -g -W no-import -O2
LIBS= -lobjc -lm
#LIBS= -lobjc -lpthread -lm
#LIBS= -lobjc -thread -lm
#
all: main.o class1.o class2.o
$(CC) $(CFLAGS) main.o class1.o calss2.o -o MyApp $(LIBS)
#
main.o: main.m class1.o class2.o
$(CC) $(CFLAGS)-c main.o
#
Class1.o: Class1.h Class1.m
$(CC) $(CFLAGS) -c class1.o
#
Class2.o: Class2.h Class2.m
$(CC) $(CFLAGS) -c class2.o
#
clean:
rm -f *.o
#end
-------------------------------------------------
then type make at the command line and things happen.
I hope this helps more than hurts a noobie!