Objective-C help

Joined
May 22, 2011
Messages
10
Reaction score
0
Points
1
Hi everyone,

I know there is a lot of threads with this topic but they didnt help me.
So, I installed the Xcode3 with the gcc 4.2 and created a simple hello world example.
Code:
#import <stdio.h>

int main( int argc, const char *argv[] ) {
    printf( "hello world\n" );
    return 0;
}

Then, I built it with the command:
Code:
gcc -lobjc hello.m -o hello
no problem until here.
Then, when i tried to run this example:
Code:
/* Comments:
 ** Example written by Pascal Bourguignon
 ** return status change by Chris B. Vetter
 ** Added comments in empty {} section by Chris B. Vetter
 ** int main() changed to int main(void) by Dennis Leeuw
 **            from comments by Chris B. Vetter
 */

#include <objc/Object.h>

@interface Greeter:Object
{
  /* This is left empty on purpose:
  ** Normally instance variables would be declared here,
  ** but these are not used in our example.
  */
}

- (void)greet;

@end

#include <stdio.h>

@implementation Greeter

- (void)greet
{
	printf("Hello, World!\n");
}

@end

#include <stdlib.h>

int main(void)
{
	id myGreeter;
	myGreeter=[Greeter new];

	[myGreeter greet];

	[myGreeter free];
	return EXIT_SUCCESS;
}
I got this:
Code:
MacBikka:Desktop bikka$ gcc -lobjc Greater.m -o Greater
Greater.m: In function ‘main’:
Greater.m:40: warning: ‘Greeter’ may not respond to ‘+new’
Greater.m:40: warning: (Messages without a matching method signature
Greater.m:40: warning: will be assumed to return ‘id’ and accept
Greater.m:40: warning: ‘...’ as arguments.)
Greater.m:44: warning: no ‘-free’ method found
I got this same errors with others examples.

Am I missing something? I think gcc is working fine. Could anyone try to run this example and tell me what happens?

thx in advanced!
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,745
Reaction score
2,071
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
I tried this code snippet on my Ubuntu machine and it worked fine with GCC 4.4.5, I even change the "[Greeter new]" to "[Greeter alloc]" and that worked fine too..

Make the same change and see if it works any better for you..

Code:
$ gcc -lobjc t1.m -o t1
$ ./t1
Hello, World!
$ gcc --version
gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
$

Regards
 
OP
bikka
Joined
May 22, 2011
Messages
10
Reaction score
0
Points
1
Hi Raz0rEdge,
thx for your help. I did the change but didnt work. I tried to download and install the xcode again but I got the same warnings.
any clue?
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,745
Reaction score
2,071
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
What version of GCC is being installed with XCode? I'll have to go home and try this on my Mac to see what I get there since GCC on Linux seems to be completely happy with this code..

Regards
 
OP
bikka
Joined
May 22, 2011
Messages
10
Reaction score
0
Points
1
thx Raz0rEdge, tell me when you compile the code.
Here is my gcc version.

Code:
MacBikka:~ bikka$ gcc --version
i686-apple-darwin10-gcc-4.2.1 (GCC) 4.2.1 (Apple Inc. build 5666) (dot 3)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
bikka, I cut and paste your code and it compiled and ran fine. I was using the same gcc version that you reported.
 
OP
bikka
Joined
May 22, 2011
Messages
10
Reaction score
0
Points
1
thx for the reply xstep,
so, if the code is fine and the gcc version also, you have any clue of what is missing?
Besides the xcode installed, do I need something else to run objective-C codes?
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
I don't know. I even cut and paste your gcc compile command.

The only thing I can think of is that your include isn't finding the Object.h. When I altered to code to get that result, I got the following. Notice though that it tells me that. Beyond the first two error lines, the warning lines are identical to yours.

Code:
Greater.m:9:26: error: objcx/Object.h: No such file or directory
Greater.m:12: error: cannot find interface declaration for ‘Object’, superclass of ‘Greeter’
Greater.m: In function ‘main’:
Greater.m:39: warning: ‘Greeter’ may not respond to ‘+new’
Greater.m:39: warning: (Messages without a matching method signature
Greater.m:39: warning: will be assumed to return ‘id’ and accept
Greater.m:39: warning: ‘...’ as arguments.)
Greater.m:43: warning: no ‘-free’ method found
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
I don't know. I even cut and paste your gcc compile command.

The only thing I can think of is that your include isn't finding the Object.h. When I altered to code to get that result, I got the following. Notice though that it tells me that. Beyond the first two error lines, the warning lines are identical to yours.

