简体   繁体   中英

How do I convert a complex C++ XCode project to a Cocoa app?

I have the source code to a relatively complex C++ application that involves lots of libraries, dependencies, etc. I am trying to take it and add a Cocoa GUI to it. I have failed so far at the following :

  • Create a new Cocoa xcodeproj and try to move all the libraries, etc. into the Cocoa app. I just can't get it to build. Thousands of errors no matter what I do. My skill here is just not good enough to figure out what's going on.

So, I am thinking that an easier method must be to make the appropriate changes to the C++ xcodeproj in order to make the main() function launch an NSApplication instead of whatever it is doing right now.

Does anyone know what I would have to do, precisely, to make this happen? I am new to Cocoa so I am unsure of what I would have to add. For reference, the main.cpp of the C++ app is actually really simple:

int main() {
    ofSetupOpenGL(320,300, OF_WINDOW);
    ofRunApp(new testApp());
}

What I'd like to do is make all the right changes so this can be:

#import <Cocoa/Cocoa.h>

int main(int argc, char *argv[]) {
    return NSApplicationMain(argc,  (const char **) argv);
}

And have the old xcodeproj compile with all the "stuff" still in it, and then when it is run, open up the Cocoa window.

It should be noted that the C++ xcodeproj appears to have absolutely nothing Cocoa-related, or even Objective-C related in it, so we'll have to add everything that is relevant and make all relevant changes.

Any ideas?

(Or, alternatively, if you know how I might easily get all the stuff from the C++ xcodeproj to build in a squeaky clean Cocoa xcodeproj, let me know, but this seems more complex to me at the moment.)

Thanks!

Well, I have two ideas...

  1. Do You really need Cocoa? Mac support could be added with QT, wxWidgets and others.
  2. You don't have to transfer every C++ line to Objective-C++. Objective-C++ can easily handle C++ code, You just have to change .cpp -> .mm and, of course, re-write some code relative to the UI. I think, the best way here is to create a blank Cocoa app from XCode and fill it with the old code step-by-step. First get You OpenGL stuff working with Cocoa app, than transfer C++ code renaming the files.

Hint : You can cover some part of Your code with "blank declarations", ie define an empty macross instead of real function, put some blank classes with an empty methods if they are really necessary. Getting all the stuff compiling is the first and big step here. When everything compiles - put another part of the real code into the project and fix it to compile.

Unfortunately, I don't know better solution. I have re-factored huge projects few times and every time, on every platform and programming language(s) the recipe was the same. Good luck!

chances are, this will not go smoothly if you don't know c++ or similarly, cocoa.

otherwise, most of the transition will be monotonous, if you do know what you're doing. otherwise, prepare to learn a few things about objc.

first: don't bother converting a functioning c++ program's implementation to objc. it's a huge waste of time. you just wrap, bind, and alter the program to operate in that context. you don't have to write everything in objc.

second: you'll have to write some objc bindings/wrappers to use the c++ program. you'll have some wrapping to do, particularly for the ui elements and user events.

third: sources should compile as-is. you just add them to your new cocoa template project and gcc will compile it as a cpp file by default if it has the a recognized c++ extension. to use c++ and objc, use objc++ FILENAME.mm (you could also use .M , but that is not supported as well). even better, put the original cpp sources in a separate library, which the app links to.

fourth: now that you have a cocoa project template and compiling cpp sources, add the wrappers/bindings so the cocoa events/drawing performs in that context. based on your example code, your starting point uses Open GL so you'll have to use something like an NSOpenGLView if you want a Cocoa app. what exactly you'll need to implement depends on the project.

your main function should call NSApplicationMain, then your initial entries for writing the bindings to the c++ code will likely be event handling, NSWindow, NSView, and maybe NSApplication.

if you're new to objc, you'll probably have a bit of ramp-up.

I am writing a similar application but as I intend for it to be cross platform (Mac primarily and then Windows) I am keeping the backend deliberately as a C++ library. I will then provide whatever frontend GUI is appropriate (Cocoa on Mac and MFC on Windows). There is no conversion to do - you simply need to integrate what you already have.

Even though it's an old post, I didn't see this answer yet.

Your code

int main() {
     ofSetupOpenGL(320,300, OF_WINDOW);
     ofRunApp(new testApp()); 
}

Looks like Open Frameworks code. Why don't you download the Open Frameworks Mac XCode edition here , and try to put your sources in one of their example projects. For example the OpenGL flockingExample?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM