how to compile objective-c++

I am teaching myself objective-c and I want to test objective-c++. I compiled objective-c using /usr/local/GNUstep/System/Library/Makefiles/GNUstep.sh and after

- with clang

Code:
clang -ObjC hello.m -o hello  `gnustep-config --objc-flags`
 -I/usr/local/lib/gcc42/gcc/x86_64-portbld-freebsd9.0/4.2.5/include
 -fconstant-string-class=NSConstantString  -I/usr/local/GNUstep/System/Library/Headers
 -L/usr/local/GNUstep/System/Library/Libraries -L/usr/local/lib -lgnustep-base

- with gcc

Code:
gcc42 hello.m -o hello  `gnustep-config --objc-flags`
 -I/usr/local/lib/gcc42/gcc/x86_64-portbld-freebsd9.0/4.2.5/include
 -fconstant-string-class=NSConstantString  -I/usr/local/GNUstep/System/Library/Headers
 -L/usr/local/GNUstep/System/Library/Libraries -L/usr/local/lib -lgnustep-base

When I try to compile objective c++
Code:
I
get this error.

I am using this example:
https://gist.github.com/752184

Code:
#include <iostream>
#import <Foundation/Foundation.h>

class MyCppNSStringWrapper
{
public:
  MyCppNSStringWrapper(const char* str);
  virtual ~MyCppNSStringWrapper();
  unsigned int length() const;
  short characterAtIndex(unsigned int index) const;
  const char* UTF8String() const;
private:
  NSString* _internal;
};

MyCppNSStringWrapper::MyCppNSStringWrapper(const char* str)
{
  _internal = [[NSString alloc] initWithCString:str
                                       encoding:NSUTF8StringEncoding];
}

MyCppNSStringWrapper::~MyCppNSStringWrapper()
{
  [_internal release];
  _internal = nil;
}

unsigned int MyCppNSStringWrapper::length() const
{
  return [_internal length];
}

short MyCppNSStringWrapper::characterAtIndex(unsigned int index) const
{
  return [_internal characterAtIndex:index];
}

const char* MyCppNSStringWrapper::UTF8String() const
{
  return [_internal UTF8String];
}

extern "C" int main(int argc, char** argv)
{
  NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  
  MyCppNSStringWrapper* s = new MyCppNSStringWrapper("Hello World!");
  
  std::cout << "The string length is " << s->length() << std::endl;
  std::cout << "The string third char value is " << s->characterAtIndex(2) << std::endl;
  std::cout << "The string is " << s->UTF8String() << std::endl;

  [pool release];
  return 0;
}

- with clang

Code:
clang -ObjC++ 1.mm -o 1  `gnustep-config --objc-flags`
 -I/usr/local/lib/gcc42/gcc/x86_64-portbld-freebsd9.0/4.2.5/include -fconstant-string-class=NSConstantString
 -I/usr/local/GNUstep/System/Library/Headers -L/usr/local/GNUstep/System/Library/Libraries -L/usr/local/lib
 -lgnustep-base

- error

Code:
In file included from 1.mm:8:
In file included from /usr/local/GNUstep/System/Library/Headers/Foundation/Foundation.h:33:
In file included from /usr/local/GNUstep/System/Library/Headers/Foundation/FoundationErrors.h:29:
In file included from /usr/local/GNUstep/System/Library/Headers/Foundation/NSObject.h:30:
In file included from /usr/local/GNUstep/System/Library/Headers/Foundation/NSObjCRuntime.h:95:
In file included from /usr/local/GNUstep/System/Library/Headers/GNUstepBase/GSObjCRuntime.h:62:
/usr/local/lib/gcc42/gcc/x86_64-portbld-freebsd9.0/4.2.5/include/objc/objc-api.h:488:10: error: cannot initialize return object of type 'MetaClass'
      (aka 'objc_class *') with an rvalue of type 'Class'
  return CLS_ISCLASS(_class)?_class->class_pointer:Nil;
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/local/lib/gcc42/gcc/x86_64-portbld-freebsd9.0/4.2.5/include/objc/objc-api.h:298:26: note: expanded from:
#define CLS_ISCLASS(cls) ((cls)&&__CLS_ISINFO(cls, _CLS_CLASS))
                         ^