Code:
Greater.m:9:26: error: objcx/Object.h: No such file or directory
Greater.m:12: error: cannot find interface declaration for ‘Object’, superclass of ‘Greeter’
Greater.m: In function ‘main’:
Greater.m:39: warning: ‘Greeter’ may not respond to ‘+new’
Greater.m:39: warning: (Messages without a matching method signature
Greater.m:39: warning: will be assumed to return ‘id’ and accept
Greater.m:39: warning: ‘...’ as arguments.)
Greater.m:43: warning: no ‘-free’ method found
I think you're on to something there. Does it exist in your include dir?

For instance:
Code:
mikeMbp:Developer mike$ ls -l /Developer/SDKs/MacOSX10.6.sdk/usr/include/objc/Object.h
-rw-r--r--  1 root  wheel  3723 Jun 24  2010 /Developer/SDKs/MacOSX10.6.sdk/usr/include/objc/Object.h
mikeMbp:Developer mike$
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
The following find command shows Object.h in many many places. As you can see by the list, I have both Xcode 3 & 4 installed, each with their own copies.

Code:
find / -name "Object.h" -print

Code:
/Xcode3/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/usr/include/objc/Object.h
/Xcode3/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/usr/include/objc/Object.h
/Xcode3/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.1.sdk/usr/include/objc/Object.h
/Xcode3/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/usr/include/objc/Object.h
/Xcode3/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/usr/include/objc/Object.h
/Xcode3/SDKs/MacOSX10.5.sdk/usr/X11/include/X11/Object.h
/Xcode3/SDKs/MacOSX10.5.sdk/usr/include/objc/Object.h
/Xcode3/SDKs/MacOSX10.6.sdk/usr/X11/include/X11/Object.h
/Xcode3/SDKs/MacOSX10.6.sdk/usr/include/objc/Object.h
/Xcode4/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/usr/include/objc/Object.h
/Xcode4/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/usr/include/objc/Object.h
/Xcode4/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.1.sdk/usr/include/objc/Object.h
/Xcode4/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/usr/include/objc/Object.h
/Xcode4/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/usr/include/objc/Object.h
/Xcode4/SDKs/MacOSX10.6.sdk/usr/X11/include/X11/Object.h
/Xcode4/SDKs/MacOSX10.6.sdk/usr/include/objc/Object.h
/usr/X11/include/X11/Object.h
/usr/include/objc/Object.h

A possible fix may be this.
Code:
#include "/usr/include/objc/Object.h"

Another fix is to use the include flag such as this in cc command.
Code:
gcc -I /usr/include/objc -lobjc Greater.m -o Greater

Unfortunately neither tells us why bikka is having the problem.

I'm thinking there is also a shell variable that can be set, but am not sure.
 
OP
bikka
Joined
May 22, 2011
Messages
10
Reaction score
0
Points
1
I don't know. I even cut and paste your gcc compile command.

The only thing I can think of is that your include isn't finding the Object.h.
I think you're on to something there. Does it exist in your include dir?

For instance:
Code:
mikeMbp:Developer mike$ ls -l /Developer/SDKs/MacOSX10.6.sdk/usr/include/objc/Object.h
-rw-r--r--  1 root  wheel  3723 Jun 24  2010 /Developer/SDKs/MacOSX10.6.sdk/usr/include/objc/Object.h
mikeMbp:Developer mike$

That was a good point, but running the command above I got the same return:
Code:
MacBikka:Desktop bikka$ ls -l /Developer/SDKs/MacOSX10.6.sdk/usr/include/objc/Object.h
-rw-r--r--  1 root  wheel  3723 Oct 23  2010 /Developer/SDKs/MacOSX10.6.sdk/usr/include/objc/Object.h





The following find command shows Object.h in many many places. As you can see by the list, I have both Xcode 3 & 4 installed, each with their own copies.

Code:
find / -name "Object.h" -print

