# Favourite C++ compilers



## JohnE (Oct 18, 2003)

I'm just about to start porting some C++ applications from Windows to the Mac - so I'm looking for a decent C++ compiler.

CodeWarrier was my first choice but when I sent a few emails to Metrowerks they were a bit off-hand with me - as though they'd be offering me some great privilege if they allowed me to purchase their product...!

What other decent compilers are available?


----------



## naodx (Oct 18, 2003)

Well my favorite compiler is the gnu compiler that comes with OS X.

The reason(s) I like it are: it is free, and it is available for many different platforms.

One of the drawbacks to it, is that it isn't as fast as products like CodeWarrior.


----------



## JohnE (Oct 19, 2003)

Forgive my ignorance but is it C++ ?

I'm a bit new to Mac programming but the impression I've got so far is that Apple's own offerings are either standard 'C' or Objective C.


----------



## naodx (Oct 19, 2003)

Yes, the gnu compiler will handle C++. It is included with the developer tools from apple, and isn't pre-installed with a standard installation of OS X.

To figure out if your computer already has the developer tools installed (if you aren't sure) open up a terminal window, and at the command prompt type in:

g++ -v

This should give your something similar to this:

Reading specs from /usr/libexec/gcc/darwin/ppc/3.3/specs
Thread model: posix
gcc version 3.3 20030304 (Apple Computer, Inc. build 1495)

To find out all the thing that the compiler is capable of, you can type:

man g++

in the terminal and it will bring up the man page for the gnu compiler. There is ALOT of information there, and sometimes you have to read and re-read it to understand all the options.

With the developer tools there is a program call project builder (called xcode under panter) which can be used to build your projects, if you aren't comfortable with programming from the command line (I personally like the terminal, using vim for my editor).

The conventional way of naming C++ programs is: myfile.cpp, although there are a couple of other . extensions that can be used.

To invoke the compiler from the command line:

g++ -Wall myfile.cpp

Will compile your program, using the -Wall flag (turns all warnings on) to make an executable call a.out.

then just run a.out using:

./a.out

if you want to name the executable with a particular name, use the -o flag (this is an o as in oscar):

g++ -Wall myfile.cpp -o myprogram

where myprogram is whatever name you want to name it (no spaces in the name)

then run it:

./myprogram

This should be enough to get you started. Good luck and happy coding.


----------



## mkwan (Oct 19, 2003)

you just have to watch out for some header files that only works on Windows machines.  I think the gcc compiler is more ansi standard than the Visual C++ compiler.  Correct me if I am wrong.


----------



## mkwan (Oct 19, 2003)

what I mean is that if you include any headers that only work on PCs then the gcc compiler will not compile


----------



## naodx (Oct 19, 2003)

mkwan does make a point, there are some functions that are proprietary to visual c++ in windows, while the gnu compiler does follow the ansi standards. I don't know the complete list, but I do know that there are equivalent function calls with the gnu compiler, you just have to look in the header files, or find a good reference of all the functions calls.


----------



## JohnE (Oct 20, 2003)

Thanks.  The original apps are written for MFC which won't work on the Mac, of course, so I'm expecting to have to do a fair bit of tweaking.

Just out of interest, does anyone have any experience of a product called "Qt" by Trolltech?  It appears to be a cross-platform C++ development environment, similar to CodeWarrier I suppose - but not as well publicised.  I only heard about it a few days ago.


----------



## rhg (Oct 20, 2003)

Qt is by far the best C++ cross-platform toolkit available. Furthermore, is is a great timesaver even if you're going to write for a single platform because of its clear API. You will never consider the MFC anymore 

Adobe wrote their Photoshop Album application with Qt, see:
http://www.trolltech.com/newsroom/announcements/00000120.html


----------



## Trip (Oct 20, 2003)

I agree with rhg about Qt. I used it once at a friends house. It was pretty fast.

The Developer Tools C++ compiler is Ok, it's really slow in comparison to other compilers, but it will work for medium sized projects.

For compiling simple C I use Terminal.app.


----------



## rhg (Oct 21, 2003)

Yeah, the turnaround cycles in ProjectBuilder are horribly slow. Especially starting the debugger takes ages. Fortunately, this is about to change with Xcode!


----------



## JohnE (Oct 21, 2003)

Can one of you guys answer something about Qt?  The web site implies that the same source is used for all target platforms (which is fair enough) but that creating a new target version is "just a matter of a simple re-compile".

Does this mean what it implies?  e.g. can I compile Windows executables even if I use a Mac workstation?  Or can I compile, say, Mach-O executables from a Windows workstation?

Until I read about the "simple re-compile" I'd assumed that two physical copies of Qt would be required - one for the Mac and one for Windows.


----------



## rhg (Oct 21, 2003)

Qt is just a C++ toolkit which manifests itself as a (shared) library plus tools. You get all the source code and compile the Qt library and tools on the system where it shall run, e.g. you compile the Mac OS X version on Mac OS X and get a Mac OS X compliant (shared) Qt library and tools.

Then you compile your program (of course, first you write it). It must be linked against this Qt library, then it is ready-to-run.

Now, let's say you wrote a nice program with Qt on Mac OS X. The Qt library covers nearly eveything: the GUI, memory management, threads, networking, file i/o, you name it. If your program fits into this and only uses the Qt API (which means that you don't invoke any operating-specific API), then you can simply copy the source code to Linux and compile it there (then link it against the Linux Qt library) and your program will run on Linux out-of-the-box.

Same goes for Windows, but for Windows - Mac cross-platform development you must buy a commercial license because Qt Windows is not free. However, Qt Mac OS X and Linux is free unless you write a commercial application which you're going to sell, then you would need a commercial license.

If you have platform-specific code in your program, let's say audio i/o or interfacing with a dedicated serial or usb device, then you must encapsulate it and re-write this section of your program for every platform you want to compile it on.

Using the Windows platform to compile a program using Qt for Mac OS X does not work. This would require a cross-compiler and all relevant libraries to be installed on Windows.

As you say, if you want to do cross-platform on Windows and Mac, you install Qt both on Windows and Mac. You get the full source code which compiles into the library/libraries and a bunch of tools such as a GUI designer, a translator which serves for easy localization, a comprehensive on-line documentation viewer and more.


----------



## JohnE (Oct 21, 2003)

Wow!  What a brilliant and comprehensive answer.  I really am grateful for all this help.  The more I find out about Qt, the more I want to try it.  Apparently, there's a similar product called "wxWindows" but I'm not sure if it's quite so mature in its development cycle.

I really appreciate the info about licensing, as well.  It's an area I hadn't yet paid much attention to so you've given me further food for thought.  Thanks.


----------



## wiz (Oct 21, 2003)

well wxWindows give a better windows look an feel to the apps.. 

and Qt dosen't really fit to the macox aqua human interface guidelines.. it's soo window-ish.. 

but Qt is really no-doubt well designed.


----------



## rhg (Oct 22, 2003)

My 0.02: It depends on the programmer if an app follows the platform guidelines or not. You can easily write an Objective-C program using the Cocoa API which does not at all match the Aqua human interface guidelines. This is not a question of the toolkit used. Qt has all Aqua controls available in exact the Aqua look and they behave (feel) exactly Aqua-ish. And the most-prominent platform-specific GUI characteristics are handled automatically. For example: Menu Bar, location of the Preferences and About menu items, keyboard shortcuts, clipboard integration - you don't have to deal with these issues explicitely, they work on the different platforms the way they are expected to work.


----------