/usr/local/lib/gcc42/gcc/x86_64-portbld-freebsd9.0/4.2.5/include/objc/objc-api.h:576:10: error: cannot initialize return object of type 'MetaClass'
      (aka 'objc_class *') with an rvalue of type 'Class'
  return ((object!=nil)?(CLS_ISCLASS(object->class_pointer)
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from 1.mm:8:
In file included from /usr/local/GNUstep/System/Library/Headers/Foundation/Foundation.h:33:
In file included from /usr/local/GNUstep/System/Library/Headers/Foundation/FoundationErrors.h:29:
In file included from /usr/local/GNUstep/System/Library/Headers/Foundation/NSObject.h:30:
/usr/local/GNUstep/System/Library/Headers/Foundation/NSObjCRuntime.h:147:20: error: use of undeclared identifier 'INTPTR_MAX'
enum {NSNotFound = NSIntegerMax};
                   ^
/usr/local/GNUstep/System/Library/Headers/Foundation/NSObjCRuntime.h:45:24: note: expanded from:
#       define NSIntegerMax  INTPTR_MAX
                             ^
1.mm:13:1: error: expected member name or ';' after declaration specifiers
����MyCppNSStringWrapper(const char* str);
^
1.mm:14:1: error: expected member name or ';' after declaration specifiers
����virtual ~MyCppNSStringWrapper();
^
1.mm:15:1: error: expected member name or ';' after declaration specifiers
����unsigned int length() const;
^
1.mm:16:1: error: expected member name or ';' after declaration specifiers
����short characterAtIndex(unsigned int index) const;
^
1.mm:17:1: error: expected member name or ';' after declaration specifiers
����const char* UTF8String() const;
^
1.mm:19:1: error: expected member name or ';' after declaration specifiers
����NSString* _internal;
^
1.mm:22:23: error: out-of-line definition of 'MyCppNSStringWrapper' does not match any declaration in 'MyCppNSStringWrapper'
MyCppNSStringWrapper::MyCppNSStringWrapper(const char* str)
                      ^~~~~~~~~~~~~~~~~~~~
1.mm:24:1: error: expected expression
����_internal = [[NSString alloc] initWithCString:str
^
1.mm:28:23: error: definition of implicitly declared destructor
MyCppNSStringWrapper::~MyCppNSStringWrapper()
                      ^
1.mm:30:1: error: expected expression
����[_internal release];
^
1.mm:31:1: error: expected expression
����_internal = nil;
^
1.mm:34:36: error: out-of-line definition of 'length' does not match any declaration in 'MyCppNSStringWrapper'
unsigned int MyCppNSStringWrapper::length() const
                                   ^~~~~~
1.mm:36:1: error: expected expression
����return [_internal length];
^
1.mm:39:29: error: out-of-line definition of 'characterAtIndex' does not match any declaration in 'MyCppNSStringWrapper'
short MyCppNSStringWrapper::characterAtIndex(unsigned int index) const
                            ^~~~~~~~~~~~~~~~
1.mm:41:1: error: expected expression
����return [_internal characterAtIndex:index];
^
1.mm:44:35: error: out-of-line definition of 'UTF8String' does not match any declaration in 'MyCppNSStringWrapper'
const char* MyCppNSStringWrapper::UTF8String() const
                                  ^~~~~~~~~~
fatal error: too many errors emitted, stopping now [-ferror-limit=]
20 errors generated.

- with gcc

Code:
gcc42 1.mm -o 1  `gnustep-config --objc-flags` -I/usr/local/lib/gcc42/gcc/x86_64-portbld-freebsd9.0/4.2.5/include
 -fconstant-string-class=NSConstantString  -I/usr/local/GNUstep/System/Library/Headers
 -L/usr/local/GNUstep/System/Library/Libraries -L/usr/local/lib -lgnustep-base

- error
Code:
gcc42: 1.mm: Objective-C++ compiler not installed on this system

Any idea?
 
Add
Code:
GNUSTEP_WITH_CLANG=yes
to your make.conf and rebuild GNUstep, to use clang backend libs.

Or try to use a higher version of the GCC port because your 4.2 does not seem to have Objective-C++ enabled.
 
expl said:
Add
Code:
GNUSTEP_WITH_CLANG=yes
to your make.conf and rebuild GNUstep, to use clang backend libs.

Or try to use a higher version of the GCC port because your 4.2 does not seem to have Objective-C++ enabled.

Thanks for your answer.

I've tried with gcc47 but the same error:

Code:
gcc47: error: main.mm: Objective-C++ compiler not installed on this system

Now I am using a more simple example:

Code:
#import <Foundation/Foundation.h>
#include <iostream>

int main(int argc, const char * argv[])
{
    
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];	
        
        // insert code here...
        NSLog(@"Hello, World!");
        std::cout << "hello" << std::endl;
        
[pool drain];
    return 0;
}

I will recompile the kernel using
Code:
GNUSTEP_WITH_CLANG=yes
.
 
I've rebuilt the kernel with
Code:
GNUSTEP_WITH_CLANG=yes
And still the same error, I will try compiling gcc from source using
Code:
 ./configure --enable-languages=obj-c++

I also tried with clang 3.1 but the same error. I am also thinking on installing Mac ports and trying to compile gcc from there.
 
expl said:
Why would you recompile the kernel? You should only recompile the lang/gnustep-base port.

Thanks for your answer.

I've deleted devel/gnustep using pkg_remove and now I Can't compile GNUstep, I get this error with lang/gnustep-base and devel/gnustep.

Code:
## ----------- ##
## confdefs.h. ##
## ----------- ##

#define PACKAGE_NAME ""
#define PACKAGE_TARNAME ""
#define PACKAGE_VERSION ""
#define PACKAGE_STRING ""
#define PACKAGE_BUGREPORT ""
#define OPTION_NO_ENVIRONMENT 0
#define GNUSTEP_TARGET_MAKEFILES "/usr/local/GNUstep/System/Library/Makefiles"
#define GNUSTEP_TARGET_USER_DEFAULTS_DIR "GNUstep/Defaults"
#define GNUSTEP_TARGET_USER_CONFIG_FILE ".GNUstep.conf"
#define GNUSTEP_TARGET_CONFIG_FILE "/usr/local/GNUstep/GNUstep.conf"
#define GNUSTEP_TARGET_SYSTEM_APPS "/usr/local/GNUstep/System/Applications"
#define GNUSTEP_TARGET_SYSTEM_ADMIN_APPS "/usr/local/GNUstep/System/Applications/Admin"
#define GNUSTEP_TARGET_SYSTEM_WEB_APPS "/usr/local/GNUstep/System/Library/WebApplications"
#define GNUSTEP_TARGET_SYSTEM_TOOLS "/usr/local/GNUstep/System/Tools"
#define GNUSTEP_TARGET_SYSTEM_ADMIN_TOOLS "/usr/local/GNUstep/System/Tools/Admin"
#define GNUSTEP_TARGET_SYSTEM_LIBRARY "/usr/local/GNUstep/System/Library"
#define GNUSTEP_TARGET_SYSTEM_LIBRARIES "/usr/local/GNUstep/System/Library/Libraries"
#define GNUSTEP_TARGET_SYSTEM_HEADERS "/usr/local/GNUstep/System/Library/Headers"
#define GNUSTEP_TARGET_SYSTEM_DOC "/usr/local/GNUstep/System/Library/Documentation"
#define GNUSTEP_TARGET_SYSTEM_DOC_MAN "/usr/local/GNUstep/System/Library/Documentation/man"
#define GNUSTEP_TARGET_SYSTEM_DOC_INFO "/usr/local/GNUstep/System/Library/Documentation/info"
#define GNUSTEP_TARGET_NETWORK_APPS "/usr/local/GNUstep/Local/Applications"
#define GNUSTEP_TARGET_NETWORK_ADMIN_APPS "/usr/local/GNUstep/Local/Applications/Admin"
#define GNUSTEP_TARGET_NETWORK_WEB_APPS "/usr/local/GNUstep/Local/Library/WebApplications"
#define GNUSTEP_TARGET_NETWORK_TOOLS "/usr/local/GNUstep/Local/Tools"
#define GNUSTEP_TARGET_NETWORK_ADMIN_TOOLS "/usr/local/GNUstep/Local/Tools/Admin"
#define GNUSTEP_TARGET_NETWORK_LIBRARY "/usr/local/GNUstep/Local/Library"
#define GNUSTEP_TARGET_NETWORK_LIBRARIES "/usr/local/GNUstep/Local/Library/Libraries"
#define GNUSTEP_TARGET_NETWORK_HEADERS "/usr/local/GNUstep/Local/Library/Headers"
#define GNUSTEP_TARGET_NETWORK_DOC "/usr/local/GNUstep/Local/Library/Documentation"
#define GNUSTEP_TARGET_NETWORK_DOC_MAN "/usr/local/GNUstep/Local/Library/Documentation/man"
#define GNUSTEP_TARGET_NETWORK_DOC_INFO "/usr/local/GNUstep/Local/Library/Documentation/info"
#define GNUSTEP_TARGET_LOCAL_APPS "/usr/local/GNUstep/Local/Applications"
#define GNUSTEP_TARGET_LOCAL_ADMIN_APPS "/usr/local/GNUstep/Local/Applications/Admin"
#define GNUSTEP_TARGET_LOCAL_WEB_APPS "/usr/local/GNUstep/Local/Library/WebApplications"
#define GNUSTEP_TARGET_LOCAL_TOOLS "/usr/local/GNUstep/Local/Tools"
#define GNUSTEP_TARGET_LOCAL_ADMIN_TOOLS "/usr/local/GNUstep/Local/Tools/Admin"
#define GNUSTEP_TARGET_LOCAL_LIBRARY "/usr/local/GNUstep/Local/Library"
#define GNUSTEP_TARGET_LOCAL_LIBRARIES "/usr/local/GNUstep/Local/Library/Libraries"
#define GNUSTEP_TARGET_LOCAL_HEADERS "/usr/local/GNUstep/Local/Library/Headers"
#define GNUSTEP_TARGET_LOCAL_DOC "/usr/local/GNUstep/Local/Library/Documentation"
#define GNUSTEP_TARGET_LOCAL_DOC_MAN "/usr/local/GNUstep/Local/Library/Documentation/man"
#define GNUSTEP_TARGET_LOCAL_DOC_INFO "/usr/local/GNUstep/Local/Library/Documentation/info"
#define GNUSTEP_TARGET_USER_DIR_APPS "GNUstep/Applications"
#define GNUSTEP_TARGET_USER_DIR_ADMIN_APPS "GNUstep/Applications/Admin"
#define GNUSTEP_TARGET_USER_DIR_WEB_APPS "GNUstep/Library/WebApplications"
#define GNUSTEP_TARGET_USER_DIR_TOOLS "GNUstep/Tools"
#define GNUSTEP_TARGET_USER_DIR_ADMIN_TOOLS "GNUstep/Tools/Admin"
#define GNUSTEP_TARGET_USER_DIR_LIBRARY "GNUstep/Library"
#define GNUSTEP_TARGET_USER_DIR_LIBRARIES "GNUstep/Library/Libraries"
#define GNUSTEP_TARGET_USER_DIR_HEADERS "GNUstep/Library/Headers"
#define GNUSTEP_TARGET_USER_DIR_DOC "GNUstep/Library/Documentation"
#define GNUSTEP_TARGET_USER_DIR_DOC_MAN "GNUstep/Library/Documentation/man"
#define GNUSTEP_TARGET_USER_DIR_DOC_INFO "GNUstep/Library/Documentation/info"
#define GNUSTEP_TARGET_SYSTEM_USERS_DIR ""
#define GNUSTEP_TARGET_NETWORK_USERS_DIR ""
#define GNUSTEP_TARGET_LOCAL_USERS_DIR ""
#define STDC_HEADERS 1
#define HAVE_SYS_TYPES_H 1
#define HAVE_SYS_STAT_H 1
#define HAVE_STDLIB_H 1
#define HAVE_STRING_H 1
#define HAVE_MEMORY_H 1
#define HAVE_STRINGS_H 1
#define HAVE_INTTYPES_H 1
#define HAVE_STDINT_H 1
#define HAVE_UNISTD_H 1
#define __EXTENSIONS__ 1
#define _ALL_SOURCE 1
#define _GNU_SOURCE 1
#define _POSIX_PTHREAD_SEMANTICS 1
#define _TANDEM_SOURCE 1

configure: exit 1
(end of "config.log")
*** Error code 1

Stop in /usr/ports/lang/gnustep-base.
*** Error code 1

Stop in /usr/ports/devel/gnustep.
 
Thanks for your answer.

I've added
Code:
WITH_GNUSTEP_DEVEL=yes
GNUSTEP_WITH_LIBOBJC2=yes
CC?=clang
CXX?=clang++
to my make.conf, But still the same error.

This is my make.conf:
Code:
# added by use.perl 2012-06-22 14:29:31
PERL_VERSION=5.12.4
GNUSTEP_WITH_CLANG=yes
WITH_GNUSTEP_DEVEL=yes
GNUSTEP_WITH_LIBOBJC2=yes
CC?=clang
CXX?=clang++
 
expl said:
Need more info about the error, upload your config.log somewhere or post relative lines from there.

Thanks for your answer.

To reinstall GNUstep, I've removed /usr/ports, deleted all packages with pkg_remove, restored my make.conf to default, reinstalled ports and it works well. But still can't compile objective-c++.

After doing that I haven't my config.log.
 
Back
Top