# How to compile Objective-C in FreeBSD



## torr_from_fallout2 (Mar 29, 2013)

Hi-yah guys!

I am currently setting out a journey of *Objective-C *programming in *FreeBSD* and I need some help. How do I compile a simple program with the new Clang compiler? I have the following:


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

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

    NSLog (@"First ObjC Program");

    return 0;
}
```

I found a great link for accomplishing my task in GCC / OpenGNU: http://www.freebsdonline.com/content/blogsection/21/550/ 

But I would like to use Clang. I have searched https://wiki.freebsd.org but no luck. My apologies for a n00b question but may someone please help?

Thank you!


----------



## fonz (Mar 29, 2013)

What have you tried so far? Does that include using the -ObjC flag?


----------



## torr_from_fallout2 (Mar 29, 2013)

I tried [cmd=]cc -c example.m -Objc[/cmd] but it errors saying 
	
	



```
cc: example.m: Objective-C compiler not installed on this system
```

I then tried [cmd=]cd /usr/ports/lang/objc ; make install clean[/cmd]. However the process gets terminated with an error: 


```
/usr/include/stdlib.h:82: fatal: syntax error "void"
*** [Block.o] Error code 1

Stop in /usr/ports/lang/objc/work/objc-3.2.11/src/objcrt.
*** [objcrt] Error code 1
```

etc.


----------



## cpm@ (Mar 29, 2013)

Note that lang/objc is marked as BROKEN: fails to build.


----------



## jackp (Mar 30, 2013)

What version of FreeBSD are you using? clang is the default in FreeBSD 10, but earlier versions still use gcc by default -- so using 'cc' will call gcc (and perhaps it's missing whatever you need for Objective-C).

I think FreeBSD 9.0 or 9.1 shipped with clang (though again, it's not the default compiler) -- try using 'clang' instead of 'cc' and see if that works. If not, you can build and install llvm and clang from the ports tree.


----------



## torr_from_fallout2 (Mar 31, 2013)

Hi,

I'm using FreeBSD 9.1 and x64. I am trying to make GNUstep from ports but I'm getting the exact same issue as in http://forums.freebsd.org/showthread.php?t=28027.

I currently have the following installed from ports:
- installed clang / llvm
- libobjc2

I modified the /etc/src.conf to have:

```
WITH_CLANG_EXTRAS=YES
CC=clang
CXX=clang++
CPP=clang-cpp
```

I have successfully run:
- `make buildworld`
- `make buildkernel`
- `make installkernel`
- `make installworld`


----------



## jackp (Mar 31, 2013)

I was looking at @dinoex's solution here:

http://forums.freebsd.org/showthread.php?p=167049#post167049

and noticed that you've edited /etc/src.conf -- this file controls the options for building the FreeBSD source tree (e.g., the kernel). The "normal" compilation options are set in /etc/make.conf, which is what @dinoex's solution uses. (It's fine to leave your edits to /etc/src.conf, you just need to add the changes to /etc/make.conf).


----------



## cpm@ (Mar 31, 2013)

Please, show your /etc/make.conf


----------



## torr_from_fallout2 (Apr 2, 2013)

/etc/make.conf

```
# added by use.perl 2013-03-30 14:50:35
PERL_VERSION=5.14.2

WITH_GNUSTEP_DEVEL=yes
GNUSTEP_WITH_LIBOBJC2=yes
CC?=clang
CXX?=clang++
```

So when I run [cmd=]cd /usr/ports/devel/gnustep && make install clean[/cmd] I get the following error:


```
configure: exit 1
(end of "config.log")
*** [do-configure] Error code 1

Stop in /usr/ports/lang/gnustep-base.
*** [build-depends] Error code 1

Stop in /usr/ports/devel/gnustep.
```

Ok, so I got into /usr/ports/lang/gnustep-base and run the whole `make` and `install` and I then get the following error:

```
configure: exit 1
(end of "config.log")
*** [do-configure] Error code 1

Stop in /usr/ports/lang/gnustep-base.
```

I'm at a loss on what to do next. Any hints?


----------



## cpm@ (Apr 2, 2013)

Well, I think you maybe be interested to read the following tutorial:
http://brilliantobjc.blogspot.com.es/2012/12/cocoa-on-freebsd.html.

**EDIT**

Make the following changes in your make.conf:

```
# added by use.perl 2013-03-30 14:50:35
PERL_VERSION=5.14.2

WITH_CLANG=yes
WITH_CLANG_EXTRAS=yes
GNUSTEP_WITH_CLANG=yes 
WITH_GNUSTEP_DEVEL=yes 
GNUSTEP_WITH_LIBOBJC2=yes 

