How to Make a Cross Compiler Using Gcc

Cross compiling a C application using GCC

Actually, you might be able to get away with simply specifying "-m32" on your gcc build command. Try it :)

If you get link errors, then trying installing the 32-bit C runtime libraries; then retry your "gcc -m32" build:

sudo apt-get install ia32-libs

Necessity of building a cross compiler

x86-64 ELF GCC (like you'll find on Linux) is perfectly capable of compiling for 32-bit x86/ELF.

If you were on some other ISA, like ARM, you would need a cross compiler to target i386.

If you were on Windows, you might need a cross-compiler to make 32-bit x86 ELF objects because MinGW or Cygwin GCC would be making PE/COFF object files, not ELF. Right machine code, wrong object-file format.

GCC cross compiler for DOS produces linker errors for simple Hello world! in C

I only stumbled on this question because of a more recent Stackoverflow question you asked about some code that didn't run properly in 32-bit Windows 10 inside an NTVDM virtual DOS session.

The problem is that you have not properly built a DJGPP cross compiler and all the needed components. You do not show us what commands you use for the build process and which versions of the dependencies were used and where they came from.

1st thing you need to do is build a DJGPP cross compiler. There are some people who maintain scripts to do just that. One build environment/script in particular I have used successfully is from the user Andrew Wu on Github. It is very simple to use. It appears you are using a Unix type environment based on the output you have shown us. Since you managed to build DJGPP (albeit one that doesn't work) I will assume you have all the necessary build tools installed already. First retrieve the scripts with:

git clone https://github.com/andrewwutw/build-djgpp

Change into the project directory with:

cd build-djgpp

Review the README.md file! It tells you what versions are supported by the script, build requirements for the type of OS you are on etc. At present they support versions all the way to 10.1.0. If you have everything needed choose a version to build (I'll use 9.3.0 since it is the version you are using) and then start the build. You will have to build as root or use sudo as it installs to directory /usr/local/djgpp

./build-djgpp.sh 9.3.0

It will take a while but when finished it should be installed and ready to use. The naming convention is a bit different than prefixing the commands with djgpp-. This script builds things with a more complete target prefix i586-pc-msdosdjgpp-

To add it to your path and set up other environment variables use:

. /usr/local/djgpp/setenv

If you wish it to be done each time you are logged in add that line to your shell login script. For BASH that is in the file ~/.bashrc

Create a file called hello.c containing:

#include<stdio.h>

int main()
{
printf("Hello, world!\n");
}

Compile it to a file called hello.exe:

i586-pc-msdosdjgpp-gcc -O3 -Wall -Wextra hello.c -o hello.exe 

Assuming you have a DPMI host installed (like CWSDPMI.EXE), hello.exe should run in MS-DOS, FreeDOS, DOSBox, a Windows NTVDM session etc. When run it should display:

Hello, world!


If you don't wish to build from scratch, Andrew Wu has a number of pre-built packages for a number of the latest DJGPP releases. Platforms they are available for are MacOS, Linux 32, Linux 64, MinGW, and a MinGW standalone version that doesn't need the MinGW environment to run.

Building GCC cross compiler (from Linux to Windows)

That's actually OK: the way things go, you need to

  1. build binutils
  2. install headers
  3. build the a partial C compiler: enough to create object files, but not enough to link
  4. build the win32api and mingw runtime (which includes your missing dllcrt2.o)
  5. build a complete C compiler (and other front-ends, such as C++, Fortran, Ada, whatever, if you want them)

You have successful performed step 3 above; it fails building libgcc (which is a GCC support library), but that means the C compiler core is functionnal (although it won't be able to link, it can still create valid object files). You can check that by looking at the gcc/xgcc file in your GCC build directory.

So, you need to go to the next step, not worrying about your current failure.

(To actuall install the partial C compiler, you should run make with the -k option, to have it do it best, even in the face of errors. For example, use make -k install.)



Related Topics



Leave a reply



Submit