Solved

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

Posted on 2010-08-19
30
1,258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 12
  • 9
  • 7
30 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33477515
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
ID: 33477649
No i'm using Dev C++ on Windows XP
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33477723
:)
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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:mindwarpltd
ID: 33478614
I need to use objective c
0
 
LVL 33

Accepted Solution

by:
pgnatyuk earned 500 total points
ID: 33478633
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
ID: 33478883
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
ID: 33478950
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
ID: 33490730
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
ID: 33490733
i believe i've given you the answer.
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33490746
@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
ID: 33490798
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
ID: 33490800
It's not 'my tool'. I use Xcode.
0
 
LVL 5

Expert Comment

by:rwj04
ID: 33490865
I was using the royal "we" and the generic "you"

dost thou understandest us?

:-P


0
 

Author Comment

by:mindwarpltd
ID: 33492058
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
 

Author Comment

by:mindwarpltd
ID: 33494562
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
ID: 33496104
m file?  is that MATLAB?


0
 

Author Comment

by:mindwarpltd
ID: 33496506
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
ID: 33497713
oh, right.

so post the error message you get.




0
 

Author Comment

by:mindwarpltd
ID: 33512625
Sorry for the delay

Nothing to be done.

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

Author Comment

by:mindwarpltd
ID: 33512634
Thats what it says in the Build Log tab
0
 
LVL 5

Expert Comment

by:rwj04
ID: 33512985
delete the executable  and rebuild.  what  are the errors?


0
 

Author Comment

by:mindwarpltd
ID: 33513154
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
ID: 33519500
I must be doing something really simple wrong ?
0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33519574
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
ID: 33520024
> ... 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
ID: 33522306
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
ID: 33522712
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
ID: 33812152
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This guide will walk you through the essential considerations and tech stack for building scalable websites. Know how to grow your business the smart way!
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.

710 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