Monday, July 25, 2011

C++ 0x on Mac

I was trying to convert my MSVC project into Qt Project so that it may compiled both on Windows and on Mac. But the problem is I used some of C++ 0x features in the project such as auto keyword, r-value reference and move constructor. Because my Qt on Windows uses MSVC 2010 compiler, there was no problem compiling it but the problem was on Mac which doesn't have C++ 0x feature yet.

What is special on Mac?
gcc 4.4 or later versions support C++ 0x standard but the version of gcc included in XCode 4.0 is still 4.2. Because I didn't have an excellent Mac specific knowledge, was thinking that it would be very easy to upgrade gcc into 4.4 or higher so that I may use C++ 0x standard on Mac. But it was not! The gcc compiler provided with XCode is not a normal gcc compiler but a special version which has been modified by Apple. Here I introduce how to use C++ 0x features with Qt Creator. May be I can try it also on XCode but I gave up because got a very dangerous feeling about switching XCode's default compiler.

Installation of gcc4.5
First, I installed gcc4.5 on my Mac by using of fink command 'fink install gcc45'. The command is very simple but the installation process took about 5 hours! Please note that if you are about to install a new version of gcc compiler via fink command on Friday afternoon and you got an date that night, you should give up one of beween date and gcc installation.

Qt Configuration
Now, launch your Qt Creator to modify the setting so that you may use gcc 4.5 compiler on your Qt Creator.
1. add gcc 4.5 into Tool Chains

Open preference window then choose Tool Chains on the tab on the left side of the preference window.
Click "add" button and choose "GCC" on the popup menu. Rename it into a new name such as "GCC4.5" to avoid confusing between other GCC compilers by double clicking the name.
Then set the compiler path , debugger, and ABI values. The gcc4.5 binary is usually located in /sw/bin/ and  its name is g++-4 or G++-4.5.
2. Create qmakespec for gcc4.x
There are differences between Apple's gcc compiler and gnu version of gcc compiler. Among these differences, in this time, -arch and -Xarch flag are the things to be cared. Qt's qmake predefined specification data is using -arch and Xarch flag to specify architecture values such as intel 32bit, intel 64bit, PPC 32bit, and PPC 64bit. We should make it work with our gcc compiler by switching these flags into -m32 or -m64.
Here is an example.
Go to your mkspecs directory of Qt. You can find out the location by checking Qt4 tab on Preferences.

Then make a copy of macx-g++ with a new name such as macx-gnu-g++. When you open qmake.conf file, you can see a line like below at the end of file.
Most of environment values for qmakeconfig are stored in the file, ../common/mac-g++.conf. Let's change it into a new file name such as ../common/mac-gnu-g++.conf.
Go to common directory then copy mac-g++.conf file with a new name mac-gnu-g++.conf.

QMAKE_CFLAGS_X86_64     += -m64 -mmacosx-version-min=10.5
QMAKE_OBJECTIVE_CFLAGS_X86_64  += -m64 -mmacosx-version-min=10.5
QMAKE_CXXFLAGS_X86_64   += -m64  -mmacosx-version-min=10.5
QMAKE_CXXFLAGS_X86      += -m32

QMAKE_CC         = gcc-4
QMAKE_CXX        = g++-4

3. Modify Build Setting
Now, almost done! Select "Projects" tab on left side of the Qt Creator, then you will see "Build Settings". Go to the second section, "Build Steps" then put the value "-spec mac-gnu-g++" into "Additional Arguments".

In my case, I can compile most of Qt projects by doing above things. But depending on the projects, especially depended libraries, there could be some more things to do.
Thank you.

No comments:

Post a Comment