Kevin
Kevin

Reputation: 21

Can you use C++ with the macOS SDK?

I want to write an application targeting both macOS and Linux computers. I would like to write my application in C++ instead of Swift. Am I able to use macOS's SDK/API (i.e. use things like this https://developer.apple.com/documentation/localauthentication/lapolicy) in C++? I can't find any C++ libraries allowing me to do things like access fingerprint authentication on macOS, and Apple's docs only show API usage in Swift and Objective-C.

Upvotes: 0

Views: 1395

Answers (3)

Boris Rasin
Boris Rasin

Reputation: 468

You can compile your C++ code for macOS, but native macOS APIs (like fingerprint authentication you mentioned) are only available in ObjC/Swift languages.

To access native macOS APIs, you can add macOS specific portion of code (written in ObjC or Swift) to your cross-platform C++ code. You can use Scapix to automatically generate C++ binding for ObjC and Swift (among other languages).

Disclaimer: I am the author of Scapix Language Bridge.

Upvotes: 1

apodidae
apodidae

Reputation: 2713

The short demo below further demonstrates that C++ and objc may be mixed. In this example a C++ class contains objc code. To run in Xcode delete the contents of the main.m file in an Xcode objc project and replace it with the following. Then change the extension of the main.m file to .mm and hit run.

#import <Cocoa/Cocoa.h>

NSTextView *txtView;

//prints formatted NSString with line feed
void NSLog(NSString *formatString, ... ) {
va_list ap;
va_start( ap, formatString );
NSString *format = [[NSString alloc] initWithFormat:@"%@\n", formatString] ;
NSString *str = [[NSString alloc] initWithFormat:format arguments:ap];
[txtView insertText:str replacementRange:NSMakeRange( [[txtView string] length], 0 )];
va_end( ap );
}

class Window {
public:
 void buildWnd();
 void printContent();
private:
 NSWindow *window;
};

void Window::buildWnd() {
    
#define _wndW  600
#define _wndH  550
window = [ [ NSWindow alloc ] initWithContentRect:NSMakeRect( 0, 0, _wndW, _wndH )
    styleMask: NSWindowStyleMaskClosable | NSWindowStyleMaskTitled
    backing:NSBackingStoreBuffered defer:YES];
[window center];
[window setTitle: @"Test window" ];
[window makeKeyAndOrderFront:nil];
    
// **** NSTextView **** //
txtView = [[NSTextView alloc] initWithFrame:NSMakeRect(0,60,_wndW,_wndH-60)];
[txtView setFont:[NSFont fontWithName:@"Lucida Grande" size:14]];
[[window contentView]addSubview:txtView];
NSLog(@"Kilroy was here.");

// **** Quit btn **** //
 NSButton *quitBtn = [[NSButton alloc]initWithFrame:NSMakeRect( _wndW - 60, 10, 40, 40 )];
 [quitBtn setBezelStyle:NSBezelStyleCircular ];
 [quitBtn setTitle: @"Q" ];
 [quitBtn setAction:@selector(terminate:)];
 [[window contentView] addSubview: quitBtn];
}

void Window::printContent(){
 NSLog(@"Hello world!!");
 NSLog(@"window = %@", window);
}

int main () {
 [NSApplication sharedApplication];
 Window w;
 w.buildWnd();
 w.printContent();
 [NSApp run];
 return 0;
}

Upvotes: 1

Geek__Lee
Geek__Lee

Reputation: 277

You can use OC and C++ mixed editing, just modify the .m file to .mm suffix you can see here:OC and C++ mixed

Upvotes: 0

Related Questions