Zelid Zelid - 1 year ago 76
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?


#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];


#ifdef __cplusplus
extern "C" {

void showMyCppMenu();

#ifdef __cplusplus


#include "app.h"

void showMyMenu();

void showMyCppMenu() {


#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();

[application run];



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
symbols is there inside

nm ./libapp.a alex@Pangaea

U ___CFConstantStringClassReference
U _objc_msgSend
0000000000000000 T _showMyMenu

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 Source

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() {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download