Tag Archives: gcc

Using crosstool-ng and Cygwin

My goal is to cross compile on Cygwin (on Winderz) for a Linux target – both 64 bit Ubuntu 13.10 or an ARM (such as a Beagle Bone). I sadistically thought that this could be done in MinGW.  Two words: Um, oops.

The real purpose is to take a Windows GUI that generates C code and compile it for a different platform (hence cross compiling).  These are my steps which are based on this guy’s post.

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

  1. Before you begin, it is imperative to set your file system to be case sensitive in Windows.  Both the kernel headers and C library use file names with the same case insensitive name but different case sensitive name.  Open regedit.exe and set the following to 0.
    HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\kernel\obcaseinsensitive

    Then reboot.

  2. Download and Cygwin from here.  We will assume that you installed it on C:\cygwin.  I am using the Cygwin 2.844 32 bit version as the compiler being built because it will run on 32 bit or 64 bit Windows.
  3. When you run setup, you will get a nice GUI to choose your packages.  If you ever want to add or remove a package, you run setup again (seems counter-intuitive on Windows).  Take the defaults and add the following packages (not all may be required but it didn’t hurt).
      • Devel/gperf
      • Devel/bison
      • Devel/flex
      • Devel/patch
      • Devel/make: The GNU version…
      • Devel/automake
      • Devel/libtool
      • Devel/subversion
      • Devel/gcc-core
      • Devel/gcc-g++
      • Devel/catgets
      • Web/wget
      • Libs/libncursesw-devel
      • Libs/libncurses-devel
      • Libs/gettext
      • Libs/libexpat-devel
  4. Open up your cygwin terminal.  If you have a shortcut on your desktop or in your start menu, use that.  If not, the shortcut contains the following target:
    c:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico -
  5. In the terminal, 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
    ./bootstrap
    ./configure --prefix=/home/maks/crosstool
  6. There is one issue with curses.  In crosstool-ng-1.19.0/kconfig/nconf.c, there is the line “ESCDELAY = 1;”.  Swap this line with “set_escdelay(1);”  (A patch for this is listed here.  I did not apply the other two patches and had success building.)
  7. After making the previous correction,  we can make and install.
    make
    make install
  8. To make life easier, export your path to include /home/maks/crosstool/bin substituting your home directory.  I added this to my .bashrc so I wouldn’t have to think about it again.
    export PATH="${PATH}:/home/maks/crosstool/bin"
  9. This is where the patching begins.  Do the following.
    mkdir /usr/include/linux
    cp /usr/include/asm/types.h /usr/include/linux

    Then edit /usr/include/linux/types.h and include the following:

    typedef __signed__ long long __s64;
    typedef unsigned long long __u64;
  10. Make a new directory.  Since I wanted a 64 bit compiler for Linux, I did the following.  Adding the src directory seemingly allows the tarballs to be saved.  (This is not used and seems like a bug in the scripts.)  
  11. mkdir ~/src
    mkdir ~/linux64
    cd ~/linux64
    ct-ng i686-nptl-linux-gnu
    ct-ng menuconfig
  12. In menuconfig, I updated everything to the latest compiler, libraries, 64 bit, eglibc (which Ubuntu uses), etc.  If you want to cheat with menuconfig, use my config.  Simply copy the text into a .config file in the linux64 directory.
  13. You can start building with the following.  I recommend that you read the remainder of the post first.  These are tips that may help out.
    ct-ng build
  14. While building the kernel headers, I came to realize that Cygwin doesn’t have enough elf headers to be successful.  I applied the patch found here.  Make sure the patch applies correctly.  I had some issues.  Also, I had to edit /usr/include/sys/elf_common.h so that R_X86_64_JMP_SLOT was spelled R_X86_64_JUMP_SLOT (only that define).
  15. It turns out that for the latest kernel, a new version of make is needed than what comes with Cygwin.  In menuconfig, add make to the list of companion tools.
  16. Make sure you are not trying to build anything statically.  The final build of the compiler will not succeed.
  17. When you get passed installation of first pass of gcc, you are probably well on your way.  It will take around 2 to 3 hours to fully compile.  You may want to turn off anti-virus protection during this time.
  18. If you come across errors, you can restart ct-ng building where it left off by selecting “Paths and misc options / Debug crosstool-NG / Save intermediate steps.  Then to restart, run
    ct-ng list-steps
    ct-ng <last successful step name>+
  19. I never got D.U.M.A to actually build.  I lost patience trying to figure it out.  However, I never really needed a memory overrun checker in my case.

So that is that.  I compiled a C and C++ program on Windows and ran the binaries on Linux 64 bit Ubuntu 13.10.

Using crosstool-ng on MinGW

Hello sadistic friends.

I have given up.

I am leaving the instructions below but this is plan too difficult and I am moving on to Cygwin.  This is will remain here for simply…  I don’t know…  warnings to others that hair loss is not a good trade-off for getting this to work under MinGW.

We are going to try to build a compiler on Winderz now.  This is a follow up to installing crosstool-ng on MinGW on Winderz.  You should have MinGW already installed.

  1. Open up your msys bash shell the batch file – C:\MinGW\msys\1.0\msys.bat as administrator.  We will assume that you installed MinGW on C:\MinGW.  We will be working from the instructions here.  I installed mine at /home/maks/crosstool.  Always, I repeat, always run the terminal as administrator or some of the decompression of packages will not occur due to permission errors.
  2. Export your tool location by typing
    export PATH="${PATH}:/home/maks/crosstool/bin"

    Substitute your own path because I don’t think your name is Maks.  Now you should be able to run ct-ng now.  In fact, if you create a .profile file in your home directory that includes this line, you will never need to type it again.

  3. Make a directory for your cross compiler.  I called mine linux64 and I placed it in my home directory.  Remember your name is not Maks.  I decided that I am going to build an x86_64_unknown-linux-gnu because I want it to run on an Ubuntu built server 13.10. So in the linux64, type
    ct-ng x86_64-unknown-linux-gnu

    You can read about using ct-ng from the link above.  Then through

    ct-ng menuconfig

    I moved to all the latest versions of everything, eglibc, the 3.10 kernel, binutils, etc.  Also, make sure that your compiler is not linking statically.  This doesn’t work on Windows (and I haven’t tracked down why).

  4. Ok, now run
    ct-ng build

    You will get errors.  We will work through them.

  5. The first error will be that the filesystem is not case sensitive.  We will remedy this by adding the key to the registry.
    HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\kernel\obcaseinsensitive

    Set this value to zero.

  6. The second will be that the OS  MINGW32_NT-6.2 is not supported.  We will add that.  Edit ~/crosstool/lib/ct-ng.1.19.0/scripts/functions and look for a function called CT_DoForceRmdir.  Find the case statement and add MINGW32* to the line.  It should look like this.
    Linux|CYGIN*|MINGW32*)
  7. You may need to create a source directory where all of the tarballs are cached.  Simply
    mkdir ~/src
  8. The next error relates to downloading the tarballs and the use of certificates.  I determined this by looking at build.log.  The utility wget is attempting to validate certificates.  For now, we will simply remote the check.   Edit ~/crosstool/lib/ct-ng.1.19.0/scripts/functions and look for CT_DoGetFile.  On the line that starts with “if CT_DoExecLog ALL wget”, add –no-check-certificate to the command line.  Be mindful for split lines in script using the \.
  9. Oh geez, there is way too much.  Headers aren’t there… ugh.  Too much.  I have given up.  I am going to now try my hand at Cygwin.

 

 

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.