How to compile Objective-C in FreeBSD

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:

Code:
#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!
 
I tried [cmd=]cc -c example.m -Objc[/cmd] but it errors saying
Code:
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:

Code:
/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.
 
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.
 
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:
Code:
WITH_CLANG_EXTRAS=YES
CC=clang
CXX=clang++
CPP=clang-cpp

I have successfully run:
- make buildworld
- make buildkernel
- make installkernel
- make installworld
 
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).
 
Last edited by a moderator:
/etc/make.conf
Code:
# 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:

Code:
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:
Code:
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?
 
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:
Code:
# 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++
 
GNUstep doesn't make

Hi,

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

Code:
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:
Code:
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:

The closest that I have come to is through the following set of steps:
  1. I installed FreeBSD amd x64 from scratch on a Zotac ZBox Nano XS computer
  2. Installed ports
    Code:
    # portsnap fetch
    # portsnap extract
    # portsnap update
  3. I modifed the file /etc/src.conf with the following:
    Code:
    WITH_CLANG_EXTRAS=YES
    CC=clang
    CXX=clang++
    CPP=clang-cpp
  4. Now I built the world:
    Code:
    # make buildworld
  5. Built kernel:
    Code:
    # make buildkernel
  6. Installed kernel:
    Code:
    # make installkernel[
    # mergemaster -p
  7. Install world:
    Code:
    # make installworld
    # mergemaster
    # reboot
  8. Modified the /etc/make.conf file so it looks like:
    Code:
    # 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++
  9. I then went to proceed in installing GNUstep:
    Code:
    # cd /usr/ports/devel/gnustep
    # make -DBATCH install clean
    Note: -DBATCH just makes the install accept all the default packages.
  10. 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
 
Code:
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:
Code:
# 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.
 
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:

Code:
===>  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).
 

Attachments

  • config.txt
    9.4 KB · Views: 229
I changed my /etc/make.conf file to look like this and I re-ran the install successfully for the devel/libtool:
Code:
# 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:
Code:
# 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:
Code:
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.
 
Please, check for the compiler version:
Code:
[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:
Code:
CC=/usr/local/bin/clang
CXX=/usr/local/bin/clang++

Take a look for better customization to /usr/ports/Mk/bsd.gnustep.mk.
 
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.
 
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:
Code:
#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:
Code:
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
 
Back
Top