Code:
/Xcode3/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/usr/include/objc/Object.h
/Xcode3/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/usr/include/objc/Object.h
/Xcode3/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.1.sdk/usr/include/objc/Object.h
/Xcode3/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/usr/include/objc/Object.h
/Xcode3/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/usr/include/objc/Object.h
/Xcode3/SDKs/MacOSX10.5.sdk/usr/X11/include/X11/Object.h
/Xcode3/SDKs/MacOSX10.5.sdk/usr/include/objc/Object.h
/Xcode3/SDKs/MacOSX10.6.sdk/usr/X11/include/X11/Object.h
/Xcode3/SDKs/MacOSX10.6.sdk/usr/include/objc/Object.h
/Xcode4/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator3.2.sdk/usr/include/objc/Object.h
/Xcode4/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.0.sdk/usr/include/objc/Object.h
/Xcode4/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.1.sdk/usr/include/objc/Object.h
/Xcode4/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/usr/include/objc/Object.h
/Xcode4/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.3.sdk/usr/include/objc/Object.h
/Xcode4/SDKs/MacOSX10.6.sdk/usr/X11/include/X11/Object.h
/Xcode4/SDKs/MacOSX10.6.sdk/usr/include/objc/Object.h
/usr/X11/include/X11/Object.h
/usr/include/objc/Object.h

A possible fix may be this.
Code:
#include "/usr/include/objc/Object.h"

Another fix is to use the include flag such as this in cc command.
Code:
gcc -I /usr/include/objc -lobjc Greater.m -o Greater

Unfortunately neither tells us why bikka is having the problem.

I'm thinking there is also a shell variable that can be set, but am not sure.

I tried both of the substitutions you mentioned but still got the same warnings.

guys, I really appreciate your help, I have absolutly no idea what's going on =/
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
Hrm.. wonder if you should subclass NSObject and include <Foundation/Foundation.h> instead
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
Hrm.. wonder if you should subclass NSObject and include <Foundation/Foundation.h> instead

His code is not Cocoa, just straight Objective-C. Besides, I and Raz0rEdge had no problem with the code as is.
 
Joined
Jun 25, 2005
Messages
3,231
Reaction score
112
Points
63
Location
On the road
Your Mac's Specs
2011 MBP, i7, 16GB RAM, MBP 2.16Ghz Core Duo, 2GB ram, Dual 867Mhz MDD, 1.75GB ram, ATI 9800 Pro vid
That was a good point, but running the command above I got the same return:
Code:
MacBikka:Desktop bikka$ ls -l /Developer/SDKs/MacOSX10.6.sdk/usr/include/objc/Object.h
-rw-r--r--  1 root  wheel  3723 Oct 23  2010 /Developer/SDKs/MacOSX10.6.sdk/usr/include/objc/Object.h

I tried both of the substitutions you mentioned but still got the same warnings.

guys, I really appreciate your help, I have absolutly no idea what's going on =/

I have to ask. Did you use them precisely as I posted, and separately?

I'd also run the find command that I posted to see if it sheds any light on the situation. I do believe you should be using the file in my samples above.

Add this to your gcc command line.
Code:
-Wmissing-include-dirs
Interestingly, -Wall doesn't pick up missing include directories.


I'm out of ideas at the moment. I'm wondering if a re-install might help. Did you install in the default location?
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,745
Reaction score
2,071
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
His code is not Cocoa, just straight Objective-C. Besides, I and Raz0rEdge had no problem with the code as is.

This is strange now, I'm trying this on my Mac at home and am getting the exact same problem as Bikka..this works in the Linux environment, but not so in Mac. I've got the same version of GCC as you two..4.2.1.

The preprocessing of Greeter.m shows that the "Object.h" file is being pulled from /usr/include/objc..

This has piqued my curiosity..gonna investigate this further..

Update: OK, I got it to work
Code:
$ gcc -o Greeter Greeter.m -lobjc -m32
$ file Greeter
Greeter: Mach-O executable i386
$ ./Greeter 
Hello, World!

Without the "-m32" flag we get
Code:
$ file Greeter
Greeter: Mach-O 64-bit executable x86_64

So my guess is that we don't have the multi-lib support and were trying to build the 64-bit application against the 32-bit libraries, with the "-m32" they match and things work..
 
OP
bikka
Joined
May 22, 2011
Messages
10
Reaction score
0
Points
1
yeah! :D
You are right Raz0rEdge! thank you very much! Its working now
And thank you xstep and Dysfunction.

One more simple question. where do I change my avatar and edit my profile settings? :p
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,745
Reaction score
2,071
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
Click on "Profile" at the top of the page, then "Edit Avatar"..
 
Joined
Mar 17, 2008
Messages
6,879
Reaction score
191
Points
63
Location
Tucson, AZ
Your Mac's Specs
Way... way too many specs to list.
If nothing else, this thread has made me get off my duff and download/install the latest xcode (things were borked on this box it would seem). The 4.1GB d/load for 3.2.6 sucks.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top