Michael Torrie's Personal Wiki

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

computer_stuff:crosscompiler [2010/04/29 17:20]
Michael Torrie created
computer_stuff:crosscompiler [2010/04/29 17:38] (current)
Michael Torrie
Line 1: Line 1:
 ====== Cross Compiling With GCC ====== ====== Cross Compiling With GCC ======
 +**Important note** - Although this document was originally written in 2004, and refers to version of gcc, binutils, glibc, and mingw runtimes that are now obsolete, the basic procedures for setting up a cross-compiling environment from scratch are still the same today. ​ For Linux users, most modern distros including Fedora and Ubuntu actually have official packages for installing a cross-compiling environment that targets MS Windows. ​ Rather than building your own compiler, you are advised to use the officially provided ones if you happen to need to build MS Windows executables on Linux.
 +
 The GNU Compiler Collection lends itself extremely well to cross compiling, due to its modular architecture. This page is a brief overview of setting up GCC to compile executables to run on foreign linux platforms, and also the Mingw Win32 platform The GNU Compiler Collection lends itself extremely well to cross compiling, due to its modular architecture. This page is a brief overview of setting up GCC to compile executables to run on foreign linux platforms, and also the Mingw Win32 platform
 What is Cross Compiling What is Cross Compiling
Line 18: Line 20:
  
 ^ Switch ^ Explaination ^ ^ Switch ^ Explaination ^
-| --build | The architecture of the host OS (usually autodetected) | +%%--%%build | The architecture of the host OS (usually autodetected) | 
-| --host | The architecture of the OS that the resulting program will run on | +%%--%%host | The architecture of the OS that the resulting program will run on | 
-| --target | The architecture of the OS that this program is intended to work with |+%%--%%target | The architecture of the OS that this program is intended to work with |
  
-Note the subtle difference between the ''​--host''​ and ''​--target''​ flags. When building a cross compiler, both bintuils and gcc are configured with the ''​--target''​ set to the target operating system, but since they are cross compilers, they will run on the local host OS. Thus we don't specify anything for the ''​--host''​ option. For building libraries and binaries with our cross compiler, we would specify both the ''​--target''​ and ''​--host''​ flags to be the target operating system because the libraries or binaries are intended to actually run on that platform.+Note the subtle difference between the ''​%%--%%host''​ and ''​%%--%%target''​ flags. When building a cross compiler, both bintuils and gcc are configured with the ''​%%--%%target''​ set to the target operating system, but since they are cross compilers, they will run on the local host OS. Thus we don't specify anything for the ''​%%--%%host''​ option. For building libraries and binaries with our cross compiler, we would specify both the ''​%%--%%target''​ and ''​%%--%%host''​ flags to be the target operating system because the libraries or binaries are intended to actually run on that platform.
  
 The architecture designations used by autoconf configure scripts usually are in the following format: The architecture designations used by autoconf configure scripts usually are in the following format:
Line 57: Line 59:
  
 ==== Configuring and compiling bintuils ==== ==== Configuring and compiling bintuils ====
-After unpacking and patching binutils, run configure in the root of the source tree with the ''​--target''​ set to ''​i386-redhat-linux'':​+After unpacking and patching binutils, run configure in the root of the source tree with the ''​%%--%%target''​ set to ''​i386-redhat-linux'':​
 <​code>​ <​code>​
 ./configure --prefix=/​sw --target=i386-redhat-linux ./configure --prefix=/​sw --target=i386-redhat-linux
Line 120: Line 122:
 </​code>​ </​code>​
  
-Although most native gcc distributions will compile will all possible languages enabled, most often you only want C, C++ and possibly objc or java. Use the --enable-languages switch to specify the languages you want the cross compiler to support. Except for the --prefix switch, you can pass all the flags to configure that the native compiler used. Just add the appropriate ''​--target''​ switch.+Although most native gcc distributions will compile will all possible languages enabled, most often you only want C, C++ and possibly objc or java. Use the %%--%%enable-languages switch to specify the languages you want the cross compiler to support. Except for the %%--%%prefix switch, you can pass all the flags to configure that the native compiler used. Just add the appropriate ''​%%--%%target''​ switch.
  
 A working example of configuration is the following: A working example of configuration is the following:
Line 139: Line 141:
  
 ==== Cross-compiling Autoconf-enabled Packages ==== ==== Cross-compiling Autoconf-enabled Packages ====
