Dovahkiin
Dovahkiin

Reputation: 1056

Why doesn't this Objective-C++ program compile?

I'm using sfml to write a game, and I thought that since I was working in Xcode I would make a Mac version and a windows version. In the mac verision I thought that it would be nice to take advantage of NSMenu to make a menu for the game. Here's what I've tried so far in a file called main.mm:

#include <iostream>
#import <AppKit/AppKit.h>

void Launch()
{
    NSMenu* menu = [[NSMenu alloc] initWithTitle:@"string"];
}

int main()
{
    std::cout << "Mac main\n";
    Launch();
}

The code doesn't compile. Xcode doesn't show any errors in the code itself. Any idea why this doesn't work?

Upvotes: 0

Views: 128

Answers (1)

Dietrich Epp
Dietrich Epp

Reputation: 213308

You must return from main. Also, you should call NSApplicationMain() if you are using AppKit. For games, this means that you write your game in C++, and you call the C++ code from Objective-C classes.

int main(int argc, char **argv)
{
    std::cout << "Mac main\n";
    return NSApplicationMain(argc, argv);
}

To call into your C++ code, I would create an application delegate and launch your C++ code from -applicationDitFinishLaunching:. You can create the application delegate in your main nib file, as well as your menus.

I don't know how to integrate with SFML. I would either use NSMenu and AppKit, or use SFML and avoid Objective C.

Upvotes: 1

Related Questions