gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:10:37 26 4
gpt4 key购买 nike

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

上下文菜单.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];
}

应用.h:

#ifdef __cplusplus
extern "C" {
#endif

void showMyCppMenu();

#ifdef __cplusplus
}
#endif

应用程序.cpp:

#include "app.h"

void showMyMenu();

void showMyCppMenu() {
showMyMenu();
}

主要.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

最佳答案

app.cpp 中的

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

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

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

extern "C" void showMyMenu();

void showMyCppMenu() {
showMyMenu();
}

关于c++ - 从 Objective-C 函数调用 C++ 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38284054/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com