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 Compiler3) 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
*.m6) 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:
@interface @implementation @end id BOOL
9) Go to
Project->Project tree->Edit file types & categories...10) Under
Sources, add
*.m to the list of filetypes.
Optional11) 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#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);
}
TestObject.h#import <objc/Object.h>
@interface TestObject : Object
{
int internalInt;
}
- (int)add:(int)anInt;
- (int)subtract:(int)anInt;
- (int)value;
@end
TestObject.m#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