Compile gcc on amd64, for running on i686, and tar

2019-08-24 06:25发布

问题:

I want to compile gcc and binutils for MIPS target. I am working on 64-bit (amd64) machine. And want to obtain binary which is able to run on i686 (not amd64) arhitecture? How should I condigure and build gcc?

If I am adding --host=i686-linux-gnu to ./configure script, then it complains on absence of i686-xxxx tools.

If I am adding CFLAGS=-m32, then I can build binutils, but not gcc, because of following error:

 g++   -g -O2 -DIN_GCC  -DCROSS_DIRECTORY_STRUCTURE  -fno-exceptions -fno-rtti -fasynchronous-unwind-tables -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wmissing-format-attribute -pedantic -Wno-long-long -Wno-variadic-macros -Wno-overlength-strings   -DHAVE_CONFIG_H -DGENERATOR_FILE -static-libstdc++ -static-libgcc  -o build/genconstants \
build/genconstants.o build/read-md.o build/errors.o ../build-x86_64-unknown-linux-gnu/libiberty/libiberty.a
/usr/bin/ld: i386 architecture of input file `../build-x86_64-unknown-linux-gnu/libiberty/libiberty.a(concat.o)' is incompatible with i386:x86-64 output
/usr/bin/ld: i386 architecture of input file `../build-x86_64-unknown-linux-gnu/libiberty/libiberty.a(fopen_unlocked.o)' is incompatible with i386:x86-64 output
...

回答1:

I found solution, need to pass following environment variables in make command line:

CC="gcc -m32" CXX="g++ -m32" LDFLAGS=-m32

Thats all. Exporting of CFLAGS will not work. You need to export CFLAGS, CXXFLAGS and LDFLAGS, but few pitfalls exists here:

  1. CFLAGS=-m32 also will be exported to MIPS compiler, which have no knowledge about -m32 flag (failure when building libgcc);
  2. CXXFLAGS=-m32 have influence on libraries used by gcc and configure script fails;
  3. You can use ALL_CFLAGS instead, but it doesn't works for gcc itself and libcpp.

See also similar question: Building 32 bit GCC from source on 64 bit: linking issue