c++linuxmakefilecompiler-constructionmultiple-makefiles

Problems linking some libraries with riscv linker


I am trying to compile a set of files with the compiler created here (riscv64-linux). However, I am having some problems that I assume are the result of my misunderstanding with the libraries.

I am including the library libgcc in the Makefile with the flag -lgcc in the LINKER_FLAGS part. However, I get the following output:

riscv64-linux-ld: cannot find -lgcc

When I execute $ riscv64-linux-ld --verbose | grep SEARCH_DIR | tr -s ' ;' \\012 to see where are the riscv64-linux-ld looking for the libraries, I get a multiple paths. One of them is /home/luna/noelv-buildroot/output/host/riscv64-buildroot-linux-musl/lib".

However, when I see the content of that path I can find the libgcc library:

$ ls /home/luna/noelv-buildroot/output/host/riscv64-buildroot-linux-musl/lib
ldscripts    libatomic.la  libatomic.so.1      libgcc_s.so
libatomic.a  libatomic.so  libatomic.so.1.2.0  libgcc_s.so.1". 

This makes me wonder:

  1. Does this really mean that the library is in that path?

  2. How can I add a path to that compiler's SEARCH_DIR?

  3. I've thought that it might happen because it needs a static library. For this, I have tried to link the static libgcc library (libgcc.a) by adding in the Makefile LINKER_INCLUDES = -L/home/laumecha/noelv-buildroot/output/host/lib/gcc/riscv64-buildroot-linux-musl/8.3.0/, where, in this path, we can find libgcc.a. Why does the linker still not find -lgcc?

  4. When I add LINKER_FLAGS += -lm -static -lgcc -nostartfiles -L/home/luna/noelv-buildroot/output/build/host-gcc-final-8.3.0/build/riscv64-buildroot-linux-musl/libgcc/ to the linker flags I got multiples undefined references:

riscv64-linux-ld: /home/luna/multibench_automotive_2.0.2653/builds/riscv64/riscv-gcc64/obj/mith.a(th_al.o): in function `al_printf':
/home/luna/multibench_automotive_2.0.2653/mith/al/src/th_al.c:505: undefined reference to `vsscanf'
riscv64-linux-ld: /home/luna/multibench_automotive_2.0.2653/builds/riscv64/riscv-gcc64/obj/mith.a(th_al.o): in function al_sprintf':
/home/luna/multibench_automotive_2.0.2653/mith/al/src/th_al.c:523: undefined reference to `vfscanf'
riscv64-linux-ld: /home/luna/multibench_automotive_2.0.2653/builds/riscv64/riscv-gcc64/obj/mith.a(th_al.o): in function al_fcreate':
/home/luna/multibench_automotive_2.0.2653/mith/al/src/th_al.c:664: undefined reference to `strchr'
riscv64-linux-ld: /home/luna/multibench_automotive_2.0.2653/builds/riscv64/riscv-gcc64/obj/mith.a(th_al.o): in function `al_fsize':
/home/luna/multibench_automotive_2.0.2653/mith/al/src/th_al.c:693: undefined reference to `stat'
riscv64-linux-ld: /home/luna/multibench_automotive_2.0.2653/mith/al/src/th_al.c:714: undefined reference to `getenv'

Does that means that I am successfully linked the library and I am missing other one?

NOTE: I have not put code from the Makefile because I understand that my problem comes with my basic concepts and the Makefile was made in some official site.


Solution

  • The problem is the linker is looking for libgcc.so or libgcc.a but in your folder you have libgcc_s.so which is not the same.

    You can give a SEARCH_DIR in a linker script or you can use he option -L which is easier.

    All the undefined references are functions from libc. However there is no libc in your folder. You will need to provide a libc or to provide these functions.

    An advice. If you do not know exactly what you are doing, it is better to link with the compiler which knows by default the libraries to use as well as their location.