Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling Objective-C project on Linux (Ubuntu)

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?

like image 246
Alex Avatar asked Oct 13 '09 06:10

Alex


People also ask

Can Objective-C run on Linux?

The best platform for developing Objective‑C is Mac OS. But Objective‑C programs can also be compiled and run on Windows or Linux by using GNUstep and an Objective‑C compiler.

Does Ubuntu have C compiler?

It's present in all Linux/Unix distributions. gcc(GNU Compiler Collection) is one of the most widely used C compilers . Ubuntu uses gcc and is installed by default when you install it on your system. Type gcc <filename> and g++ filename on the terminal to compile C and C++ programs respectively.

Can GCC compile Objective-C?

These front ends, like that for C++, are built in subdirectories of GCC and link to it. The result is an integrated compiler that can compile programs written in C, C++, Objective-C, or any of the languages for which you have installed front ends.


2 Answers

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

From the comment, this command works

gcc `gnustep-config --objc-flags` -o main *.m -lobjc -lgnustep-base
like image 191
newacct Avatar answered Oct 12 '22 14:10

newacct


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)

like image 28
Jay Avatar answered Oct 12 '22 14:10

Jay