riscv-software-src / riscv-tools

RISC-V Tools (ISA Simulator and Tests)
1.13k stars 446 forks source link

ERROR When Compiling C File #329

Closed Louisyoung056 closed 3 years ago

Louisyoung056 commented 3 years ago

Hi,

I followed instructions on https://github.com/riscv/riscv-gnu-toolchain, installed the newest riscv-gnu-toolchain on my CentOS7 virtual machine, and successfully built the Newlib cross-compiler. According to the README, I should then be able to use riscv64-unknown-elf-gcc and its cousins.

$ riscv64-unknown-elf-gcc -v
Using built-in specs.
COLLECT_GCC=riscv64-unknown-elf-gcc
COLLECT_LTO_WRAPPER=/opt/riscv/libexec/gcc/riscv64-unknown-elf/10.2.0/lto-wrapper
Target: riscv64-unknown-elf
Configured with: /home/designer/riscv-gnu-toolchain/riscv-gcc/configure --target=riscv64-unknown-elf --prefix=/opt/riscv --disable-shared --disable-threads --enable-languages=c,c++ --with-system-zlib --enable-tls --with-newlib --with-sysroot=/opt/riscv/riscv64-unknown-elf --with-native-system-header-dir=/include --disable-libmudflap --disable-libssp --disable-libquadmath --disable-libgomp --disable-nls --disable-tm-clone-registry --src=.././riscv-gcc --disable-multilib --with-abi=lp64d --with-arch=rv64imafdc --with-tune=rocket 'CFLAGS_FOR_TARGET=-Os   -mcmodel=medlow' 'CXXFLAGS_FOR_TARGET=-Os   -mcmodel=medlow'
Thread model: single
Supported LTO compression algorithms: zlib
gcc version 10.2.0 (GCC) 

But it shows error message like this when I try to compile my C program(test.c) into machine code:

Commands: riscv64-unknown-elf-gcc -mno-relax -march=rv32imc -mabi=ilp32 -nostartfiles -std=gnu11 -mstrict-align -mno-div -O0 crt.s test.c -T linker.s -o program.elf

Error message:

/opt/riscv64/lib/gcc/riscv64-unknown-elf/10.2.0/../../../../riscv64-unknown-elf/bin/ld: /opt/riscv64/lib/gcc/riscv64-unknown-elf/10.2.0/../../../../riscv64-unknown-elf/lib/libc.a(lib_a-rand.o): **ABI is incompatible with that of the selected emulation:
  target emulation `elf64-littleriscv' does not match `elf32-littleriscv'**
/opt/riscv64/lib/gcc/riscv64-unknown-elf/10.2.0/../../../../riscv64-unknown-elf/bin/ld: failed to merge target specific data of file /opt/riscv64/lib/gcc/riscv64-unknown-elf/10.2.0/../../../../riscv64-unknown-elf/lib/libc.a(lib_a-rand.o)
/opt/riscv64/lib/gcc/riscv64-unknown-elf/10.2.0/../../../../riscv64-unknown-elf/bin/ld: /opt/riscv64/lib/gcc/riscv64-unknown-elf/10.2.0/../../../../riscv64-unknown-elf/lib/libc.a(lib_a-impure.o): ABI is incompatible with that of the selected emulation:
  target emulation `elf64-littleriscv' does not match `elf32-littleriscv'
/opt/riscv64/lib/gcc/riscv64-unknown-elf/10.2.0/../../../../riscv64-unknown-elf/bin/ld: failed to merge target specific data of file /opt/riscv64/lib/gcc/riscv64-unknown-elf/10.2.0/../../../../riscv64-unknown-elf/lib/libc.a(lib_a-impure.o)
collect2: error: ld returned 1 exit status

Is there something wrong with the arguments I added? Any help would be appreciated. Thanks!

jim-wilson commented 3 years ago

The compiler will accept any reasonable arch/abi combination, but the precompiled libraries will only be compiled for the default arch/abi specified at configure time. You can getting an error linking against the precompiled libraries as they were built with a different arch/abi then you specified on the command line.

You can use --enable-multilib at configure time which will build multiple arch/abi versions of the precompiled libraries, but it only builds a subset of them. There are far too many possibilities to be reasonable to build all of them. So this will help only if you are using one of the arch/abi combinations in the default set. rv32imc/ilp32 is not in the default set, but we have a re-use rule that tells gcc to link witih the rv32im/ilp32 precompiled library. If that is OK for you, then --enable-multilib will work for you.

Otherwise, you should specify --with-arch= and --with-abi= at configure time to get a toolchain that directly supports your arch/abi choice directly.

Louisyoung056 commented 3 years ago

The compiler will accept any reasonable arch/abi combination, but the precompiled libraries will only be compiled for the default arch/abi specified at configure time. You can getting an error linking against the precompiled libraries as they were built with a different arch/abi then you specified on the command line.

You can use --enable-multilib at configure time which will build multiple arch/abi versions of the precompiled libraries, but it only builds a subset of them. There are far too many possibilities to be reasonable to build all of them. So this will help only if you are using one of the arch/abi combinations in the default set. rv32imc/ilp32 is not in the default set, but we have a re-use rule that tells gcc to link witih the rv32im/ilp32 precompiled library. If that is OK for you, then --enable-multilib will work for you.

Otherwise, you should specify --with-arch= and --with-abi= at configure time to get a toolchain that directly supports your arch/abi choice directly.

Thanks for the advice! Problem solved using --enable-multilib.