Tag Archives: compiler

Cross compiling with MinGW and crosstool-ng

I am sadistic.

I have given up.

I am leaving the instructions below but this is plan too difficult and I am moving on to Cygwin.  These instructions will definitely get cross tools built but it is a serious uphill battle to get it to compile a compiler.

My goal is to cross compile on MinGW (on Winderz) for a Linux target – both 64 bit Ubuntu 13.10 or an ARM (such as a Beagle Bone).  Why?  Because I am sadistic.  We covered that.

The real purpose is to take a Windows GUI that generates C code and compile it for a different platform (hence cross compiling).  Should be easy right?  well… let’s find out.  This is the steps on how I did it (in my own little hacky way).

The first step will be to use crosstools-ng and get it to run under MinGW.  This post will deal with that.  A subsequent post will deal with creating the cross compiler.

Note that as information on the web becomes quickly out of date, realize that this is the middle of March in 2014.

  1. Download and MinGW from here.  We will assume that you installed it on C:\MinGW.
  2. In the installation manager, mark the following packages to add and apply them.  The installer is really easy to use and should be self-explanatory.  (While applying them, it may be good to refill your coffee mug.)
      • mingw-developer-toolkit
      • mingw32-base
      • msys-wget
      • msys-gcc
      • msys-libtool
      • mingw32-pdcurses  This doesn’t work.  We will do this manually later.
      • msys-libregex (the dev package)
      • mingw32-gcc-v3-java
  3. This is not obvious now but later we will need Subversion for eglibc and gcj.exe (Java) for crosstool-ng.  First copy gcj.exe from /MinGW/bin to /MinGW/msys/1.0.  It is included with MinGW but not with msys.  Second, install subversion onto your Windows machine.  In C:\Program Files (x86)\Subversion\bin\, copy svn.exe to  /MinGW/msys/1.0.
  4. Open up your msys bash shell the batch file – C:\MinGW\msys\1.0\msys.bat.
  5. Download and build crosstool-ng following the steps here.  Substitute your version.  There are listed below with my version and some other things I did.

    wget http://crosstool-ng.org/download/crosstool-ng/crosstool-ng-1.19.0.tar.bz2
    tar xjf crosstool-ng-1.19.0.tar.bz2
    cd crosstool-ng-1.19.0
    ./configure --prefix=/home/maks/crosstool

  6. A couple of problems now.  First there was a strange tar error where it couldn’t copy a symbolic link.  The second time it ran fine.  The second issue is that gperf doesn’t exist.  We will build that from source as MinGW doesn’t include it.  Get that from GNU source here.  Grab the bz2 file, uncompress, and copy the source to your home directory.  Inside that directory, do the old standby.

    cd gperf-3.0.4
    ./configure --prefix=/usr
    make
    make install
    cd crosstool-ng-1.19.0
    ./configure --prefix=/home/maks/crosstool

  7. Now it can’t find the curses header.   Originally, I thought I had pdcurses package installed and was good to go.  The package is missing some important things like a DLL and header.  So onto source we go.  This page has all the information.  Even though it appears for mingw-w64, it works for 32 bit as well.  In the source directory, do the following.

    ./configure --enable-term-driver --enable-sp-funcs --prefix=/usr
    make
    make install 

  8. Surprise! More issues.  Here is a list of corrections then redo the previous step.
      • In ./ncurses/win32con/gettimeofday.c, add “#include <sys/time.h>”.  Also, change the gettime of day definition t the following:  “int gettimeofday(struct timeval *tv, struct timezone *tz)”.  This eliminates a duplicate definition error.
      • Side note: GetSystemTimeAsFileTime in gettimeofday seems to have a bug.  Whenever the fractional seconds becomes higher than 0.5, the integer seconds increment.  After all the testing I did but didn’t describe here, I can’t believe it is anything with long longs, or gcc, etc.  It has to be the underlying win32 api.
      • In ./ncurses/win32con/win_driver.c, add “#include <windows.h>” near the top.  Also add “#define ATTACH_PARENT_PROCESS (DWORD)-1” somewhere near the top.
      • In ./test/tclock.c, the double fraction declaration is inside an #if HAVE_GETTIMEOFDAY statement and it shouldn’t be.  Move it below the #endif.
  9. Now back in crosstools-ng, run ./configure –prefix=/home/maks/crosstool.  Yea!!!  It creates a make file.  Now on to making it.  Run “make” now.
  10. Next issue?  The gnu extension strcasestr doesn’t exist in MinGW.  The file ./kconfig/nconf.c uses it.  I am a bit surprised that the configure script didn’t check for that.  After much research, I decided to simply implement it inside the file that needed it.  Add the prototype to the top of the file:
    // Added for support in mingw. This ought to be checked and enabled with autotools.
    const char *strcasestr(const char *s1, const char *s2);

    Add the following to the buttom of the file.

    // Added for support in mingw. This ought to be checked and enabled with autotools.
    const char *strcasestr(const char *s1, const char *s2)
    {
     // if either pointer is null
     if (s1 == 0 || s2 == 0)
      return 0;
     // the length of the needle
     size_t n = strlen(s2);
     // iterate through the string
     while(*s1)
     // if the compare which is case insensitive is a match, return the pointer
     if(!strncmpi(s1++,s2,n))
      return (s1-1);
     // no match was found
     return 0;
    }

    One more thing. If you really wanted to get rid of all warnings, cast the first argument to every bzero call to a void pointer.

  11. Finally, a make and a make install will work.  You will see a bin and lib and other directories in /home/maks/crosstools (or wherever you placed your crosstool.

Next we are finally ready to build our cross compiler.  We will discuss this in our next post.  Why? Because we are sadistic.