CC=/usr/local/bin/clang
CXX=/usr/local/bin/clang++
```


----------



## torr_from_fallout2 (Apr 7, 2013)

*GNUstep doesn't make*

Hi,

I tried that and GNUstep didn't install. Here is the error:


```
checking for gcc... /usr/local/libexec/world/clang
checking for C compiler default output file name... 
configure: error: in `/usr/ports/devel/binutils/work/binutils-2.22':
configure: error: C compiler cannot create executables
See `config.log' for more details.
===>  Script "configure" failed unexpectedly.
Please report the problem to zeising@FreeBSD.org [maintainer] and attach the
"/usr/ports/devel/binutils/work/binutils-2.22/config.log" including the output
of the failure of your make command. Also, it might be a good idea to provide
an overview of all packages installed on your system (e.g. a /usr/sbin/pkg_info
-Ea).
*** [do-configure] Error code 1

Stop in /usr/ports/devel/binutils.
*** [build-depends] Error code 1

Stop in /usr/ports/lang/libobjc2.
*** [lib-depends] Error code 1

Stop in /usr/ports/devel/gnustep-make.
*** [build-depends] Error code 1

Stop in /usr/ports/lang/gnustep-base.
*** [build-depends] Error code 1

Stop in /usr/ports/devel/gnustep.
```

I ran the recommended command I received the following packages list:

```
bison-2.5.1,1       A parser generator from FSF, (mostly) compatible with Yacc
gettext-0.18.1.1    GNU gettext package
gmake-3.82_1        GNU version of 'make' utility
gmp-5.0.5           A free library for arbitrary precision arithmetic
libiconv-1.14       A character set conversion library
libtool-2.4.2       Generic shared library support script
m4-1.4.16_1,1       GNU m4
mpfr-3.1.1          A library for multiple-precision floating-point computation
perl-5.14.2_2       Practical Extraction and Report Language
```

I have tried the following links and I was unsuccessful every time:

http://www.freebsdonline.com/content/blogsection/21/550/
http://etoileos.com/downloads/installrelease/freebsd/
http://brilliantobjc.blogspot.com.es/2012/12/cocoa-on-freebsd.html

The closest that I have come to is through the following set of steps:

I installed FreeBSD amd x64 from scratch on a Zotac ZBox Nano XS computer

Installed ports

```
# portsnap fetch
# portsnap extract
# portsnap update
```

I modifed the file /etc/src.conf with the following:

```
WITH_CLANG_EXTRAS=YES
CC=clang
CXX=clang++
CPP=clang-cpp
```

Now I built the world:

```
# make buildworld
```

Built kernel:

```
# make buildkernel
```

Installed kernel:

```
# make installkernel[
# mergemaster -p
```

Install world:

```
# make installworld
# mergemaster
# reboot
```

Modified the /etc/make.conf file so it looks like:

```
# added by use.perl 2013-03-30 14:50:35
PERL_VERSION=5.14.2

WITH_CLANG=yes
WITH_CLANG_EXTRAS=yes
GNUSTEP_WITH_CLANG=yes 
WITH_GNUSTEP_DEVEL=yes 
GNUSTEP_WITH_LIBOBJC2=yes 

CC=/usr/local/libexec/world/clang
CXX=/usr/local/libexec/world/clang++
```

I then went to proceed in installing GNUstep:

```
# cd /usr/ports/devel/gnustep
# make -DBATCH install clean
```
Note: -DBATCH just makes the install accept all the default packages.

Unfortunately, the process terminates with the mentioned error. I have been spending this whole week after work working on figuring this out with no success. I kindly request some help / submit a bug report with the ports admin for GNUstep. I just want my objective-c compiler working. :e


----------



## cpm@ (Apr 7, 2013)

```
checking for gcc... /usr/local/libexec/world/clang
checking for C compiler default output file name... 
configure: error: in `/usr/ports/devel/binutils/work/binutils-2.22':
configure: error: C compiler cannot create executables
<snip>
```

For build with Clang add in /etc/make.conf the following lines:

```
# added by use.perl 2013-03-30 14:50:35
PERL_VERSION=5.14.2

WITH_CLANG=yes
WITH_CLANG_EXTRAS=yes
[B]WITH_CLANG_IS_CC=yes
WITHOUT_GCC=yes[/B]
GNUSTEP_WITH_CLANG=yes 
WITH_GNUSTEP_DEVEL=yes 
GNUSTEP_WITH_LIBOBJC2=yes 

CC=/usr/local/bin/clang
CXX=/usr/local/bin/clang++
```

Must also reinstall devel/libtool, in order to discard such part of the problem.

Show the config.log, if it still does not work.


----------



## torr_from_fallout2 (Apr 8, 2013)

Thanks,

I made the changes to the /etc/make.conf  file with what you described and I went to /usr/ports/devel/libtool and I entered [cmd=]make reinstall[/cmd] and I get the following error:


