Difference between revisions of "Code::Blocks and Cross Compilers"

From Code::Blocks
(Added references/instructions for other distros than ubuntu)
m
Line 59: Line 59:
 
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 [http://www.winehq.org/ Wine].
 
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 [http://www.winehq.org/ Wine].
  
==Tips from the forum(likely unnecessary)==
+
==Tips from the forum(pretty old tutorial, but may still have some tips)==
 
The following has been copied from a forum article originally posted by visualphoenix:
 
The following has been copied from a forum article originally posted by visualphoenix:
  

Revision as of 16:07, 28 March 2024


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

Package available for many major OS versions(Ubuntu,Debian etc)

Install the mingw-w64 package from the Universe Repository(Ubuntu) or your relevant distro Repo, using synaptics or with the following command:

sudo apt-get install mingw-w64 (Debian/Ubuntu command, use relevant command for your distro package manager)

(For older users, the old mingw project had been forked has been replace by this new, more updated version.)

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.

NOTE: Regarding the following screenshot, in later CodeBlocks versions,

"Compiler has in been renamed to Compiler Settings"

"Linker has in been renamed to Linker Settings"

"Directories has in been renamed to Search directories"

"Programs has in been renamed to Toolchain Executables"

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. Every entry follows the pattern: /usr/x86_64-w64-mingw32/ , followed the required subdirectory(Alternatively, you may use "/usr/i686-w64-mingw32" for 32 bit targets. The pictures below show how it should look like.

Specifically,

For Compiler:/usr/x86_64-w64-mingw32/include

For Linker: /usr/x86_64-w64-mingw32/lib

For Directories: /usr/x86_64-w64-mingw32/include

(Most users simply ignore this line. However, 32 bit targets may replace all instances of x86_64-w64-mingw32 with i686-w64-mingw32 as mentioned prior.)

NOTE: Below screenshots references "i586-mingw32msvc" in the quoted paths/directories. There screenshots are out of date. All instances of "i586-mingw32msvc" are to be replaced by "x86_64-w64-mingw32" as mentioned in instructions above for compiler, linker and directories tabs.

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 ) and setup the executables as shown in the picture below.


NOTE: If you are using the likes of std::thread, std::mutex, std::condition_variable etc,

you may receive compilation errors. In this case make sure you are using a recent version of mingw and in the abov screenshot replace all instances of x86_64-w64-mingw32-gcc with x86_64-w64-mingw32-gcc-posix.

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(pretty old tutorial, but may still have some tips)

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!!!