# HOWTO: Install GNUStep on FreeBSD & Compile Objective-C Code



## torr_from_fallout2 (May 4, 2013)

HOWTO: Install GNUStep on FreeBSD & Compile Objective-C Code
*Purpose:*
If you've planning of doing Objective-C compiling on FreeBSD 9.1, then the following HOWTO will explain to setup and compile objective-C code up using the 'GNUStep' framework. 

Note: This is a good introduction into programming objective-C programs on FreeBSD, afterward successfully getting your first app to compile, you will have to set out on learning more complex stuff on your own.

*Assumptions:*

You are a novice/beginner with FreeBSD.
You installed FreeBSD 9.1 from scratch with nothing else installed beforehand.
You have basic/advanced knowledge of Objective-C programming.
You program on a Apple Macintosh Computer using Xcode or something else and you're looking to port your code to FreeBSD.
You are running FreeBSD 9.1.
*GNUStep Setup Steps:*
1) Update ports
`# portsnap fetch`
`# ports extract`
`# ports update`

2) Install portupgrade
`# cd /usr/ports/ports-mgmt/portupgrade`
`# make install -DBATCH clean`

(Note: "DBATCH" makes it automatically select the default packages, I'm ok with that. If you are as well, leave it, else remove it and you will have to be on stand-by to select the options as they popup. If you left it and your computer is slow, go grab some tea/coffee/beer/etc. and do something else.)

3) Let's install the Objective-C 2.0 Runtime framework upgrade as this is the framework that you find on the Apple platform:
`# portupgrade -N --batch libobjc2`

4) Install GNUstep:
`# portupgrade -N --batch gnustep`

(Note: The 'batch' works the same way with the portupgrade application. So if you have other things to do, feel free to leave the installation unattended.)

Now we need to set it up with our user.
`# vi ~/.profile`

Append the file with the following line:

```
GNUSTEP_SYSTEM_TOOLS=/usr/local/GNUstep/System/Tools
  export GNUSTEP_SYSTEM_TOOLS
```

*Objective-C Compiling Steps:*
5) Once the installation completes, we will have to confirm the code works. Lets create a temporary directory to run our code in:
`# cd ~/`
`# mkdir testdir`
`# cd testdir`

6) Let's create our first application! The following was 'more then inspired' from the following link:
`# cat > example.m`

Populate the file with the following:

```
:
#import <Foundation/Foundation.h>

int main( int argc, const char *argv[] ) {

    NSLog (@"First ObjC Program");

    return 0;
}
```

Ok, now lets create our make file: `# cat > GNUmakefile`

Populate the file with the following:

```
GNUSTEP_MAKEFILES=/usr/local/GNUstep/System/Library/Makefiles

include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = exampleapp
exampleapp_OBJC_FILES = example.m

include $(GNUSTEP_MAKEFILES)/tool.make
```
Now we will compile our objective-c code:
`# gmake`

The compiler should have made an 'obj' directory, now lets go into it and give permission to run everything inside:
`# cd ./obj`
`# chmod 755 ./*`

Now lets run it! It should give get the 'First ObjC Program' message in the console:
`# ./exampleapp`

GREAT! You should see a "First ObjC Program" in the console. You have written your first application. The next step is to read the manual from GNUstepand good luck with your learning. :stud

*Note to Programmers:*

You can compile code which uses the "Grand Central Dispatch" framework. You simply have to install it through ports. (Reference: Link)
Be mindful that there will be some functions that you find in Xcode missing in GNUStep.
There are other Objective-C Run-time Frameworks available if GNUStep doesn't meet your requirements. Try CocoTron.


----------



## torr_from_fallout2 (May 5, 2013)

<Reserved for any addendum steps>


----------



## throAU (May 6, 2013)

Awesome, will give this a shot soon.

Would really like to see Objective-C take off on FreeBSD as it would enable a lot more cross-platform development between OS X and FreeBSD.


----------



## zeissoctopus (May 6, 2013)

Thank you very much! I am waiting for this howto over 1 year.

May you tech our how to compile GNUstep with libobjc2 and Clang from FreeBSD 9.1 base?


----------



## torr_from_fallout2 (May 8, 2013)

Thanks guys!

I agree - I am wanting to port some Mac OS X code into FreeBSD and have been surprised at the lack of resources which is why I decided to write this.

Sadly, I originally tried installing GNUStep from ports and it didn't work, I wasn't able to get it working with Clang either sadly. I created this threadbecause of my problem.

Hmmm, I tried pkg_info and I didn't see libobjc2 which is problematic as the code I wanted to port is written with the new Objective-C 2.0 ... I will add this to my TODO list to investigate and once I figure it out, I'll post it in this thread. Another TODO item I have is getting 'Grand Central Dispatch' working as well so I can compile code with NSOperation objects.


----------



## kpa (May 8, 2013)

lang/gnustep-base fails to configure with clang(1) and I believe the problem is the CPP variable that is supposed to give the correct invocation of the C preprocessor. With gcc(1) the correct C preprocessor is `gcc -E` but with clang(1) it is `clang-cpp`. The port seems to mistakenly set CPP to `clang -E` even with CPP set to `clang-cpp` in my make.conf(5). See my comment in the PR.


----------

