Object C ++ binding

I am trying to understand why, when I convert the main.m file to the main.mm file, it will no longer link correctly.

I reduced the problem to the following code example:

#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 (the same as 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 **) '

- , ? , .

++ c, .

, .

+3
2

, ++, NSApplicationMain, , - __Z17NSApplicationMainiPPKc. nm ( binutils), , :

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

, C- extern "C", . <AppKit/NSApplication.h>, , NSApplicationMain , :

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

, APPKIT_EXPORT extern, __declspec(dllexport), extern __declspec(dllexport) <AppKit/AppKitDefines.h>. , , extern "C" ( kludgy ). AppKit, , extern "C", Foundation/ GNUStepBase/.

? , extern "C":

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

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

, , , . - GNUstep, extern "C" .

+9

, , ++, .

++ extern "C", C .

++, : -

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

, C ++, "C" extern C, C .

#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

, ? , main.mm , Foundation.h AppKit.h ++. Apple NSApplicationMain "C", , .

, , #imports, :

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);
}
+4

Source: https://habr.com/ru/post/1727149/


All Articles