从Objective-C函数调用C ++函数不起作用

时间:2016-07-09 16:46:57

标签: c++ objective-c c clang clang++

我无法弄清楚为什么这个来自Objective-C文件的简单调用C ++函数不起作用......如何解决这个问题?

上下文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

当我运行./build.sh时,我总是收到错误:

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

_showMyCppMenu内有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

我真的需要从main.m objective-c调用c ++函数,而第一个c ++函数调用另一个objective-c函数。

如何修复构建脚本?为什么它不起作用?

更新: 解决问题后找到它。将C ++与C混合的好材料: http://yosefk.com/c++fqa/mixing.html

2 个答案:

答案 0 :(得分:4)

来自showMyMenu

app.cpp未标记为C函数,因此编译器会将符号导出为C ++符号:showMyMenu,而不是_showMyMenu,因为它将在C

要解决您的问题,您只需将该功能标记为C功能:

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

extern "C" void showMyMenu();

void showMyCppMenu() {
    showMyMenu();
}

答案 1 :(得分:2)

在app.cpp中,你在C ++应用程序的上下文中声明'showMyMenu',所以它被链接到C ++名称被破坏的'__Z10showMyMenu'函数,而不是你的'C'函数意图。

如果定义包含以下内容的main.h(或类似内容):

extern "C" {
   void showMyMenu(void);
};

或者只是使用相同的范围规则将app.c中的定义包装起来,它将正常工作。