kitsuneFox
kitsuneFox

Reputation: 1271

How to deal with linker error - error - "cannot find -lgcc"

This is my makefile:

task0 : main.o numbers.o add.o
        gcc -m32 -g -Wall -o task0 main.o numbers.o add.o

main.o : main.c
        gcc -g -Wall -m32 -ansi -c -o main.c
numbers.o : numbers.c
             gcc -g -Wall -m32 -ansi -c -o numbers.c
add.o: add.s
      nasm -g -f elf -w+all -o add.o add.s
clean :
        rm -f *.o task0

And this is the terminal output:

gcc -m32 -g -Wall -o task0 main.o numbers.o add.o

Output:

/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc.a when      searching for -lgcc
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.8/libgcc_s.so when searching for -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status
make: *** [task0] 1 הלקת

What is lgcc and how can I fix that?

Upvotes: 24

Views: 38006

Answers (2)

theSparky
theSparky

Reputation: 405

For those using compilers installed at a location other than /usr/bin/ it may be necessary to set the sysroot for the architecture you are targeting.

For example, this:

echo "int main() { return 0; }" > main.c

/arm/4.x/sysroot/x86_64-yodasdk-linux/usr/bin/aarch64-poky-linux/gcc  \
    -mcpu=cortex-a53  -o test_armv8a  main.c

returns:

real-ld: cannot find Scrt1.o: No such file or directory
real-ld: cannot find crti.o: No such file or directory
real-ld: cannot find crtbeginS.o: No such file or directory
real-ld: cannot find -lgcc
real-ld: cannot find -lgcc_s
real-ld: cannot find -lc
real-ld: cannot find -lgcc
real-ld: cannot find -lgcc_s
real-ld: cannot find crtendS.o: No such file or directory
real-ld: cannot find crtn.o: No such file or directory
collect2: error: ld returned 1 exit status

Searching for the standard libraries with:

find /arm/4.x/ -name *gcc*.so 2>/dev/null

returns:

/arm/4.x/sysroots/aarch64-poky-linux/lib/libgcc_s.so
/arm/4.x/sysroots/x86_64-yodasdk-linux/lib/libgcc_s.so

So the libraries for the target device can be specified by compiling with:

/arm/4.x/sysroot/x86_64-yodasdk-linux/usr/bin/aarch64-poky-linux/gcc  \
    --sysroot /arm/4.x/sysroot/aarch64-poky-linux/  \
    -mcpu=cortex-a53  -o test_armv8a  main.c

Or via make: CFLAGS="--sysroot /arm/4.x/sysroot/aarch64-poky-linux/"

Or via cmake: set(CMAKE_SYSROOT "/arm/4.x/sysroot/aarch64-poky-linux/" )

https://gcc.gnu.org/onlinedocs/gcc/Directory-Options.html#index-sysroot

Upvotes: 0

(Answered in a comment. See Question with no answers, but issue solved in the comments (or extended in chat) )

@Basil Starynkevitch wrote:

You should install the gcc-multilib package

@Jekyll wrote:

https://askubuntu.com/questions/250910/cross-compilation-issues-with-gcc-g

Upvotes: 26

Related Questions