Author Topic: Compiling Objective-C - A Short Guide  (Read 32152 times)

Offline Justin Brimm

  • Multiple posting newcomer
  • *
  • Posts: 13
Compiling Objective-C - A Short Guide
« 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!

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

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

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Compiling Objective-C - A Short Guide
« Reply #1 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?
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline Justin Brimm

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: Compiling Objective-C - A Short Guide
« Reply #2 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.

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5490
Re: Compiling Objective-C - A Short Guide
« Reply #3 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.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Compiling Objective-C - A Short Guide
« Reply #4 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 ...
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5490
Re: Compiling Objective-C - A Short Guide
« Reply #5 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.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13413
    • Travis build status
Re: Compiling Objective-C - A Short Guide
« Reply #6 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...
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline x-code

  • Single posting newcomer
  • *
  • Posts: 3
Re: Compiling Objective-C - A Short Guide
« Reply #7 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.

Offline Justin Brimm

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: Compiling Objective-C - A Short Guide
« Reply #8 on: March 29, 2011, 10:40:09 am »
Created a new page on the wiki: 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.
« Last Edit: March 29, 2011, 11:07:21 am by Justin Brimm »

Offline Justin Brimm

  • Multiple posting newcomer
  • *
  • Posts: 13
Re: Compiling Objective-C - A Short Guide
« Reply #9 on: April 10, 2011, 04:29:01 am »
Modified the wiki page!
  • Added troubleshooting section and information on how to resolve some poorly documented errors I've run into myself.
  • Updated the list of keywords for syntax highlighting.

Offline uSer

  • Single posting newcomer
  • *
  • Posts: 4
Re: Compiling Objective-C - A Short Guide
« Reply #10 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.

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Compiling Objective-C - A Short Guide
« Reply #11 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.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline uSer

  • Single posting newcomer
  • *
  • Posts: 4
Re: Compiling Objective-C - A Short Guide
« Reply #12 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?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Compiling Objective-C - A Short Guide
« Reply #13 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.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline uSer

  • Single posting newcomer
  • *
  • Posts: 4
Re: Compiling Objective-C - A Short Guide
« Reply #14 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

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: Compiling Objective-C - A Short Guide
« Reply #15 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.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline uSer

  • Single posting newcomer
  • *
  • Posts: 4
Re: Compiling Objective-C - A Short Guide
« Reply #16 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

rro4785

  • Guest
Re: Compiling Objective-C - A Short Guide
« Reply #17 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

Offline mariusm

  • Multiple posting newcomer
  • *
  • Posts: 46
Re: Compiling Objective-C - A Short Guide
« Reply #18 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.
« Last Edit: February 16, 2023, 12:11:47 pm by mariusm »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1553
Re: Compiling Objective-C - A Short Guide
« Reply #19 on: February 16, 2023, 01:21:07 pm »
Try in compiler global settings, clicking in the "Advanced options" button.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7582
    • My Best Post
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline mariusm

  • Multiple posting newcomer
  • *
  • Posts: 46
Re: Compiling Objective-C - A Short Guide
« Reply #21 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)
« Last Edit: February 17, 2023, 12:29:49 pm by mariusm »