User forums > Using Code::Blocks

Compiling Objective-C - A Short Guide

(1/5) > >>

Justin Brimm:
For the longest time, I had endless trouble getting Objective-C to compile in C::B for me, and some web searches show that quite a few others have had issues with it as well. Well, a few weeks ago I finally got ObjC to compile flawlessly in C::B for me, and while I make no guarantees it'll actually work on anyone else's system, I've decided it'd be best to detail my settings so that others who are having trouble may have something to work from.

First things first, I'm compiling using TDM-GCC x64 4.5.1 on Windows 7, just in case it makes a difference.

1) Go to Settings->Compiler and dubugger...
2) Select GNU GCC Compiler and copy it; name it whatever you like, but I just went with GNU GCC Obj-C Compiler
3) Under Linker Settings, add -lobjc to Other linker options:; you don't need to explicitly add the libobjc.a library, that's what the flag is for.
4) Go to Settings->Environment...
5) Select Files extension handling and add *.m
6) Go to Settings->Editor...
7) Select Syntax highlighting and go to Filemasks.... Add *.m to the list of filetypes.
8) Go to Keywords... (next to Filemasks...) and create a new set (up arrow). Add this to the Keywords: box:


--- Code: ---@interface @implementation @end id BOOL
--- End code ---

9) Go to Project->Project tree->Edit file types & categories...
10) Under Sources, add *.m to the list of filetypes.

Optional

11) Go to Settings->Compiler and dubugger...
12) Under Other Settings, change Compiler logging to Full command line. If ObjC still refuses to build properly for you, you can use this to compare the command line arguments C::B uses against the commands you would use if you were building the program manually on the command line.
13) Under Other Settings, go to Advanced Options. For Link object files to executable and Link object files to console executable, move -o $exe_output to the end of the macros. I don't think this is strictly necessary, but it doesn't hurt.

Things to note!


* Whenever you add or create a new ObjC source (*.m) in your project, you must right-click on it and go to Properties.... Under advanced, change the compiler variable to CC. Under Build, select both Compile file and Link file. Before you close the dialog, go to General and uncheck File is read-only. This will automatically get selected when you change the other options and if you close the dialog before you uncheck it, you'll have to go back and change it, then close and reopen the file in the viewer before you can edit it. I don't know how to make C::B do this automatically, so if someone has an answer to that question, that'd be great.
* When you add a header file (*.h), you'll also need to open up its properties window and change the compiler variable to CC. You don't need to do anything else to it.
Test Build!

Here's an absolute bare-bones project you can throw together to test if your C::B settings will actually compile correctly. You can't actually test with just strict C, since the errors most people get stem from actually calling the Obj-C runtime library.

main.m

--- Code: ---#import <stdlib.h>
#import "TestObject.h"

int main(int argc, char** argv)
{
TestObject *aTestObject = [[TestObject alloc] init];
printf("Initial Value: %d\n", [aTestObject value]);
printf("+45 Value: %d\n", [aTestObject add: 45]);
printf("-63 Value: %d\n", [aTestObject subtract: 63]);

[aTestObject add: 103];
printf("+103 Value: %d\n", [aTestObject value]);

return (EXIT_SUCCESS);
}

--- End code ---

TestObject.h

--- Code: ---#import <objc/Object.h>

@interface TestObject : Object
{
int internalInt;
}

- (int)add:(int)anInt;
- (int)subtract:(int)anInt;
- (int)value;

@end

--- End code ---

TestObject.m

--- Code: ---#import "TestObject.h"

@implementation TestObject

- (id)init
{
if ((self = [super init]))
{
internalInt = 0;
}

return self;
}

- (int)add:(int)anInt
{
internalInt += anInt;
return internalInt;
}

- (int)subtract:(int)anInt
{
internalInt -= anInt;
return internalInt;
}

- (int)value
{
return internalInt;
}

@end

--- End code ---

MortenMacFly:
Thank you very much for this interesting information. I believe this should be in the WiKi in addition, so that it doesn't get lost. Mind doing so, please?

Justin Brimm:
I'm not sure I've ever added anything to a wiki before, but I'll give it a shot.

killerbot:
How about adding native support for the GCC objective c compiler in CB ?

Seems like a good addition to me.

oBFusCATed:
The current architecture is not suitable for supporting different compilers :(
Unfortunately, it is too C/C++ centric. Adding ObjC probably won't be a problem, but will be a hack/workaround not a true sollution. The same thing is adding D, Fortran, etc ...

Navigation

[0] Message Index

[#] Next page

Go to full version