```
===>  Configuring for libtool-2.4.2
## ------------------------- ##
## Configuring libtool 2.4.2 ##
## ------------------------- ##

checking for a BSD-compatible install... /usr/bin/install -c -o root -g wheel
checking whether build environment is sane... yes
checking for a thread-safe mkdir -p... libltdl/config/install-sh -c -d
checking for gawk... no
checking for mawk... no
checking for nawk... nawk
checking whether make sets $(MAKE)... yes
checking build system type... amd64-portbld-freebsd9.1
checking host system type... amd64-portbld-freebsd9.1
configure: autobuild project... GNU Libtool
configure: autobuild revision... 2.4.2 ()
configure: autobuild hostname... drone
configure: autobuild mode... default
configure: autobuild timestamp... 20130409T021629Z
checking for gcc... /usr/local/libexec/world/clang
checking whether the C compiler works... no
configure: error: in `/usr/ports/devel/libtool/work/libtool-2.4.2':
configure: error: C compiler cannot create executables
See `config.log' for more details
===>  Script "configure" failed unexpectedly.
Please report the problem to autotools@FreeBSD.org [maintainer] and attach
the "/usr/ports/devel/libtool/work/libtool-2.4.2/config.log" including the
output of the failure of your make command. Also, it might be a good idea to
provide an overview of all packages installed on your system (e.g. a
/usr/sbin/pkg_info -Ea).
*** [do-configure] Error code 1

Stop in /usr/ports/devel/libtool.
*** [reinstall] Error code 1

Stop in /usr/ports/devel/libtool.
```

Here is the output for the file /usr/ports/devel/libtool/work/libtool-2.4.2/config.log: (See attached).


----------



## torr_from_fallout2 (Apr 9, 2013)

I changed my /etc/make.conf  file to look like this and I re-ran the install successfully for the devel/libtool:

```
# added by use.perl 2013-04-05 23:15:45
PERL_VERSION=5.14.2

WITH_CLANG=yes
WITH_CLANG_EXTRAS=yes
WITH_CLANG_IS_CC=yes
WITH_CLANG_IS_CXX=yes
WITHOUT_GCC=yes
GNUSTEP_WITH_CLANG=yes
WITH_GNUSTEP_DEVEL=yes
GNUSTEP_WITH_LIBOBJC2=yes

CC?=clang
CXX?=clang++
```

I then proceeded to devel/gnu and ran the following command:

```
# make -DBATCH install clean CC=clang CXX=clang++
```

It appeared like GNUstep was running for a while and then it exited with the following error again:

```
configure: exit 1
(end of "config.log")
*** [do-configure] Error code 1

Stop in /usr/ports/lang/gnustep-base.
*** [build-depends] Error code 1

Stop in /usr/ports/devel/gnustep.
```


----------



## cpm@ (Apr 9, 2013)

Please, check for the compiler version:

```
[CMD="%"]/usr/local/bin/clang -v[/CMD]
clang version 3.2 (tags/RELEASE_32/final)
Target: i386-portbld-freebsd9.1
Thread model: posix
```

`% clang -v` tells you FreeBSD clang version.

You should set path for environment variables point to latest Clang version in /etc/make.conf: 

```
CC=/usr/local/bin/clang
CXX=/usr/local/bin/clang++
```

Take a look for better customization to /usr/ports/Mk/bsd.gnustep.mk.


----------



## torr_from_fallout2 (Apr 11, 2013)

No luck still. Same errors. GNUstep doesn't work. 

I just want to compile objective-c code, absolutely no GUI stuff... is there any alternative ways? Do I have to use devel/GNUstep? Can't Clang/LLVM compile objective-c? Will FreeBSD 10.0 have a working objective-c compiler? I guess I might have to wait until version 10.0 comes out.


----------



## cpm@ (Apr 11, 2013)

This post confirms that libobjc2 SVN version works on FreeBSD 9.1 with Clang 3.2.


----------



## torr_from_fallout2 (Apr 11, 2013)

cpu82 said:
			
		

> This post confirms that libobjc2 SVN version works on FreeBSD 9.1 with Clang 3.2.



Yup, I confirm, lang/libobjc2 works, I successfully installed it. I believe I have the following version: libobjc2-1.6 afterwords.


----------



## cpm@ (Apr 11, 2013)

Well, then install GNUstep SVN: http://wiki.gnustep.org/index.php/GNUstep_SVN_installation_guide.

Seems that the problem reported is still open. Take a look to ports/172361.


----------



## torr_from_fallout2 (May 4, 2013)

*Instructions for Compiling in Objective-C on FreeBSD 9.1*

Howdy,

I figured it out! Thanks for all the help though cpu82. :beer

If you would like to compile Objective-C code, here is how to do it. Note: I'm assuming you have a clean before proceeding.

1) Update ports
`# portsnap fetch`
`# portsnap extract`
`# portsnap 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) Install GNUstep:
`# portupgrade -N --batch gnustep`

(Note: The 'batch' works the same way with the portupgrade application)

4) Confirm code works. Note: Special thanks to this link:
`# cd ~/`
`# mkdir testdir`
`# cd testdir`
`# 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! :stud


----------



## cneira (Jul 14, 2013)

Thanks this worked like a charm! 

Bests


----------

