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

时间:2021-05-06 19:48:46

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

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

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:

当我运行./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

But the _showMyCppMenu symbols is there inside libapp.a:

但是在libapp.a里面有_showMyCppMenu符号:

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.

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

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

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

Update: Found it after solved the problem. Good materials for mixing C++ with C: http://yosefk.com/c++fqa/mixing.html

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

2 个解决方案

#1


4  

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.

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

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

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

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

extern "C" void showMyMenu();

void showMyCppMenu() {
    showMyMenu();
}

#2


2  

In app.cpp, you're declaring 'showMyMenu' in the context of a C++ application, so it's being linked to as the C++ name-mangled '__Z10showMyMenu' function, not as the 'C' function that you're intending.

在app.cpp中,您在C ++应用程序的上下文中声明了“showMyMenu”,因此它被链接到C ++名称错误的“__Z10showMyMenu”函数,而不是您想要的“C”函数。

If you define a main.h (or something similar) that contains:

如果你定义一个main.h(或类似的东西),它包含:

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

or simply wrap the definition in app.c with the same scoping rule, it will work fine.

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

#1


4  

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.

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

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

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

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

extern "C" void showMyMenu();

void showMyCppMenu() {
    showMyMenu();
}

#2


2  

In app.cpp, you're declaring 'showMyMenu' in the context of a C++ application, so it's being linked to as the C++ name-mangled '__Z10showMyMenu' function, not as the 'C' function that you're intending.

在app.cpp中,您在C ++应用程序的上下文中声明了“showMyMenu”,因此它被链接到C ++名称错误的“__Z10showMyMenu”函数,而不是您想要的“C”函数。

If you define a main.h (or something similar) that contains:

如果你定义一个main.h(或类似的东西),它包含:

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

or simply wrap the definition in app.c with the same scoping rule, it will work fine.

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