Saturday, June 21, 2008

How to make a portable Codeblocks project

Perhaps this will help other developers who want to write cross-platform applications with Code::Blocks. After breaking my head for various hours, I finally found out how to do it. Code::Blocks 8.02 includes a script engine which can make certain compiler options apply only to Windows environments.

Example: In your project settings:
Under "Compiler settings", "other options":

[[if (PLATFORM == PLATFORM_MSW) print(_T("-mthreads -D__GNUWIN32__ -D__WXMSW__"));;]]
`wx-config --cflags`


(Note: The double ;; at the end is a workaround a scripting bug)
As you can see, the one-line script is pretty much like C. the _T must have been incorporated for wxWidgets compatibility. In any case, Notice the compilation string: -mthreads, -D__GNUWIN32__ and -D__WXMSW__. The -D is a compiler define. I tried to add scripting to the #defines, but that didn't work, so I had to add them on the compiler command line.

The wx-config part is for Linux. I've found that experimentally, having this option in the project doesn't affect compilation under Windows.

The same can be done with the linker. Under Linker settings, other linker options:


[[if (PLATFORM == PLATFORM_MSW) print(_T("-mthreads -lwxmsw28u -lintl.dll"));;]]
`wx-config --libs`


Here I tell the linker to link two windows-only libraries: libwxmsw28u.a, and libintl.dll.a (libintl is used for internationalization). For posix environments (OS X or GNU/Linux), the backticked expressions are more than enough, but you could do the same with "if (PLATFORM != PLATFORM_MSW).

Under search directories, I add both the Windows and posix directories.

Compiler search dirs:


$(#wx.include)
$(#wx)/contrib/include
$(#wx)/lib/gcc_dll/mswu


Linker search dirs:


$(#wx)/lib/gcc_dll


Resource Compiler search dirs:


$(#wx.include)
$(#wx)/lib/gcc_dll/mswu


With this simple settings, you won't require having two different projects, one for GNU/Linux or Mac OS, and another for Windows.

This will help making cross-platform projects quite easy.

No comments: