如何解决为什么在调用[myclass alloc] misx c ++到object-c ++到object-c时出错?
我尝试从对象c使用调用c ++
使用B4I
此代码,但我得到错误
我已经在使用file.mm
此文件Greeting.cpp
#include "Greeting.hpp"
Greeting::Greeting() {
greeting = "Hello C++!";
}
std::string Greeting::greet() {
return greeting;
}
此文件Greeting.hpp
#ifndef Greeting_hpp
#define Greeting_hpp
#include <stdio.h>
#include <string>
class Greeting {
std::string greeting;
public:
Greeting();
std::string greet();
};
#endif /* Greeting_hpp */
此文件ViewController.h
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
- (void)showGreeting;
@end
此文件ViewController.mm
#import "ViewController.h"
#import "Greeting.hpp"
@interface ViewController ()
{
Greeting greeting;
IBOutlet UIButton *helloButton;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view,typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// dispose of any resources that can be recreated.
}
- (void)showGreeting {
NSLog(@"test %@");
}
@end
我尝试称呼它
ViewController* ViewController1= [[ViewController alloc] init];
[ ViewController1 showGreeting ];
-------------------------------
Undefined symbols for architecture arm64:
"_OBJC_CLASS_$_ViewController",referenced from:
objc-class-ref in b4i_main.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command Failed with exit code 1 (use -v to see invocation)
---------------------------------
当我使用
ViewController* ViewController1;
[ ViewController1 showGreeting ];
返回null 注意此示例HelloCpp-master
谢谢
解决方法
以下示例显示了如何使用AppDelegate中的方法从objc应用程序调用c ++类。它可以在终端中运行,方法是先将源代码保存在名为“ greet.mm”的文件中,然后使用以下命令:clang greet.mm -framework Cocoa -o greet && ./greet
//clang greet.mm -framework Cocoa -o greet && ./greet
#import <Cocoa/Cocoa.h>
class Greeting {
public:
void greet();
};
void Greeting::greet() {
NSLog(@"Hello C++!");
}
@interface AppDelegate : NSObject <NSApplicationDelegate>
{
NSWindow *window;
}
- (void) myBtnAction: (id)sender;
- (void) buildMenu;
- (void) buildWnd;
@end
@implementation AppDelegate
- (void) myBtnAction: (id)sender {
Greeting g;
g.greet();
}
- (void) buildMenu {
NSMenu *menubar = [NSMenu new];
NSMenuItem *menuBarItem = [NSMenuItem new];
[menubar addItem:menuBarItem];
[NSApp setMainMenu:menubar];
NSMenu *appMenu = [NSMenu new];
NSMenuItem *quitMenuItem = [[NSMenuItem alloc] initWithTitle:@"Quit"
action:@selector(terminate:) keyEquivalent:@"q"];
[appMenu addItem:quitMenuItem];
[menuBarItem setSubmenu:appMenu];
}
- (void) buildWnd {
#define _wndW 200
#define _wndH 150
window = [ [ NSWindow alloc ] initWithContentRect:NSMakeRect( 0,_wndW,_wndH )
styleMask: NSWindowStyleMaskClosable | NSWindowStyleMaskTitled
backing:NSBackingStoreBuffered defer:YES];
[window center];
[window setTitle: @"Test window" ];
[window makeKeyAndOrderFront:nil];
// **** My Button **** //
NSButton *myBtn =[[NSButton alloc]initWithFrame:NSMakeRect( 40,80,125,24 )];
[myBtn setBezelStyle:NSBezelStyleRounded ];
[myBtn setTitle: @"Run CPP Class"];
[myBtn setAction: @selector (myBtnAction:)];
[[window contentView] addSubview: myBtn];
// **** Quit btn **** //
NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 50,10,40,40 )];
[quitBtn setBezelStyle:NSBezelStyleCircular ];
[quitBtn setTitle: @"Q" ];
[quitBtn setAction:@selector(terminate:)];
[[window contentView] addSubview: quitBtn];
}
- (void) applicationWillFinishLaunching: (NSNotification *)notification {
[self buildMenu];
[self buildWnd];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
@end
int main () {
NSApplication *app = [NSApplication sharedApplication];
AppDelegate *appDelegate = [[AppDelegate alloc] init];
[app setDelegate:appDelegate];
[NSApp run];
return 0;
}
,
应在iOS中运行。将ViewController的扩展名从.m更改为.mm
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
class Greeting {
public:
void greet();
};
void Greeting::greet() {
NSLog(@"Hello C++!");
}
@interface ViewController ()
@property (strong,nonatomic) IBOutlet UIButton *LaunchBtn;
@end
@implementation ViewController
- (IBAction)showGreeting:(id)sender {
Greeting g;
g.greet();
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
}
@end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。