Building cross compiler

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
another1
Posts: 8
Joined: Sun May 29, 2016 10:37 am

Building cross compiler

Post by another1 »

Hi,

I have trouble building a cross compiler.
I am using a Ubuntu PC for building the compiler which should run on a Windows PC to produce Elf/Elf64 binaries.
I read the wiki instructions on osdev.org as well on other websites but I fail every time with lots of different erros. I downloaded (binutils-2.26 and) gcc-6.1.0. Also I have installed mingw-w64. I configure gcc with the following command:

Code: Select all

../gcc-6.1.0/configure --prefix=/home/myusername/xc64 --host=x86_64-w64-mingw32 --target=* --enable-languages=c,c++ --disable-nls --disable-shared --without-headers
* Tried different options but I am not sure about the right target name...
Before I post detailed error messages I want to ask you about the correct configure command. As I said.
- Building on Ubuntu 16.04
- Compiler will run on Win x64
- Compiler should build Elf and Elf64 (afaik I need to have to seperate versions of gcc, one for each architecture, right?)

I am working on this problem since the last 3 or 4 days...

Thank you!
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: Building cross compiler

Post by Octocontrabass »

another1 wrote:Tried different options but I am not sure about the right target name...
Typically, one would use i686-elf for a cross-compiler that produces 32-bit executables, and x86_64-elf for a cross-compiler that produces 64-bit executables.
another1 wrote:- Building on Ubuntu 16.04
- Compiler will run on Win x64
Is there any particular reason you don't want to build those compilers on Windows? I've been using MSYS2 to build GCC cross-compilers, and it works pretty well (although it took a few tries to figure out which dependencies I needed; MSYS2 is extremely bare by default). The only potential downside I see is that these compilers might not run outside the MSYS2 shell.

Now that I'm thinking about it, don't you need patches to get a truly native GCC on Windows?
another1
Posts: 8
Joined: Sun May 29, 2016 10:37 am

Re: Building cross compiler

Post by another1 »

Hi, thanks for your answer and please excuse the late responding... I was busy in the last days...

As you said I switched to Windows for building, using msys2 and mingw.
I managed to successfully compile binutils and gcc now. Both for i686-elf and x86_64-elf.
The only thing left are the header files which are missing.
Here is what I did:

Code: Select all

// Binutils
../binutils-2.26/configure --target=i686-elf --build=i686-w64-mingw32 --disable-Werrror --disable-nls --prefix=/L/B/out32
make -j8
make install-strip

// gcc
// Inside gcc-6.1.0:
contrib/download_prerequisites
// Inside gcc build directory
../gcc-6.1.0/configure --prefix=/L/B/out32 --build=i686-w64-mingw32 --target=i686-elf --disable-shared --disable-nls --enable-languages=c,c++
make all-gcc -j8
make all-target-libgcc -j8
make install-strip-gcc
make install-strip-target-libgcc
As I said everything works fine so far.
I have found some C-headers in "[...]\out32\lib\gcc\i686-elf\6.1.0\include" but only a few and C++-headers are missing as well.
I am sure I did not build something I need but I have no idea what it is.
Do I need glibc? Or maybe newlib?

Thank you!
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Building cross compiler

Post by xenos »

I ran into the same problem of missing (freestanding) headers quite some time ago, so nowadays I use newlib to build a full hosted libstdc++ and install that along with GCC, when I actually want a bare metal *-elf targeted compiler.

http://forum.osdev.org/viewtopic.php?f=8&t=23947
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
another1
Posts: 8
Joined: Sun May 29, 2016 10:37 am

Re: Building cross compiler

Post by another1 »

I read the thread you posted and I guess there is no quick solution?
You wrote about adding a solution on wiki if you find some time for that... did you find it? Maybe I searched for the wrong keywords but I could not find anything.
If not, could you give me some guidance? I guess my skills are not good enough (yet) to find a solution by myself...

Thank you.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Building cross compiler

Post by xenos »

I guess the quickest solution I could find is the following, which is more or less straightforward, once you managed to compile gcc:
  • build and install binutils
  • build and install gcc (all-gcc, all-target-libgcc)
  • build and install newlib
  • build and install libstdc++ (all-target-libstdc++v3, configure with --with-newlib option)
I have some setup that works for me, but also includes some other build processes and options, which are not used in the wiki, so I would first need to clean and test this before it can be posted there.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
another1
Posts: 8
Joined: Sun May 29, 2016 10:37 am

