Code::Blocks and Cross Compilers

From Code::Blocks
Revision as of 23:11, 6 December 2006 by MortenMacFly (talk | contribs)
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.


This Document will describe how to setup Code::Blocks to use it with a cross compiler. The idea is to create a easy way to develop software under a platform such as Linux, and create an executable for another platform e.g. Windows without booting to the native Operating System.

Compiling Windows binaries on Linux

This will describe how to setup your Linux box to compile your code for the Windows platform using MinGW (Minimalist GNU for Windows). Depending on your distribution, the installation of the needed packages can differ.

Installing MinGW

Ubuntu 6.06

Install the mingw package from the Universe Repository, using synaptics or with the following command:

sudo apt-get install mingw32

Setup Code::Blocks to use the Cross Compiler

To let Code::Blocks know about your cross compiler you have to create a new compiler profile. Go to Settings->Compiler and Debugger Settings and create a copy of the current selected compiler. Type in a Name for the new profile e.g. cross_linux and hit enter.

Settings compiler debugger name.png 

Now you will have to change some directories for the new cross compiler. You will need to setup the directories for Compiler, Linker, and Resource Compiler. For every entry you have to put: /usr/i586-mingw32-msvc. The pictures below show how it should look like.

Settings compiler debugger compiler.png
Settings compiler debugger directories.png 

Settings compiler debugger Resource compiler.png

The last step is to tell Code::Blocks where and what your cross compiler binaries are. Just select the directory where you have copied all the compilers binaries ( /usr/i586-mingw32-msvc/ ) and setup the executables as shown in the picture below.

Settings compiler debugger programms.png


Now you can start a new project or open an existing project, and if you want to make a cross compilation for Windows platforms, just go to Project->Build Options and select your cross compiler. Remember that you will have to rebuild the whole project. You can test your build with Wine.

Tips from the forum

The following has been copied from a forum article originally posted by visualphoenix:

Today I managed to finish figuring out how to set up the build options for cross compiling, debugging and running windows executables for projects built with Code::Blocks using linux... As such, I decided it was time to sign up for the c::b forums and post a howto in case anyone else was interested in knowing what I did...

The following is how I did this on Ubuntu 'Dapper Drake' Linux:

Step 1: Install MingW32 for linux

 # sudo apt-get install mingw32

Step 2: Settings->Compiler and debugger settings

 Select GNU GCC Compiler and click the Copy button.
 Name this: MingW32 Compiler

Step 3: Click the Compiler tab and then click the #defines tab and add the following:

 WINVER=0x0400
 __WIN95__
 __GNUWIN32__
 STRICT
 HAVE_W32API_H
 __WXMSW__
 __WINDOWS__

Click the Linker tab and the following under "Other Linker Options":

 -lstdc++
 -lgcc
 -lodbc32
 -lwsock32
 -lwinspool
 -lwinmm
 -lshell32
 -lcomctl32
 -lctl3d32
 -lodbc32
 -ladvapi32
 -lodbc32
 -lwsock32
 -lopengl32
 -lglu32
 -lole32
 -loleaut32
 -luuid

Note: Not all of these are REQUIRED... As I have been recently messing with compiling apps for windows with OGL and DX9 support I have realized that there are some additions I have needed to add here... I will update accordingly when I know more.

Step 4: Click the Directories tab and the Compiler tab.

 Modify the path to read the following (where ix86 is your architecture type):
 /usr/i586-mingw32msvc/include

Click the Directories tab and the Linker tab:

 Modify the path to read the following (where ix86 is your architecture type):
 /usr/i586-mingw32msvc/lib

Click the Directories tab and the Resource Compiler tab:

 Modify the path to read the following (where ix86 is your architecture type):
 /usr/i586-mingw32msvc/include

Step 5: Click the Programs tab:

 C compiler: i586-mingw32msvc-gcc
 C++ compiler: i586-mingw32msvc-g++
 Linker for dynamic libs: i586-mingw32msvc-g++
 Linker for static libs: i586-mingw32msvc-ar
 Debugger: i586-mingw32msvc-gdb    **** MORE ON THIS LATER ****

Click OK and save your changes.

Step 6: Ubuntu's mingw32 package and from what I can tell, MingW32 in general doesn't really have a solid gdb option for debugging natively in Linux so we're going to work around this using wine and mingw32's latest insight build for windows

Install Wine

 # sudo apt-get install wine

Step 7: Download Insight from the Insight homepage.

Step 8: Once you download insight.exe, extract the archive using wine:

 wine insight.exe

I extracted this to my desktop

Step 9: Move the insight folder to /opt. the path should now look like

 /opt/insight/bin/gdb.exe

Step 10: create a shell script in /usr/bin: (Note: shell scripts should start with a hash (#) bang (!), ie: "# ! / bin / sh " [with no spaces] but when I add that the forum post tanks)

 # sudo gedit /usr/bin/i586-mingw32msvc-gdb

and add the following:

 wine /opt/insight/bin/gdb.exe "$@"

Save the file and quit gedit

Step 11:

 # sudo chmod +x /usr/bin/i586-mingw32msvc-gdb

Now we have a way to execute the windows version of mingw32's gdb for windows in linux using our shell script wrapper

Step 12: Create a new console application project in Code::Blocks. Using the wizard select the MingW32 Compiler option.

Step 13: Right click the project and go to properties. Click the Targets tab and set the Output Filename to be whatever you want with a .exe file extension. Make sure the type is a Console Application.

Step 14: When I reached this step, compiled and tried to run my application I realized that for some reason Code::Blocks was trying to execute my .exe through /usr/bin/wine-auto (which I do not have)... So I created a simlink to wine:

 # sudo ln -s /usr/bin/wine /usr/bin/wine-auto

Step 15: Hit F9 in Code::Blocks and the hello world application runs!! YAY! Set a breakpoint on line 5 and hit F8 and the application breaks in the debugger!! Woot! Now you can successfully compile, execute, and debug windows applications in Linux using Code::Blocks!!!