views:

124

answers:

4

I'm about to write a program in C++, but I'm unsure as to how to go about it. I want to create a program that can be used with a command line frontend but also a GUI frontend as I don't want to bind a user to a specific interface such as a widget toolkit for dependencies' sake.

How would be the best way to do this? I know some programs like RSync and SSH have frontends in GUIs, but I'm not sure how this is done. Wouldn't it be hacky to have a program that just uses system() to run it all while having a pretty GUI?

+6  A: 

Without knowing any other requirements, the simplest answer is just to compile your "backend" as a library, and then link your various "frontends" against it.

More complex answers involve setting your backend up as a daemon/server.

You shouldn't need system-calls to do any of this (unless you have very specific requirements).

Oli Charlesworth
+5  A: 

You implement your program's algorithms in a library, carefully avoiding any UI stuff. The API to your algorithms is specified in header files.

Then you can write several applications that use this library, one implementing a GUI fron tend and one a command line interface. They include the headers and compile against the API, and you link the library to it.
Be careful to not to compile the library and the GUI with inconsistent settings.

IME the separation of algorithms from UI can be achieved best when you first implement a command line UI. You might have to employ callbacks for that separation.

sbi
+1  A: 

You can inspire yourself on the MVC design pattern. The differing front-ends are the views on your model-controller. The controller can be a library which will factor the common tasks of your application. The GUI part and the shell part (or another language integration part for example) all use this "headless" library. Having a clear separation from the start will help enforcing modularity and decoupling.

David
Aren't MVC _views_ to view the data of the _model_?
sbi
@sbi, you are right, my response was not precise enough, I will edit my answer.
David
A: 

You could use QT for the GUI front end. But I'd actually just write your library first. Then do your GUI last.

Some helpful advice

  1. Be sure to write unit tests WHILE writing your code.

  2. Be sure to use a code coverage tool to evaluate your unit tests while writing your code.

  3. Be sure to avoid BOOL types since those are usually defined in the platform API (like win32). Use bool instead.

  4. Forward declare as many types as you can in your header files. Include as few as possible header files in each library header file.

  5. Expose as minimal an interface as possible.

  6. Be sure to follow good coding practices for c++. Herb Sutters Book C++ coding standards is excellent in this regard.

  7. Once you do write your gui, put as little business logic in your GUI event handlers as possible. Basically you want your library to be agnostic towards your GUI. Someone else mentioned the MVC pattern here. That is excellent advice.

C Johnson