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:
@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.
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
#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
Hello! First of all, I would like to thank for the guide. ;D
I would like to clarify that my English is very bad, and try to translate my question here. ;D
When trying to compile the example of your setup guide, throws me the following error:
:-\
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\main.m||In function 'main':|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\main.m|5|warning: 'TestObject' may not respond to '+alloc' [enabled by default]|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\main.m|5|warning: (Messages without a matching method signature [enabled by default]|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\main.m|5|warning: will be assumed to return 'id' and accept [enabled by default]|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\main.m|5|warning: '...' as arguments.) [enabled by default]|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\main.m|5|warning: no '-init' method found [enabled by default]|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\main.m|6|warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\main.m|6|warning: incompatible implicit declaration of built-in function 'printf' [enabled by default]|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\TestObject.h|1|warning: #import is a deprecated GCC extension [-Wdeprecated]|
d:\herramientas\mingw\bin\..\lib\gcc\mingw32\4.7.2\include\objc\Object.h|50|error: stray '@' in program|
d:\herramientas\mingw\bin\..\lib\gcc\mingw32\4.7.2\include\objc\Object.h|50|error: unknown type name 'interface'|
d:\herramientas\mingw\bin\..\lib\gcc\mingw32\4.7.2\include\objc\Object.h|51|error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' token|
d:\herramientas\mingw\bin\..\lib\gcc\mingw32\4.7.2\include\objc\Object.h|54|error: expected identifier or '(' before '-' token|
d:\herramientas\mingw\bin\..\lib\gcc\mingw32\4.7.2\include\objc\Object.h|55|error: expected identifier or '(' before '-' token|
d:\herramientas\mingw\bin\..\lib\gcc\mingw32\4.7.2\include\objc\Object.h|56|error: stray '@' in program|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\TestObject.h|3|error: stray '@' in program|
d:\herramientas\mingw\bin\..\lib\gcc\mingw32\4.7.2\include\objc\Object.h|56|error: unknown type name 'end'|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\TestObject.h|3|error: expected '=', ',', ';', 'asm' or '__attribute__' before 'TestObject'|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\TestObject.h|7|error: expected identifier or '(' before '-' token|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\TestObject.h|8|error: expected identifier or '(' before '-' token|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\TestObject.h|9|error: expected identifier or '(' before '-' token|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\TestObject.h|11|error: stray '@' in program|
F:\Proyectos\Otros\CodeBlocks\Objective-C\Test\Test\TestObject.h|11|error: expected '=', ',', ';', 'asm' or '__attribute__' at end of input|
||=== Build finished: 14 errors, 8 warnings (0 minutes, 3 seconds) ===|
???
No that's what I do, might help me please.
Thanks in advance. :D