Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: Justin Brimm on March 17, 2011, 01:58:13 am

Title: Compiling Objective-C - A Short Guide
Post by: Justin Brimm on March 17, 2011, 01:58:13 am
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

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!


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);
}

TestObject.h
Code
#import <objc/Object.h>

@interface TestObject : Object
{
int internalInt;
}

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

@end

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
Title: Re: Compiling Objective-C - A Short Guide
Post by: MortenMacFly on March 17, 2011, 07:13:59 am
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?
Title: Re: Compiling Objective-C - A Short Guide
Post by: Justin Brimm on March 23, 2011, 02:18:42 am
I'm not sure I've ever added anything to a wiki before, but I'll give it a shot.
Title: Re: Compiling Objective-C - A Short Guide
Post by: killerbot on March 23, 2011, 12:50:04 pm
How about adding native support for the GCC objective c compiler in CB ?

Seems like a good addition to me.
Title: Re: Compiling Objective-C - A Short Guide
Post by: oBFusCATed on March 23, 2011, 01:40:49 pm
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 ...
Title: Re: Compiling Objective-C - A Short Guide
Post by: killerbot on March 23, 2011, 03:03:45 pm
but the compiler itself can be supported. CB supports nicely different compilers. I use to have projects where each target (total 7 targets) was building the same sources but with a different compiler.
Title: Re: Compiling Objective-C - A Short Guide
Post by: oBFusCATed on March 23, 2011, 03:44:06 pm
Yes, but this is done in a hacky way...
You have to make the compiler look like a C/C++ compiler...

Something should be done to make it more native for other compilers...
Title: Re: Compiling Objective-C - A Short Guide
Post by: x-code on March 27, 2011, 04:00:58 pm
Adding Code:: Blocks support ObjC and Go languages would be very helpful. Indeed, support of D language is present and D is not even part of gcc, as opposed to ObjC and, more recently, Go.
I think that is enough to start a minimum of support: add project types "ObjC Project" ("Go Project", "Fortran Project", "Ada Project" etc.), and a simple syntax highlighting.
Title: Re: Compiling Objective-C - A Short Guide
Post by: Justin Brimm on March 29, 2011, 10:40:09 am
Created a new page on the wiki: Installing Objective-C Compiler (http://wiki.codeblocks.org/index.php?title=Installing_Objective-C_Compiler)

I don't like half-***ing things, so I took the time to expand on a few more things and made the page properly formatted and sectioned, instead of just copy and pasting my post into the wiki page. Hopefully this helps people out.

By the way, Objective-C is a strict superset of C, so it actually uses the standard GCC C compiler; all the magic is actually done by the ObjC library. When you compile, the C compiler uses the library to convert all ObjC functionality into strict C. Methods are all converted into C functions, and things like classes are simply used by the compiler to properly name and optimize the instance variables and functions. The compiler and library also handle the low-level memory management for classes, so creating or destroying a class properly creates and releases blocks of memory that can store all the information defined in that class.

Because ObjC uses the standard GCC C compiler, C::B really shouldn't need much in the way of hacks to natively support Objective-C compiling. All C::B really needs beyond a default GCC Objective-C Compiler setup and Objective-C project type, is proper support for Objective-C filetypes. By default, C::B won't compile or link new source files until you change their settings, and it defaults the compiler variable for all sources and headers to CPP, when they need to be CC. I think this is because ObjC sources are *.m and C::B confuses them with C++ sources.
Title: Re: Compiling Objective-C - A Short Guide
Post by: Justin Brimm on April 10, 2011, 04:29:01 am
Modified the wiki page!
Title: Re: Compiling Objective-C - A Short Guide
Post by: uSer on December 29, 2011, 05:08:40 pm
I have done everything as instructed but on building the the project i am getting this error message:
 
     fatal error: foundation\foundation.h: No such file or directory

Please reply ASAP.
Title: Re: Compiling Objective-C - A Short Guide
Post by: MortenMacFly on December 29, 2011, 06:02:28 pm
     fatal error: foundation\foundation.h: No such file or directory
Seems you don't have installed the OSX SDK. Without that, you are missing the heart of you development chain.
Title: Re: Compiling Objective-C - A Short Guide
Post by: uSer on December 30, 2011, 04:07:22 am
Thanks for reply, i am working on windows XP with CodeBlocks, how can I install OSX SDK. Is it available for windows?
Title: Re: Compiling Objective-C - A Short Guide
Post by: MortenMacFly on December 30, 2011, 07:41:02 am
Thanks for reply, i am working on windows XP with CodeBlocks, how can I install OSX SDK. Is it available for windows?
No. You cannot develop for a Mac on Windows. You'll need a Mac to develop for Mac, also for legal reasons.
Title: Re: Compiling Objective-C - A Short Guide
Post by: uSer on December 30, 2011, 02:42:56 pm
Thanks, but he said that "he is compiling using TDM-GCC x64 4.5.1 on Windows 7" for Objective-C
Title: Re: Compiling Objective-C - A Short Guide
Post by: MortenMacFly on December 30, 2011, 04:17:23 pm
Thanks, but he said that "he is compiling using TDM-GCC x64 4.5.1 on Windows 7" for Objective-C
Yes, so then don't use OSX SDK dependencies, cause even if you are using an Objective/C compiler on Windows, this won't work otherwise. Surely you can create any projects that do not depend on the OSX SDK. That wasn't my point.
Title: Re: Compiling Objective-C - A Short Guide
Post by: uSer on January 08, 2012, 07:07:41 pm
 Oh got your point now, but then what's the alternate of THESE frameworks, because we need them to compile obj C
Title: Re: Compiling Objective-C - A Short Guide
Post by: rro4785 on March 09, 2013, 05:03:14 am
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:

 :-\

Code
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
Title: Re: Compiling Objective-C - A Short Guide
Post by: mariusm on February 16, 2023, 11:58:57 am
I know it's old but need.

How to add -x objective-c++ on .mm files compilation and -x c++ to .cpp files compilation?

I tried to modify the xml compiler options but I've no success with that.
Title: Re: Compiling Objective-C - A Short Guide
Post by: Miguel Gimenez on February 16, 2023, 01:21:07 pm
Try in compiler global settings, clicking in the "Advanced options" button.
Title: Re: Compiling Objective-C - A Short Guide
Post by: stahta01 on February 16, 2023, 05:28:58 pm
This wiki link might help https://wiki.codeblocks.org/index.php/Adding_support_for_non_C/C%2B%2B_files_to_the_build_system (https://wiki.codeblocks.org/index.php/Adding_support_for_non_C/C%2B%2B_files_to_the_build_system)

Tim S.
Title: Re: Compiling Objective-C - A Short Guide
Post by: mariusm on February 17, 2023, 12:06:04 pm
Works even for C files in cpp project, must add an option with -x c.

THANKS  ;D 8)