Re: Building cross compiler

Post by another1 »

Thanks a lot!
I will try that now
another1
Posts: 8
Joined: Sun May 29, 2016 10:37 am

Re: Building cross compiler

Post by another1 »

Alright, tried it but got an error.

Code: Select all

// Binutils
../binutils-2.26/configure --target=x86_64-elf --enable-targets=x86_64-elf,i686-elf --enable-multilib --build=i686-w64-mingw32 --disable-Werrror --disable-nls --prefix=/L/B/out64
make
make install

// GCC
../gcc-6.1.0/configure --prefix=/L/B/out64 --build=i686-w64-mingw32 --target=x86_64-elf --enable-multilib --enable-targets=i686-elf,x86_64-elf --disable-shared --disable-nls --enable-languages=c,c++ --with-newlib=../newlib-2.4.0.20160527 --without-headers
make all-gcc
make all-target-libgcc
make install-gcc
make install-target-libgcc

// NewLib
../newlib-2.4.0.20160527/configure --build=i686-w64-mingw32 --target=x86_64-elf --prefix=/L/B/out64
make
make install

// Again GCC for libstdc++
make all-target-libstdc++v3
-> make: *** No rule to make target 'all-target-libstdc++v3'.  Stop.
So I guess I am missing something again.
I also used different options for configure this time, based on some things I read on different websites. I am not completly sure what those option do exactly... so please correct me if I did a mistake.
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Building cross compiler

Post by xenos »

I had a typo - that should have been all-target-libstdc++-v3.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
another1
Posts: 8
Joined: Sun May 29, 2016 10:37 am

Re: Building cross compiler

Post by another1 »

Now its working a bit further. I read about all-target-libstdc++-v3 on other websites as well but somehow I managed to miss that "-" sign too :)
Here is the output (I removed everything before the first error I found)

Code: Select all

checking command to parse /L/B/b-gcc/./gcc/nm output from  /L/B/b-gcc/./gcc/xgcc -B/L/B/b-gcc/./gcc/ -B/L/B/out64/x86_64-elf/bin/ -B/L/B/out64/x86_64-elf/lib/ -isystem /L/B/out64/x86_64-elf/include -isystem /L/B/out64/x86_64-elf/sys-include    object... failed
checking for ANSI C header files... yes
checking for sys/types.h... yes
checking for sys/stat.h... yes
checking for stdlib.h... yes
checking for string.h... yes
checking for memory.h... no
checking for strings.h... yes
checking for inttypes.h... yes
checking for stdint.h... yes
checking for unistd.h... yes
checking for dlfcn.h... no
checking for objdir... .libs
checking if  /L/B/b-gcc/./gcc/xgcc -B/L/B/b-gcc/./gcc/ -B/L/B/out64/x86_64-elf/bin/ -B/L/B/out64/x86_64-elf/lib/ -isystem /L/B/out64/x86_64-elf/include -isystem /L/B/out64/x86_64-elf/sys-include    supports -fno-rtti -fno-exceptions... no
checking for  /L/B/b-gcc/./gcc/xgcc -B/L/B/b-gcc/./gcc/ -B/L/B/out64/x86_64-elf/bin/ -B/L/B/out64/x86_64-elf/lib/ -isystem /L/B/out64/x86_64-elf/include -isystem /L/B/out64/x86_64-elf/sys-include    option to produce PIC... -fPIC -DPIC
checking if  /L/B/b-gcc/./gcc/xgcc -B/L/B/b-gcc/./gcc/ -B/L/B/out64/x86_64-elf/bin/ -B/L/B/out64/x86_64-elf/lib/ -isystem /L/B/out64/x86_64-elf/include -isystem /L/B/out64/x86_64-elf/sys-include    PIC flag -fPIC -DPIC works... yes
checking if  /L/B/b-gcc/./gcc/xgcc -B/L/B/b-gcc/./gcc/ -B/L/B/out64/x86_64-elf/bin/ -B/L/B/out64/x86_64-elf/lib/ -isystem /L/B/out64/x86_64-elf/include -isystem /L/B/out64/x86_64-elf/sys-include    static flag -static works... no
checking if  /L/B/b-gcc/./gcc/xgcc -B/L/B/b-gcc/./gcc/ -B/L/B/out64/x86_64-elf/bin/ -B/L/B/out64/x86_64-elf/lib/ -isystem /L/B/out64/x86_64-elf/include -isystem /L/B/out64/x86_64-elf/sys-include    supports -c -o file.o... yes
checking if  /L/B/b-gcc/./gcc/xgcc -B/L/B/b-gcc/./gcc/ -B/L/B/out64/x86_64-elf/bin/ -B/L/B/out64/x86_64-elf/lib/ -isystem /L/B/out64/x86_64-elf/include -isystem /L/B/out64/x86_64-elf/sys-include    supports -c -o file.o... (cached) yes
checking whether the  /L/B/b-gcc/./gcc/xgcc -B/L/B/b-gcc/./gcc/ -B/L/B/out64/x86_64-elf/bin/ -B/L/B/out64/x86_64-elf/lib/ -isystem /L/B/out64/x86_64-elf/include -isystem /L/B/out64/x86_64-elf/sys-include    linker (/L/B/b-gcc/./gcc/collect-ld) supports shared libraries... yes
checking dynamic linker characteristics... no
checking how to hardcode library paths into programs... immediate
checking for shl_load... configure: error: Link tests are not allowed after GCC_NO_EXECUTABLES.
make: *** [Makefile:11462: configure-target-libstdc++-v3] Error 1
Thank you so much for your help!
User avatar
xenos
Member
Member
Posts: 1121
Joined: Thu Aug 11, 2005 11:00 pm
Libera.chat IRC: xenos1984
Location: Tartu, Estonia
Contact:

