views:

291

answers:

2

I am trying to figure out why when I convert my main.m file to a main.mm file, it no longer will link properly.

I have reduces the problem to the following example code:

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
int main( int argc, const char ** argv ) {
return NSApplicationMain( argc, argv);
}

I am using gnustep and linux. I enter the following commands and everything works as expected:

g++ -g -c main.m -I/usr/GNUstep/Local/Library/Headers -I/usr/GNUstep/System/Library/Headers

g++ -g -o test main.o -L/usr/GNUstep/Local/Library/Libraries -L/usr/GNUstep/System/Library/Libraries -lgnustep-base -lgnustep-gui

Now if I rename main.m to main.mm and use these two commands ( same exept main.m now main.mm):

g++ -g -c main.mm -I/usr/GNUstep/Local/Library/Headers -I/usr/GNUstep/System/Library/Headers

g++ -g -o test main.o -L/usr/GNUstep/Local/Library/Libraries -L/usr/GNUstep/System/Library/Libraries -lgnustep-base -lgnustep-gui

I get the following error: main.mm:7: undefined reference to `NSApplicationMain(int, char const**)'

Can someone please find what I am doing wrong? I do not see why it is now failing to link.

I am trying to add some C++ classes to an objective c program and this is preventing me from continuing.

Thank you for any help you can provide.

+3  A: 

The problem is the name mangling c++ compilers typically use to enable function overloading at the link stage.

C++ defines a extern "C" directive that forces it to use a C compatible name for a function.

You can use it in a C++ file like so :-

  // this makes func use a C compatible linkage
  extern "C" void func(int a)
  {

In a header file that is included by C and C++ it is necessary to protect the extern "C" declaration from C, and the C compiler does not under stand it.

#ifndef EXTERN_C
#ifdef __cplusplus
#define EXTERN_C extern "C"
#else
#define EXTERN_C
#endif
#endif
// Use it like this to declare a Function with C linkage
EXTERN_C void func(int a);
// If you have a lot of functions and declarations that need to be C compatible
#ifdef __cplusplus
extern "C" {
#endif
//functions with C linkage
void func(int a);
...
#ifdef __cplusplus
}
#endif

Now, how does this help your problem? Well, main.mm means the Foundation.h and AppKit.h files are being compiled as C++. Why Apple have not protected NSApplicationMain with a extern "C" directive I cannot guess, but it clearly isn't guarded.

A simple, if brutal, fix would be to alter your #imports like follows:

extern "C" {
// All declarations inside this block will use C linkage
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
}

int main( int argc, const char ** argv ) {
  return NSApplicationMain( argc, argv);
}
Chris Becke
+6  A: 

The problem is that when you compile it as C++, the compiler mangles the name of the symbol NSApplicationMain, so it can't find it, since it's looking for something like __Z17NSApplicationMainiPPKc. You can use the nm program (from binutils) to see what symbols the object files are referencing:

$ # When compiled as Objective-C:
$ nm main.o | grep NSApplicationMain
                 U NSApplicationMain
$ # When compiled as Objective-C++:
$ nm main.o | grep NSApplicationMain
                 U _Z17NSApplicationMainiPPKc

In order to avoid this problem, C functions need to be declared with an extern "C" modifier in order to tell the compiler not to mangle the name. Looking into <AppKit/NSApplication.h>, the header file where NSApplicationMain is declared, I see this:

APPKIT_EXPORT int
NSApplicationMain(int argc, const char **argv);

Alas, APPKIT_EXPORT is defined to be one of extern, __declspec(dllexport), extern __declspec(dllexport), or nothing in <AppKit/AppKitDefines.h>. Since it's also used for global variable declarations, we can't get around this by redefining it to extern "C" (which would be extremely hacky and kludgy anyways). The AppKit header files do not seem to contain any extern "C" declarations at all, although I do see them in various header files under Foundation/ and GNUStepBase/.

So what can you do? The solution is to wrap your includes with an extern "C":

extern "C"
{
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
}

int main( int argc, const char ** argv ) {
  return NSApplicationMain( argc, argv);
}

This will give the functions defined in those header files the proper linkage, and it will all work out. But you shouldn't have to do this -- I would file a bug report with GNUstep, telling them to add proper extern "C" declarations to their header files.

Adam Rosenfield