Discussion:
unable to build gcc 4.9.0 for i686-elf target with posix threads
Ragnar Rüütel
2014-07-02 12:43:27 UTC
Permalink
Hi,

I've been trying to wrap my head around this for weeks and I'm almost
ready to give up. I need a i686-elf compiler to build generic linux
binaries. So, first I created native compiler (like advised here:
http://wiki.osdev.org/Building_GCC) into
/home/ragnar/cpp-tools/compiler/local/gcc. Then exported these:

export CC="/home/ragnar/cpp-tools/compiler/local/gcc/bin/gcc"
export CXX="/home/ragnar/cpp-tools/compiler/local/gcc/bin/g++"
export CPP="/home/ragnar/cpp-tools/compiler/local/gcc/bin/cpp"
export LD="/home/ragnar/cpp-tools/compiler/local/gcc/bin/ld"
export PATH="/home/ragnar/cpp-tools/compiler/local/gcc/bin:$PATH"
export LD_LIBRARY_PATH="/home/ragnar/cpp-tools/compiler/local/gcc/lib32"

and started to build i686-elf compiler (as advised here
http://wiki.osdev.org/GCC_Cross-Compiler) with following commands:

#GMP
$gmp_src/configure
--prefix="/home/ragnar/cpp-tools/compiler/linux32/gcc" --enable-cxx
--disable-shared
make CPPFLAGS="-fexceptions"
make install
#MPFR
$mpfr_src/configure
--prefix="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--with-gmp="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--disable-shared
make
make install
#MPC
$mpc_src/configure
--prefix="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--with-gmp="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--with-mpfr="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--disable-shared
make
make install
export LD_LIBRARY_PATH="/home/ragnar/cpp-tools/compiler/linux32/gcc/lib:$LD_LIBRARY_PATH"
#BINUTILS
$binutils_src/configure
--prefix="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--target=i686-elf --disable-multilib
--with-gmp="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--with-mpfr="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--with-mpc="/home/ragnar/cpp-tools/compiler/linux32/gcc" --disable-nls
--disable-shared --disable-werror
make
make check
make install
#GCC
$gcc_src/configure
--prefix="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--target=i686-elf --enable-32bit --disable-64bit
--enable-languages="c,c++" --disable-multilib --enable-libssp
--enable-threads=posix --disable-nls --disable-shared
--with-gmp="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--with-mpfr="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--with-mpc="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--enable-checking=release --enable-lto
--enable-version-specific-runtime-libs
make all-gcc
make all-target-libgcc

while building libcc I get following error message:
...
/home/ragnar/cpp-tools/compiler/cpp-build-tools/build-linux32/gcc/./gcc/xgcc
-B/home/ragnar/cpp-tools/compiler/cpp-build-tools/build-linux32/gcc/./gcc/
-B/home/ragnar/cpp-tools/compiler/cpp-build-tools/../linux32/gcc/i686-elf/bin/
-B/home/ragnar/cpp-tools/compiler/cpp-build-tools/../linux32/gcc/i686-elf/lib/
-isystem /home/ragnar/cpp-tools/compiler/cpp-build-tools/../linux32/gcc/i686-elf/include
-isystem /home/ragnar/cpp-tools/compiler/cpp-build-tools/../linux32/gcc/i686-elf/sys-include
-g -O2 -O2 -g -O2 -DIN_GCC -DCROSS_DIRECTORY_STRUCTURE -W -Wall
-Wno-narrowing -Wwrite-strings -Wcast-qual -Wstrict-prototypes
-Wmissing-prototypes -Wold-style-definition -isystem ./include
-fpic -g -DIN_LIBGCC2 -fbuilding-libgcc -fno-stack-protector
-Dinhibit_libc -fpic -I. -I. -I../.././gcc
-I/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc
-I/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/.
-I/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/../gcc
-I/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/../include
-DHAVE_CC_TLS -o unwind-dw2.o -MT unwind-dw2.o -MD -MP -MF
unwind-dw2.dep -fexceptions -c
/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/unwind-dw2.c
-fvisibility=hidden -DHIDE_EXPORTS
In file included from
/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/gthr.h:148:0,
from
/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/unwind-dw2.c:37:
./gthr-default.h:35:21: fatal error: pthread.h: No such file or directory
#include <pthread.h>
^
compilation terminated.
make[1]: *** [unwind-dw2.o] Error 1
make[1]: Leaving directory
`/home/ragnar/cpp-tools/compiler/cpp-build-tools/build-linux32/gcc/i686-elf/libgcc'
make: *** [all-target-libgcc] Error 2

when I seach for phtread.h I get following results:
/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc-4.9.0/fixincludes/tests/base/pthread.h
/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc-4.9.0/fixincludes/tests/base/sys/pthread.h
/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc-4.9.0/gcc/config/openbsd-libpthread.h
/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc-4.9.0/gcc/config/i386/mingw-pthread.h
/usr/include/pthread.h

I've attached config.log from
/home/ragnar/cpp-tools/compiler/cpp-build-tools/build-linux32/gcc/i686-elf/libgcc

Any help is greatly appreciated.
Jonathan Wakely
2014-07-02 13:00:36 UTC
Permalink
Post by Ragnar Rüütel
Hi,
I've been trying to wrap my head around this for weeks and I'm almost
ready to give up. I need a i686-elf compiler to build generic linux
binaries.
What do you mean by "generic linux binaries"?
With no dependency on a C library?

Why do you need "i686-elf" rather than e.g. i686-unknown-linux-gnu ?

What are you actually trying to do in the end?
Post by Ragnar Rüütel
So, first I created native compiler
Native to what? What system are you building on?
Post by Ragnar Rüütel
http://wiki.osdev.org/Building_GCC) into
export CC="/home/ragnar/cpp-tools/compiler/local/gcc/bin/gcc"
export CXX="/home/ragnar/cpp-tools/compiler/local/gcc/bin/g++"
export CPP="/home/ragnar/cpp-tools/compiler/local/gcc/bin/cpp"
export LD="/home/ragnar/cpp-tools/compiler/local/gcc/bin/ld"
export PATH="/home/ragnar/cpp-tools/compiler/local/gcc/bin:$PATH"
export LD_LIBRARY_PATH="/home/ragnar/cpp-tools/compiler/local/gcc/lib32"
and started to build i686-elf compiler (as advised here
#GMP
$gmp_src/configure
--prefix="/home/ragnar/cpp-tools/compiler/linux32/gcc" --enable-cxx
--disable-shared
make CPPFLAGS="-fexceptions"
make install
#MPFR
$mpfr_src/configure
--prefix="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--with-gmp="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--disable-shared
make
make install
#MPC
$mpc_src/configure
--prefix="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--with-gmp="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--with-mpfr="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--disable-shared
make
make install
export LD_LIBRARY_PATH="/home/ragnar/cpp-tools/compiler/linux32/gcc/lib:$LD_LIBRARY_PATH"
#BINUTILS
$binutils_src/configure
--prefix="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--target=i686-elf --disable-multilib
--with-gmp="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--with-mpfr="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--with-mpc="/home/ragnar/cpp-tools/compiler/linux32/gcc" --disable-nls
--disable-shared --disable-werror
make
make check
make install
Why did you do this? The instructions you said you're following don't do this.

I no longer help people who install gmp, mpfr and mpc manually and ask for help.
Post by Ragnar Rüütel
#GCC
$gcc_src/configure
--prefix="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--target=i686-elf --enable-32bit --disable-64bit
--enable-languages="c,c++" --disable-multilib --enable-libssp
--enable-threads=posix --disable-nls --disable-shared
--with-gmp="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--with-mpfr="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--with-mpc="/home/ragnar/cpp-tools/compiler/linux32/gcc"
--enable-checking=release --enable-lto
--enable-version-specific-runtime-libs
make all-gcc
make all-target-libgcc
Where is the --without-headers option that the wiki.osdev.org
instructions say to use?

Why are you surprised it doesn't work if you don't follow the
instructions properly?
Ragnar Rüütel
2014-07-02 13:36:06 UTC
Permalink
Hi,

Thanks for the reply and I apologize for my ignorance.
Post by Jonathan Wakely
What do you mean by "generic linux binaries"?
With no dependency on a C library?
Why do you need "i686-elf" rather than e.g. i686-unknown-linux-gnu ?
What are you actually trying to do in the end?
I need to create linux binaries of my tools that will run on any x86
linux distribution. So I can compile them once and copy to other linux
machines. As I understood i686-elf target should be best choice for
this.
Post by Jonathan Wakely
Native to what? What system are you building on?
From osdev tutorials I learned that I should first build compiler
which works on system (this is what I meant with native)
x86_64-unknown-linux-gnu and then use it to build cross compiler.
Post by Jonathan Wakely
Why did you do this? The instructions you said you're following don't do this.
I no longer help people who install gmp, mpfr and mpc manually and ask for help.
I wasn't aware that having gmp, mpfr and mpc inside gcc source
directory doesn't have the same effect as using --with-gmp,
--with-mpfr and -with-mpc. But that's a good point, I'll try it.
Post by Jonathan Wakely
Where is the --without-headers option that the wiki.osdev.org
instructions say to use?
Why are you surprised it doesn't work if you don't follow the
instructions properly?
I noticed that I had --without-headers option missing right after
posting so I added it and tried again. Unfortunately there's not
difference. I still get the same error:

/home/ragnar/cpp-tools/compiler/cpp-build-tools/build-linux32/gcc/./gcc/xgcc
-B/home/ragnar/cpp-tools/compiler/cpp-build-tools/build-linux32/gcc/./gcc/
-B/home/ragnar/cpp-tools/compiler/cpp-build-tools/../linux32/gcc/i686-elf/bin/
-B/home/ragnar/cpp-tools/compiler/cpp-build-tools/../linux32/gcc/i686-elf/lib/
-isystem /home/ragnar/cpp-tools/compiler/cpp-build-tools/../linux32/gcc/i686-elf/include
-isystem /home/ragnar/cpp-tools/compiler/cpp-build-tools/../linux32/gcc/i686-elf/sys-include
-g -O2 -O2 -g -O2 -DIN_GCC -DCROSS_DIRECTORY_STRUCTURE -W -Wall
-Wno-narrowing -Wwrite-strings -Wcast-qual -Wstrict-prototypes
-Wmissing-prototypes -Wold-style-definition -isystem ./include
-fpic -g -DIN_LIBGCC2 -fbuilding-libgcc -fno-stack-protector
-Dinhibit_libc -fpic -I. -I. -I../.././gcc
-I/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc
-I/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/.
-I/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/../gcc
-I/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/../include
-DHAVE_CC_TLS -o unwind-dw2.o -MT unwind-dw2.o -MD -MP -MF
unwind-dw2.dep -fexceptions -c
/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/unwind-dw2.c
-fvisibility=hidden -DHIDE_EXPORTS
In file included from
/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/gthr.h:148:0,
from
/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/unwind-dw2.c:37:
./gthr-default.h:35:21: fatal error: pthread.h: No such file or directory
#include <pthread.h>
^
compilation terminated.
make[1]: *** [unwind-dw2.o] Error 1
make[1]: Leaving directory
`/home/ragnar/cpp-tools/compiler/cpp-build-tools/build-linux32/gcc/i686-elf/libgcc'
make: *** [all-target-libgcc] Error 2
Andrew Haley
2014-07-02 13:42:11 UTC
Permalink
Post by Ragnar Rüütel
Thanks for the reply and I apologize for my ignorance.
Post by Jonathan Wakely
What do you mean by "generic linux binaries"?
With no dependency on a C library?
Why do you need "i686-elf" rather than e.g. i686-unknown-linux-gnu ?
What are you actually trying to do in the end?
I need to create linux binaries of my tools that will run on any x86
linux distribution. So I can compile them once and copy to other linux
machines. As I understood i686-elf target should be best choice for
this.
This is wrong. If you want your binaries to run on linux you must
specify a linux target when you configure gcc.
Post by Ragnar Rüütel
Post by Jonathan Wakely
Why did you do this? The instructions you said you're following don't do this.
I no longer help people who install gmp, mpfr and mpc manually and ask for help.
I wasn't aware that having gmp, mpfr and mpc inside gcc source
directory doesn't have the same effect as using --with-gmp,
--with-mpfr and -with-mpc. But that's a good point, I'll try it.
It's just harder, and you run the risk of version mismatches.

Andrew.
Brian Drummond
2014-07-02 13:56:59 UTC
Permalink
Post by Ragnar Rüütel
Hi,
Thanks for the reply and I apologize for my ignorance.
Post by Jonathan Wakely
What do you mean by "generic linux binaries"?
With no dependency on a C library?
Why do you need "i686-elf" rather than e.g. i686-unknown-linux-gnu ?
What are you actually trying to do in the end?
I need to create linux binaries of my tools that will run on any x86
linux distribution. So I can compile them once and copy to other linux
machines. As I understood i686-elf target should be best choice for
this.
Then you are really using the wrong instructions!
The OSDEV Wiki is helpful for people writing binaries which run without
any operating system! No Linux, no OS at all, just bare metal.

What you need as Andrew says is a compiler targetting Linux : most
likely you can simply install one as a package. Perhaps 2 packages, one
for 32-bit and another for 64 bit (amd64) targets.

Then you need to learn to use it : building to run on any Linux distro,
you probably want to minimise dependence on shared libraries (.so files)
which may not be installed, or the wrong version, on the target system.
So learn to "statically link" your executables. They will be larger,
because they carry their dependencies instead of assuming things about
the distro.

If you DO decide to build your own gcc (targeting Linux), follow
instructions that use the "contrib/download_prerequisites" script. This
is the simplest way round troubles with gmp and friends.

- Brian
Jonathan Wakely
2014-07-02 14:27:17 UTC
Permalink
Post by Ragnar Rüütel
I need to create linux binaries of my tools that will run on any x86
linux distribution. So I can compile them once and copy to other linux
machines. As I understood i686-elf target should be best choice for
this.
No, probably not.

You should either look into static linking, or just compile on a
*really* old GNU/Linux distribution so that it will run on any modern
systems.
Ragnar Rüütel
2014-07-02 14:46:19 UTC
Permalink
So as I understand from You and Brain Drummond, all I need is to set
GCC target as i686-unknown-linux-gnu and link my tools statically and
they should work for majority of linux distributions?
Post by Jonathan Wakely
Post by Ragnar Rüütel
I need to create linux binaries of my tools that will run on any x86
linux distribution. So I can compile them once and copy to other linux
machines. As I understood i686-elf target should be best choice for
this.
No, probably not.
You should either look into static linking, or just compile on a
*really* old GNU/Linux distribution so that it will run on any modern
systems.
Jonathan Wakely
2014-07-02 14:57:44 UTC
Permalink
Post by Ragnar Rüütel
So as I understand from You and Brain Drummond, all I need is to set
GCC target as i686-unknown-linux-gnu and link my tools statically and
they should work for majority of linux distributions?
You don't need a new GCC, just compile with -m32
Ragnar Rüütel
2014-07-02 16:05:49 UTC
Permalink
Post by Jonathan Wakely
You don't need a new GCC, just compile with -m32
Unfortunately, for reasons too awkward to explain here, this is not an
option for me, so I have to build with i686-* target.

Also, using gmp and friends inside GCC source tree didn't help. Here's
what I did:

I executed $gcc_src/contrib/contrib/download_prerequisites and verified that


$binutils_src/configure
--prefix=/home/ragnar/cpp-tools/compiler/linux32/gcc
--target=i686-unknown-linux-gnu --disable-multilib --disable-nls
--disable-shared --disable-werror
make
make check
make install

$gcc_src/configure
--prefix=/home/ragnar/cpp-tools/compiler/linux32/gcc
--target=i686-unknown-linux-gnu --enable-32bit --disable-64bit
--enable-languages="c,c++" --disable-multilib --disable-libssp
--enable-threads=posix --disable-nls --disable-shared
--enable-checking=release --enable-lto
--enable-version-specific-runtime-libs --without-headers
make all-gcc
make all-target-libgcc

And I get exactly the same result:
...
/home/ragnar/cpp-tools/compiler/cpp-build-tools/build-linux32/gcc/./gcc/xgcc
-B/home/ragnar/cpp-tools/compiler/cpp-build-tools/build-linux32/gcc/./gcc/
-B/home/ragnar/cpp-tools/compiler/cpp-build-tools/../linux32/gcc/i686-unknown-linux-gnu/bin/
-B/home/ragnar/cpp-tools/compiler/cpp-build-tools/../linux32/gcc/i686-unknown-linux-gnu/lib/
-isystem /home/ragnar/cpp-tools/compiler/cpp-build-tools/../linux32/gcc/i686-unknown-linux-gnu/include
-isystem /home/ragnar/cpp-tools/compiler/cpp-build-tools/../linux32/gcc/i686-unknown-linux-gnu/sys-include
-g -O2 -O2 -g -O2 -DIN_GCC -DCROSS_DIRECTORY_STRUCTURE -W -Wall
-Wno-narrowing -Wwrite-strings -Wcast-qual -Wstrict-prototypes
-Wmissing-prototypes -Wold-style-definition -isystem ./include
-fpic -mlong-double-80 -g -DIN_LIBGCC2 -fbuilding-libgcc
-fno-stack-protector -Dinhibit_libc -fpic -mlong-double-80 -I. -I.
-I../.././gcc -I/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc
-I/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/.
-I/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/../gcc
-I/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/../include
-DHAVE_CC_TLS -DUSE_TLS -o unwind-dw2.o -MT unwind-dw2.o -MD -MP -MF
unwind-dw2.dep -fexceptions -c
/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/unwind-dw2.c
-fvisibility=hidden -DHIDE_EXPORTS
In file included from
/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/gthr.h:148:0,
from
/home/ragnar/cpp-tools/compiler/cpp-build-tools/sources/gcc/libgcc/unwind-dw2.c:37:
./gthr-default.h:35:21: fatal error: pthread.h: No such file or directory
#include <pthread.h>
^
compilation terminated.
make[1]: *** [unwind-dw2.o] Error 1
make[1]: Leaving directory
`/home/ragnar/cpp-tools/compiler/cpp-build-tools/build-linux32/gcc/i686-unknown-linux-gnu/libgcc'
make: *** [all-target-libgcc] Error 2

Do I need to have external library for posix threads?
Marc Glisse
2014-07-02 16:18:58 UTC
Permalink
Post by Ragnar Rüütel
Post by Jonathan Wakely
You don't need a new GCC, just compile with -m32
Unfortunately, for reasons too awkward to explain here, this is not an
option for me, so I have to build with i686-* target.
Well, don't be surprised if we can't help when we don't know what the
problem is.
Post by Ragnar Rüütel
$binutils_src/configure
--prefix=/home/ragnar/cpp-tools/compiler/linux32/gcc
--target=i686-unknown-linux-gnu --disable-multilib --disable-nls
--disable-shared --disable-werror
Specifying --target means you are building a cross-compiler. If that's
really what you want, you need to specify a sysroot at least.
--
Marc Glisse
Ragnar Rüütel
2014-07-03 15:48:57 UTC
Permalink
Post by Marc Glisse
Specifying --target means you are building a cross-compiler. If that's
really what you want, you need to specify a sysroot at least.
When I add --with-sysroot then make all-gcc complains about system
headers (although I'm using --without-headers).

The directory that should contain system headers does not exist:
/usr/local/build-system/cpp/linux32/gcc/usr/include
make[1]: *** [stmp-fixinc] Error 1
make[1]: Leaving directory
`/usr/local/build-system/cpp/cpp-build-tools/build-linux32/gcc/gcc'
make: *** [all-gcc] Error 2

configure --prefix=/usr/local/build-system/cpp/linux32/gcc
--with-sysroot=/usr/local/build-system/cpp/linux32/gcc
--target=i686-elf --enable-32bit --disable-64bit
--enable-languages=c,c++ --disable-multilib --enable-threads=posix
--disable-nls --disable-shared --enable-checking=release --enable-lto
--enable-version-specific-runtime-libs --without-headers

I'm going try to remove some options to see if that helps.
Andrew Haley
2014-07-03 16:28:41 UTC
Permalink
Post by Ragnar Rüütel
When I add --with-sysroot then make all-gcc complains about system
headers (although I'm using --without-headers).
Well, yes, it will. A Linux cross-compiler needs a sysroot which contains
the headers for the system you're targeting.

Andrew.

Loading...