views:

1800

answers:

3

How to make an Objective-C project work on Ubuntu?

My files are:

Fraction.h

    #import <Foundation/NSObject.h>

    @interface Fraction: NSObject {
        int numerator;
        int denominator;
    }

    -(void) print;
    -(void) setNumerator: (int) n;
    -(void) setDenominator: (int) d;
    -(int) numerator;
    -(int) denominator;
    @end

Fraction.m

    #import "Fraction.h"
    #import <stdio.h>

    @implementation Fraction
    -(void) print {
        printf( "%i/%i", numerator, denominator );
    }

    -(void) setNumerator: (int) n {
        numerator = n;
    }

    -(void) setDenominator: (int) d {
        denominator = d;
    }

    -(int) denominator {
        return denominator;
    }

    -(int) numerator {
        return numerator;
    }
    @end

main.m

    #import <stdio.h>
    #import "Fraction.h"

    int main( int argc, const char *argv[] ) {
        // create a new instance
        Fraction *frac = [[Fraction alloc] init];

        // set the values
        [frac setNumerator: 1];
        [frac setDenominator: 3];

        // print it
        printf( "The fraction is: " );
        [frac print];
        printf( "\n" );

        // free memory
        [frac release];

        return 0;
    }

I've tried two approaches to compile it:

  1. Pure gcc:

    $ sudo apt-get install gobjc gnustep gnustep-devel
    $ gcc `gnustep-config --objc-flags` -o main main.m -lobjc -lgnustep-base
    /tmp/ccIQKhfH.o:(.data.rel+0x0): undefined reference to `__objc_class_name_Fraction'
    
  2. I created a GNUmakefile Makefile:

    include ${GNUSTEP_MAKEFILES}/common.make
    
    
    TOOL_NAME = main
    main_OBJC_FILES = main.m
    
    
    include ${GNUSTEP_MAKEFILES}/tool.make
    

    ... and ran:

    $ source /usr/share/GNUstep/Makefiles/GNUstep.sh
    $ make
    Making all for tool main...
     Linking tool main ...
    ./obj/main.o:(.data.rel+0x0): undefined reference to `__objc_class_name_Fraction'
    

So in both cases compiler gets stuck at

    undefined reference to `__objc_class_name_Fraction'

Do you have and idea how to resolve this issue?

+4  A: 

It's right. In both cases you did not include Fraction.m in your list of files to be compiled, so it can't find the implementation of the class Fraction

newacct
actually it i a linker error. newacct is right.Im not sure about the <Foundation/NSObject.h> include, too. I think this only provided on MacOS not in GNUStep.
Johannes Rudolph
@Johannes Rudolphp: I changed main.m to *.m and it worked!
Alex
+3  A: 

I am not an expert at writing the make files like that, I find simply typing the following works on ubuntu quite well:

gcc -I /usr/include/GNUstep/ -I /usr/include/mysql -L /usr/lib/GNUstep/\
 -lgnustep-base -lmysqlclient\
 -g -ggdb\
 -fconstant-string-class=NSConstantString -o test *.m

I am using it on this project:

http://github.com/uptecs/SmsgateDelivery/

If the above GCC command does not work you have not installed enough packages, use apt-cache to search for more gcc and objective c packages to install (I just installed more packages that looked relevant at random until it worked)

Jacob
+1  A: 

The approach I just got working was (in Ubuntu, which is closely related to Debian):

  • Use Synaptic to install all likely-looking GnuStep packages;
  • Source ( . ) the GnuStep startup script, /usr/share/GNUstep/Makefiles/GNUstep.sh (this can go into .profile or .bashrc or something so you don't have to do it manually every time)
  • Create a GNUmakefile according to the instructions in A First Tool

This allowed me to successfully build command line programs.

Carl Smotricz