Re: Building cross compiler

Post by xenos »

Hard to say where that error comes from. The relevant part of my script looks like this, so you can compare and try to modify your build instructions:

Code: Select all

cd build-binutils
../binutils-$binutils_version/configure --target=$target --prefix=/usr/cross/$target --disable-nls
make all
sudo make install
cd ..

cd build-gcc
../gcc-$gcc_version/configure --target=$target --prefix=/usr/cross/$target --disable-nls --enable-languages=c,c++ --enable-libstdcxx --without-headers
make all-gcc
sudo make install-gcc
make all-target-libgcc
sudo make install-target-libgcc
cd ..

sudo ln -s -f /usr/cross/$target/bin/* /usr/local/bin/

cd build-newlib
../newlib-$newlib_version/configure --target=$target --prefix=/usr/cross/$target
make all
sudo make install
cd ..

cd build-gcc
../gcc-$gcc_version/configure --target=$target --prefix=/usr/cross/$target --disable-nls --enable-languages=c,c++ --enable-libstdcxx --without-headers --with-newlib
make all-target-libstdc++-v3
sudo make install-target-libstdc++-v3
cd ..
You'll probably need --enable-libstdcxx when you configure gcc.
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: Building cross compiler

Post by Octocontrabass »

another1 wrote:--enable-multilib --build=i686-w64-mingw32
Can a cross-compiler be multilib? (Why would you want it to be?)

You shouldn't need to specify --build under MSYS2, the configure scripts should automatically detect the correct environment (as long as you're using the MSYS2 shell and not the MinGW shell).
gerryg400
Member
Member
Posts: 1801
Joined: Thu Mar 25, 2010 11:26 pm
Location: Melbourne, Australia

Re: Building cross compiler

Post by gerryg400 »

Octocontrabass, you can use multilib to target, for example, both x86_64 and i386. e.g. enable multilib in your x86_64 cross-compiler so that it can compile grub and your boot loader.
If a trainstation is where trains stop, what is a workstation ?
another1
Posts: 8
Joined: Sun May 29, 2016 10:37 am

Re: Building cross compiler

Post by another1 »

@XenOS:
Thanks you, I will try that!

@Octocontrabass:
I read somewhere it is safer to specify --build because some script could be unable to detect the build environment correctly... But anyway, I think it doesnt hurt if I set this option.
Octocontrabass
Member
Member
Posts: 5587
Joined: Mon Mar 25, 2013 7:01 pm

Re: Building cross compiler

Post by Octocontrabass »

another1 wrote:I read somewhere it is safer to specify --build because some script could be unable to detect the build environment correctly... But anyway, I think it doesnt hurt if I set this option.
It can hurt if you set it wrong. Is your build environment really i686-w64-mingw32? Mine is x86_64-pc-msys. (The script automatically detected it for me.)
Post Reply