Link to home
Start Free TrialLog in
Avatar of mindwarpltd
mindwarpltd

asked on

How do I compile an Objectiver C souce file with Dev-C++ ?

I have the following Object C code (see below).

What compiler parameter and linker options do I need to set to compile this code?

I've created a C command line project and added a main.c file and added the code below.

But I can't get it to compile.

Heres the parameters I used with the gcc compiler..

#!/bin/bash
gcc `gnustep-config --objc-flags` -L /GNUstep/System/Library/Libraries test.m -o test.exe -lgnustep-base -lobjc

#import <Foundation/Foundation.h>
 
 
/* Interface */
@interface Fraction: NSObject 
{
	int numerator;
	int denominator;
}
 
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(void) dealloc;
@end
 
 
/* Implementation */
@implementation Fraction;
-(void) print
{
	NSLog (@"The value of the fraction is %i/%i", numerator, denominator);
	
}
 
-(void) setNumerator: (int) n {
	numerator = n;
}
 
-(void) setDenominator: (int) d {
	denominator = d;
}
 
-(void) dealloc {
	NSLog(@"Release called. Deallocating memory...");
	[super dealloc];
}
@end
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
	Fraction *myFraction = [[Fraction alloc] init];
	
	// Set fraction to 3/4
	[myFraction setNumerator: 3];
	[myFraction setDenominator: 4];
	
	// the 'print' method was never called, decided to put the message there
	// Display the fraction
	[myFraction print];
	
        //avoid memory leak with release and super delloc
	[myFraction release];
    [pool drain];
    return 0;
}

Open in new window

Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

In Xcode simply press Cmd+B.

The code is in Objective-C. So the file name should be main.m.

Probably, you are still under Windows XP with GNUStep. So in the command line change the file name. This is test.m now. Set is as main.m

gcc `gnustep-config --objc-flags` -L /GNUstep/System/Library/Libraries main.m -o Fraction.exe -lgnustep-base -lobjc

The output file is Fraction.exe.

You question does not contain anything about C++.
Avatar of mindwarpltd
mindwarpltd

ASKER

No i'm using Dev C++ on Windows XP
:)
So write this program in C++.
We have C++ zone here on EE. You can post this code in that zone.

Right now, the code in the original question is a Foundation tool project, pure Objective-C 2.0 code from a well-known Objective-C book - "Programming in Objective-C 2.0" by Stephan G. Kochan.
I need to use objective c
ASKER CERTIFIED SOLUTION
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That line works with gcc for another objective c program.
But I'm not sure how to specify this in Dev-C++
I do not know. Sorry.
If you have time, you can wait here. You can delete this question and open new one in C++ zones.
the problem here is Dev C++.

Dev C++ has been abandoned:  there has been no development work on it since 2005.  

scrap Dev C++ and use a fully maintained IDE with the most up-to-date compiler available.

i suggest Code::Blocks.   free, open source, cross-platform and stable release this past May 2010.

www.codeblocks.com


i believe i've given you the answer.
@rwj04: no.

http://#a33477515 is the answer. The command line was wrong - it was compiling test.m file but the source code was in main.c. I said how to fix it. But I have no idea how to work with Dev-C.
you may be right.  but...

DevC++ on Windows XP uses the MinGW compiler which has had significant development related to Objective C in the past 6 years.

DevC++ has been abandoned for the past 6 years.

Given that, and given the fact he is having problems compiling Objective C on his Windows version of Dev C++, the *very first thing* he needs to do is get rid of the problematic abandoned development environment.

then he can work on the details.  

otherwise we're just flailing about poking things at random and hoping something works out.   you can't very well diagnose and fix a problem when your tools are quite likely to be failing.




It's not 'my tool'. I use Xcode.
I was using the royal "we" and the generic "you"

dost thou understandest us?

:-P


I'm having some problems downloading code blocks due to my very poor internet connection at the moment. I hope to have this sorted out soon.
I've managed to get code blocks installed, and have loaded an m file, which I can't get to compile, not sure what to do from here.
m file?  is that MATLAB?


Sorry I'm calling it an m file, as in xcode thats the implementation file, however I also have the interface code at the top which would be in a h file in xcode.

Anyways, heres what I have in my m file.
#import <Foundation/Foundation.h>
 
 
/* Interface */
@interface Fraction: NSObject 
{
	int numerator;
	int denominator;
}
 
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(void) dealloc;
@end
 
 
/* Implementation */
@implementation Fraction;
-(void) print
{
	NSLog (@"The value of the fraction is %i/%i", numerator, denominator);
	
}
 
-(void) setNumerator: (int) n {
	numerator = n;
}
 
-(void) setDenominator: (int) d {
	denominator = d;
}
 
-(void) dealloc {
	NSLog(@"Release called. Deallocating memory...");
	[super dealloc];
}
@end
 
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 
	Fraction *myFraction = [[Fraction alloc] init];
	
	// Set fraction to 3/4
	[myFraction setNumerator: 3];
	[myFraction setDenominator: 4];
	
	// the 'print' method was never called, decided to put the message there
	// Display the fraction
	[myFraction print];
	
        //avoid memory leak with release and super delloc
	[myFraction release];
    [pool drain];
    return 0;
}

Open in new window

oh, right.

so post the error message you get.




Sorry for the delay

Nothing to be done.

Checking for existence: C:\My Documents\ObjC\fraction.exe
Thats what it says in the Build Log tab
delete the executable  and rebuild.  what  are the errors?


I don't have an executable.

Also, if I click compile this file, instead, it says nothing to be done.
I must be doing something really simple wrong ?
Yes.
It does possible to compile Objective-C code with GNUStep. You can use any text editor (Notepad, for example), or any IDE such as Dev-C++ or CodeBlocks as a text editor. You do know how to compile this Objective-C code in the command line and it works for you.
But what you are trying to achieve? That's the most strange thing.
Anyway, this Objective-C app compiled under Windows will not work on iPhone. For sure you understand that with C++ or C# you can make much better Windows application than with Objective-C.

> ... Objective-C code in the command line and it works for you.
Yes I was able to get GNUStep to work from the command line before I unsintalled it for Code Blocks

>But what you are trying to achieve? That's the most strange thing.
I'm just following rwj04 suggestion, which would get me one step closer to my goal.

>Anyway, this Objective-C app compiled under Windows will not work on iPhone. For sure you understand that with C++ or C# you can make much better Windows application than with Objective-C.

Yes I understand I can't compile iphone apps on windows, however I had hoped to produce some functions which I could get working on my laptop out of the office then copy them over to xcode. However, I want to get sqlite working in the ide on windows too.
You do know my opinion about it: you waste your time. If you need to develop on Mac in Xcode, do it.

Any other way will bring you a lot of troubles and will require much more time. What is natural in Xcode on Snow Leopard can be weird with GNUStep and Windows. Now you stuck on an issue for a week and cannot compile anything. You are not learning and not developing. If you will solve this problem, then a problem with sqllite (that is not about GNUStep at all), you will find new problem. And so on. All the time you will be facing strange unexpected problems related to this Windows + GNUStep -> Xcode bridge.
You may need GNUStep under Windows only if you are developing a cross-platform application. Even in this case Objective-C is not the best choice.

I wish you good luck!

Best regards
No I've been on holiday, i've only spent time answering these questions.

Just will enable me to do little bit of code when i'm away from my mac mini
comment http://#a33477515 is the answer.

I verified it in http://#a33478633.
I do not know how to compile an Objective-C code in Dev-C. Probably, there is a way to specify the command line.