Zelid Zelid - 5 months ago 22
Objective-C Question

Calling C++ function from Objective-C function doesn't work

I can't figure out why this simple calling C++ function from Objective-C file doesn't work... How to fix the problem?

context-menu.m:

#import <Cocoa/Cocoa.h>

void showMyMenu() {
NSMenu *theMenu = [[NSMenu alloc] initWithTitle:@"Contextual Menu"];
[theMenu insertItemWithTitle:@"Beep" action:@selector(beep:) keyEquivalent:@"" atIndex:0];
[theMenu insertItemWithTitle:@"Honk" action:@selector(honk:) keyEquivalent:@"" atIndex:1];
[theMenu popUpMenuPositioningItem:nil atLocation:[NSEvent mouseLocation] inView:nil];
}


app.h:

#ifdef __cplusplus
extern "C" {
#endif

void showMyCppMenu();

#ifdef __cplusplus
}
#endif


app.cpp:

#include "app.h"

void showMyMenu();

void showMyCppMenu() {
showMyMenu();
}


main.m:

#import <Cocoa/Cocoa.h>

#include "app.h"

// void showMyMenu();
// void showMyCppMenu();

int main(int argc, const char * argv[])
{
NSApplication * application = [NSApplication sharedApplication];

// NSView* ns = (NSView*) startup();

[application setActivationPolicy:NSApplicationActivationPolicyRegular];

id ns = [[NSView new] autorelease];

id menubar = [[NSMenu new] autorelease];
id appMenuItem = [[NSMenuItem new] autorelease];
[menubar addItem:appMenuItem];
[application setMainMenu:menubar];

id appMenu = [[NSMenu new] autorelease];
id appName = [[NSProcessInfo processInfo] processName];
id quitTitle = [@"Quit " stringByAppendingString:appName];
id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle
action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
[appMenu addItem:quitMenuItem];
[appMenuItem setSubmenu:appMenu];

[[ns window] setTitle:appName];
[[ns window] makeKeyAndOrderFront:nil];
[NSApp activateIgnoringOtherApps:YES];

[NSApp activateIgnoringOtherApps:YES];

NSStatusItem * statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength] retain];
[statusItem setMenu:appMenu];
// [statusItem setImage:icon];
// [statusItem setAlternateImage:icon2];
[statusItem setHighlightMode:YES];
// [statusItem setToolTip:[NSString stringWithUTF8String:title]];

// showMyMenu();
showMyCppMenu();

[application run];

return EXIT_SUCCESS;
}


build.sh:

gcc -fPIC context-menu.m app.cpp -framework Cocoa -x objective-c -c -lobjc -lstdc++
ar rcs libapp.a context-menu.o app.o
gcc -L/Users/alex/Workspace/SimpleAppFromScratch/mixing-objc1 main.m -framework Cocoa -x objective-c -o main -lobjc -lstdc++ -lapp


When I run ./build.sh I always get an error:

Undefined symbols for architecture x86_64:
"showMyMenu()", referenced from:
_showMyCppMenu in libapp.a(app.o)
ld: symbol(s) not found for architecture x86_64


But the
_showMyCppMenu
symbols is there inside
libapp.a
:

nm ./libapp.a alex@Pangaea

./libapp.a(context-menu.o):
U _OBJC_CLASS_$_NSEvent
U _OBJC_CLASS_$_NSMenu
U ___CFConstantStringClassReference
U _objc_msgSend
0000000000000000 T _showMyMenu

./libapp.a(app.o):
U __Z10showMyMenuv
0000000000000000 T _showMyCppMenu


I really have a need to call c++ function from main.m objective-c while the first c++ function is calling another objective-c function.

How to fix the build script? Why doesn't it work?

Answer

showMyMenu from your app.cpp is not marked as C function, so that compiler exports the symbol as C++ symbol: showMyMenu, not _showMyMenu as it would be in C.

To solve your problem you need to simply mark the function as C function:

// app.cpp
#include "app.h"

extern "C" void showMyMenu();

void showMyCppMenu() {
    showMyMenu();
}