0

I have an existing framework for a command line application built from C++, I want to use it as the model layer for a particular button in a Cocoa application. The GUI for the application is in Cocoa and already constructed, however at the moment any buttons that don't interact exclusively with the GUI do nothing as a result. I have only been using Swift for about four days and already find it far too convoluted and poorly designed. Likewise Objective-C makes very little sense to me.

The majority of programs I have written in C++ rely entirely on if/and/else checks and reading/writing to files via fstream. This has worked very well for me, I have never really needed more to achieve what I want each program to do, and I have been able to make fairly complex jobs entirely automated using a simple goto statement (yes I know most people find them to be bad practice, but I have had very few issues with them and they get the job done). However to achieve the same results in Obj-C or Swift seems to unconditionally require a gross amount of excess code. One of my programs quickly goes from 56 lines for the entire source in C++ to well over 150 lines for a 1/5th of the total source when rewritting it in Swift, and I couldn't even get it to work in Obj-C.

All I need the GUI to do is run the automated C++ executable in the background, and if possible (without too much excess code) display console output in a textview when a button is clicked. That's all it needs to do, so how would I go about linking push-button Test in Cocoa GUI Test.app to function main() in C++ source file test.cpp?

Alison E.E.
  • 196
  • 7
  • You can in fact mix C++ code with Objective-C, check out: http://stackoverflow.com/questions/2683101/how-can-i-use-c-with-objective-c-in-xcode – Steeve Nov 11 '16 at 11:47
  • @Steeve Thanks for your reply, however I had heard this was possible but when I attempted to do so by adding my C++ source as `.mm` and calling its main function from ViewController.m it caused an unhandled exception with very little information on what went wrong. I undid the changes and the next time I tried to open the project none of the files would load. – Alison E.E. Nov 11 '16 at 18:44
  • There are two ways to approach this: 1) Compile your GUI and your C++ code into two separate executables, and have a button in the GUI run the C++ command line binary, or 2) Link the C++ code into the GUI application and run it by calling its entry point function. Which of these approaches do you want to pursue? – Jim Matthews Nov 14 '16 at 14:39
  • @JimMatthews While I have far more experience with making command line programs, I am trying to make an all in one .app bundle that can be both distributed and uninstalled easily, so I would have to go with the second option. – Alison E.E. Nov 21 '16 at 19:57
  • @JimMatthews In fact if it is possible I would also like to link the parts of that source that get user input to text fields/drop down menus in cocoa as well to more properly transition it to a GUI only program. (They almost all use `getline(cin,var)` in case that makes a difference. – Alison E.E. Nov 21 '16 at 20:04
  • I would start by creating a new Cocoa application project with a button and an IBAction method in the view controller. Then rename the view controller source file to have a .mm suffix so it will be compiled as C++. Next, add a .cpp C++ source file and .h header file to the project, and write a C++ function to print "Hello, World" to the console. #include the .h file in your .mm view controller file, and call the HelloWorld function from the button's IBAction. Run and test that clicking the button prints a message to the console. If you get that far you'll have a template for how to proceed. – Jim Matthews Nov 21 '16 at 20:36

0 Answers0