Main Page | Report this Page
Computers Forum Index  »  Computer Languages (Objective-C)  »  simple program...
Page 1 of 1    

simple program...

Author Message
Bill Cunningham...
Posted: Thu Oct 08, 2009 3:25 am
Guest
I tried this simple program and got this error. This is the first time
I've writtten anything in obj-C. Why this error?

#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
 
Stefan Arentz...
Posted: Thu Oct 08, 2009 3:31 am
Guest
"Bill Cunningham" <nospam at (no spam) nspam.invalid> writes:

Quote:
I tried this simple program and got this error. This is the first time
I've writtten anything in obj-C. Why this error?

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

What command are you using to compile this file? Does the file have a
..m extension?

S.
 
Bill Cunningham...
Posted: Thu Oct 08, 2009 3:54 am
Guest
"Stefan Arentz" <stefan at (no spam) keizer.soze.com> wrote in message
news:87eipewzkw.fsf at (no spam) keizer.soze.com...

Quote:
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
 
Pascal J. Bourguignon...
Posted: Thu Oct 08, 2009 4:23 am
Guest
"Bill Cunningham" <nospam at (no spam) nspam.invalid> writes:

Quote:
I tried this simple program and got this error. This is the first time
I've writtten anything in obj-C. Why this error?

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

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__
 
Stefan Arentz...
Posted: Thu Oct 08, 2009 4:44 am
Guest
"Bill Cunningham" <nospam at (no spam) nspam.invalid> writes:

Quote:
"Stefan Arentz" <stefan at (no spam) keizer.soze.com> wrote in message
news:87eipewzkw.fsf at (no spam) keizer.soze.com...

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.

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.
 
Bill Cunningham...
Posted: Thu Oct 08, 2009 5:17 am
Guest
"Stefan Arentz" <stefan at (no spam) keizer.soze.com> wrote in message
news:87ab02ww75.fsf at (no spam) keizer.soze.com...
Quote:
What gcc version are you using? Is this actually on OS X?

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
 
Sherm Pendley...
Posted: Thu Oct 08, 2009 5:17 am
Guest
Stefan Arentz <stefan at (no spam) keizer.soze.com> writes:

Quote:
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--
 
spikeysnack...
Posted: Thu Oct 08, 2009 8:05 pm
Guest
On Oct 7, 4:25 pm, "Bill Cunningham" <nos... at (no spam) nspam.invalid> wrote:
Quote:
    I tried this simple program and got this error. This is the first time
I've writtten anything in obj-C. Why this error?

#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

Yo you walked into a religious fight going on since the 80s ...
Here's my take:
stdio.h is part of the standard C library it should be included as
per C rules:
#include <stdio.h>

objc headers should be imported:
 
spikeysnack...
Posted: Thu Oct 08, 2009 8:44 pm
Guest
On Oct 7, 4:25 pm, "Bill Cunningham" <nos... at (no spam) nspam.invalid> wrote:
Quote:
    I tried this simple program and got this error. This is the first time
I've writtten anything in obj-C. Why this error?

#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

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!
 
Bill Cunningham...
Posted: Thu Oct 08, 2009 8:55 pm
Guest
"Sherm Pendley" <spamtrap at (no spam) shermpendley.com> wrote in message
news:m2hbuavboc.fsf at (no spam) shermpendley.com...

Quote:
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
 
 
Page 1 of 1    
All times are GMT
The time now is Sun Nov 29, 2009 1:50 am