Solved

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

Posted on 2010-08-19
30
1,180 Views
Last Modified: 2013-12-13
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

0
Comment
Question by:mindwarpltd
  • 12
  • 9
  • 7
30 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
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++.
0
 

Author Comment

by:mindwarpltd
Comment Utility
No i'm using Dev C++ on Windows XP
0
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
:)
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.
0
 

Author Comment

by:mindwarpltd
Comment Utility
I need to use objective c
0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
Comment Utility
gcc `gnustep-config --objc-flags` -L /GNUstep/System/Library/Libraries main.m -o Fraction.exe -lgnustep-base -lobjc

Does this line works?

main.m is the name of the file with the source code.
Fraction.exe - name of the executable.
0
 

Author Comment

by:mindwarpltd
Comment Utility
That line works with gcc for another objective c program.
But I'm not sure how to specify this in Dev-C++
0
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
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.
0
 
LVL 5

Expert Comment

by:rwj04
Comment Utility
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


0
 
LVL 5

Expert Comment

by:rwj04
Comment Utility
i believe i've given you the answer.
0
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
@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.
0
 
LVL 5

Expert Comment

by:rwj04
Comment Utility
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.




0
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
It's not 'my tool'. I use Xcode.
0
 
LVL 5

Expert Comment

by:rwj04
Comment Utility
I was using the royal "we" and the generic "you"

dost thou understandest us?

:-P


0
 

Author Comment

by:mindwarpltd
Comment Utility
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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:mindwarpltd
Comment Utility
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.
0
 
LVL 5

Expert Comment

by:rwj04
Comment Utility
m file?  is that MATLAB?


0
 

Author Comment

by:mindwarpltd
Comment Utility
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

0
 
LVL 5

Expert Comment

by:rwj04
Comment Utility
oh, right.

so post the error message you get.




0
 

Author Comment

by:mindwarpltd
Comment Utility
Sorry for the delay

Nothing to be done.

Checking for existence: C:\My Documents\ObjC\fraction.exe
0
 

Author Comment

by:mindwarpltd
Comment Utility
Thats what it says in the Build Log tab
0
 
LVL 5

Expert Comment

by:rwj04
Comment Utility
delete the executable  and rebuild.  what  are the errors?


0
 

Author Comment

by:mindwarpltd
Comment Utility
I don't have an executable.

Also, if I click compile this file, instead, it says nothing to be done.
0
 

Author Comment

by:mindwarpltd
Comment Utility
I must be doing something really simple wrong ?
0
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
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.

0
 

Author Comment

by:mindwarpltd
Comment Utility
> ... 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.
0
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
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
0
 

Author Comment

by:mindwarpltd
Comment Utility
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
0
 
LVL 33

Expert Comment

by:pgnatyuk
Comment Utility
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.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

A list of useful business intelligence software.
Workplace bullying has increased with the use of email and social media. Retain evidence of this with email archiving to protect your employees.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Viewers will learn how to use the Hootsuite Dashboard.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now