-To compile an autoconf configured program to run on a target architecture using your cross compiler, and if the configure.in file supports it, you configure using the --target and --host switches together: +To compile an autoconf configured program to run on a target architecture using your cross compiler, and if the configure.in file supports it, you configure using the %%--%%target and %%--%%host switches together: 
- +<​code>​ 
-    ./configure --target=i386-redhat-linux --host=i386-redhat-linux+./configure --target=i386-redhat-linux --host=i386-redhat-linux 
 +</​code>​
  
 ===== Building an i386-pc-mingw32 Cross Compiler ===== ===== Building an i386-pc-mingw32 Cross Compiler =====
Line 225: Line 228:
 </​code>​ </​code>​
  
-==== Adding Libraries To the Cross Compiling Environment ====+===== Adding Libraries To the Cross Compiling Environment ====
 ==== Linux Targets ==== ==== Linux Targets ====
  
 You can add libraries (already compiled for the target platform) to the directory PREFIX/​ARCH/​lib. In our case, that is /​sw/​i386-redhat-linux/​lib. Copy the libraries (complete with symlinks) just as they appear in the devel package. Header files should all go in PREFIX/​ARCH/​include,​ for example, /​sw/​i386-redhat-linux/​include. You can add libraries (already compiled for the target platform) to the directory PREFIX/​ARCH/​lib. In our case, that is /​sw/​i386-redhat-linux/​lib. Copy the libraries (complete with symlinks) just as they appear in the devel package. Header files should all go in PREFIX/​ARCH/​include,​ for example, /​sw/​i386-redhat-linux/​include.
-Mingw Target +==== Mingw Target ​==== 
-The libraries and headers go in the same place as in the linux architectures,​ except that you don't need .dll files. You do need the .dll.a stubs, however +The libraries and headers go in the same place as in the linux architectures,​ except that you don't need .dll files. You do need the .dll.a stubs, however. 
-Dealing with pkg-config +===== Dealing with pkg-config ​===== 
- +''​pkg-config'' ​is a convenient way of dealing with include files and linker paths, and it works in a cross-compiling environment just as well as in a native environment,​ with a few caveats. You do not want to use the same .pc files for the cross-compile target as you do for your native host. If you do, the program may compile but it will not link. Instead, put .pc files in PREFIX/​ARCH/​lib/​pkgconfig. Then edit the pc file and make sure that the paths are set to your real paths as they really are in the cross-compiling environment. For example, a .pc file may say that its include path is /​usr/​include/​gtk-2.0,​ but in the cross-compile environment it should be set to /​sw/​i386-redhat-linux/​include/​gtk-2.0 in this case.
-pkg-config is a convenient way of dealing with include files and linker paths, and it works in a cross-compiling environment just as well as in a native environment,​ with a few caveats. You do not want to use the same .pc files for the cross-compile target as you do for your native host. If you do, the program may compile but it will not link. Instead, put .pc files in PREFIX/​ARCH/​lib/​pkgconfig. Then edit the pc file and make sure that the paths are set to your real paths as they really are in the cross-compiling environment. For example, a .pc file may say that its include path is /​usr/​include/​gtk-2.0,​ but in the cross-compile environment it should be set to /​sw/​i386-redhat-linux/​include/​gtk-2.0 in this case.+
  
 Once pkg-config is set up right, simply set the PKG_CONFIG_DIR environment variable before you run configure or make the program. Once pkg-config is set up right, simply set the PKG_CONFIG_DIR environment variable before you run configure or make the program.
-Links 
- 
-    * http://​www.wxwindows.org/​technote/​crosscmp.htm - notes on setting up a cross compiler targetting mingw32 on linux and using the wxWindows library in that environment 
-    * http://​www.mingw.org - Official site for the Mingw32 tools for Windows. 
-    * http://​sources.redhat.com/​autobook/​autobook/​autobook_262.html#​SEC262 - Autoconf documentation on building a cross compiler 
-    * http://​bytesex.org/​cross-compiler.html - a good mini-howto on building cross compilers. Also has some good tips for extracting the source tarballs and patches from RedHat SRPMS. 
-    * http://​linuxdevices.com/​articles/​AT6830035793.html - Detailed how-to: Cross-compiling for Sharp Zaurus 
  
-Copyright 2004 Michael Torrie +===== Links ===== 
-11 Feb 2004 +  * http://​wiki.wxwidgets.org/​Cross-Compiling_Under_Linux - notes on setting up a cross compiler targetting mingw32 on linux and using the wxWindows library in that environment 
 +  * http://​www.mingw.org - Official site for the Mingw32 tools for Windows. 
 +  * http://​sources.redhat.com/​autobook/​autobook/​autobook_262.html#​SEC262 - Autoconf documentation on building a cross compiler 
 +  * http://​bytesex.org/​cross-compiler.html - a good mini-howto on building cross compilers. Also has some good tips for extracting the source tarballs and patches from RedHat SRPMS.

QR Code
QR Code computer_stuff:crosscompiler (generated for current page)