1

So I have this piece of code...

/**
 * cdb.m
 * Copyright (C) 2013 Naveen Mathew. All rights reserved.
 */

#import <objc/Object.h>
#import "cdb.h"
#import <stdio.h>
#import <stdlib.h>

@implementation CDB : Object
{

}

- (int) main {
    printf("Hello world");
    return 0;
}

@end

int main(void)
{
    CDB *myNumber = [CDB new]; // equal to [[Number alloc] init]
    [myNumber main];

    return 0;
}

and I want to compile it in Ubuntu 13.04 but without all the crap that GNUStep gives me. So I use the GNU Objective C runtime (gobjc) but when I compile I get the following...

clang -Wall -lobjc -o cdb cdb.m -I/usr/lib/gcc/x86_64-linux-gnu/4.7/include
cdb.m:25:21: warning: class method '+new' not found (return type defaults to
      'id') [-Wobjc-method-access]
        CDB *myNumber = [CDB new]; // equal to [[Number alloc] init]
                        ^    ~~~
1 warning generated.

and when I run the program I get a segmentation fault... I'm using gobjc 4.7. I tried it with gobjc 4.6... it compiles but I still get a segmentation fault...

  • What's the libobjc you're using? I believe GNUStep's root class is `NSObject`, not `Object`. – jscs Aug 04 '13 at 00:00
  • I'm using the the GNU Objective C library. It's gobjc 4.7... The previous versions had the new method but they deprecated it in 4.7 and I have no idea how to instantiate methods now... – Naveen Mathew Aug 04 '13 at 01:49
  • You can use `alloc`/`init` like it says in the comment. – jscs Aug 04 '13 at 01:53
  • Tried `alloc`, `init`, and `new` but when i compile it says `class method not found` and when i try running the program, i get a segmentation fault... – Naveen Mathew Aug 04 '13 at 02:08
  • Have you tried inheriting from `NSObject`? – jscs Aug 04 '13 at 02:09
  • That's the thing... I'm not using GNUStep... I'm using gobjc provided by GCC... – Naveen Mathew Aug 04 '13 at 02:11
  • As far as I can tell, gobjc is just the compiler, and the GNU ObjC runtime -- libobjc2 -- is part of GNUStep. – jscs Aug 04 '13 at 02:17
  • I followed this guide to install gobjc... Thing is the new gobjc doesn't have new soo yeah... http://ubuntuforums.org/showthread.php?t=1064045 – Naveen Mathew Aug 04 '13 at 02:58
  • There are many versions of objective c runtime. You should use GNUstep runtime if you want Objective-C 2.0 though I don't think it would work without GNUstep, like those things like literals or fast enumeration that rely on NS-protocols. Seriously, you are wasting your time trying to do things without GNUstep, at this level of experience you seem to have. – Fred Frith-MacDonald Aug 04 '13 at 19:18
  • 1
    I notice you're using clang as compiler but with gcc's header files and libraries. That might be a problem (although I hope someone with more knowledge can say for sure). – echristopherson Aug 06 '13 at 17:30

2 Answers2

3

I think some ancient runtime has +new and friends implemented. For newer runtime like gnustep-runtime including one came with GCC which is very different, I think, you must implement your own craps using category or whatsoever. You can just cut & paste GNUstep's implementation of NSObject but it could be too tricky for you as it does things like prefixing the malloc with retain counter et al, else you may want to implement your own way to maintain ref counting like using hash map or anything. You may also try alternate framework like ObjFW if GNUstep-base alone has too much craps for you.

2

+(id)new is a function of the NSObject class. However, you are subclassing a runtime object. To use most of the Apple methods you're used to using in OS X, you'll need to subclass NSObject instead.

Also, you declare an object's superclass in the interface, not the implementation. You need to change @implementation CDB : NSObject to @implementation CDB, then, in your header file, place @interface CDB : NSObject { ...

eswick
  • 519
  • 4
  • 16
  • It would work if I was on OS X but I am on Ubuntu Linux and I'm not using Apple's Objective-C runtime... I'm using the GNU Objective-C runtime (gobjc 4.7) – Naveen Mathew Aug 04 '13 at 01:50
  • 2
    Oh, I totally realize that. You can still use the Foundation framework while using the GNU runtime. It should be available. There is a reference you can use here: http://blog.lyxite.com/2008/01/compile-objective-c-programs-using-gcc.html – eswick Aug 04 '13 at 14:27