2
votes

Based on This Stackoverflow link I downloaded & installed glibc v2.29 in "/usr/local/glibc" path of Linux OS. Then based on this Stackoverflow link I tried to compile This Example, But I got following errors.
First Try Command:

gcc -Wall -g -o main main.c -Wl,--rpath=/usr/local/glibc/lib -Wl,--dynamic-linker=/usr/local/glibc/lib/ld-linux-x86-64.so.2

First Try Error Log:

main.c:1:10: fatal error: threads.h: No such file or directory
#include <threads.h>
         ^~~~~~~~~~~
compilation terminated.

Second Try Command: In second try, I am using "-I" & "-L" GCC command options.

gcc -Wall -g -I/usr/local/glibc/include -o main main.c -L/usr/local/glibc/lib -Wl,--rpath=/usr/local/glibc/lib -Wl,--dynamic-linker=/usr/local/glibc/lib/ld-linux-x86-64.so.2

Second Try Error Log:

/tmp/ccCNYemW.o: In function `main':
/home/.../main.c:14: undefined reference to `thrd_create'
/home/.../main.c:16: undefined reference to `thrd_join'
collect2: error: ld returned 1 exit status

So I don't know where is the problem. Please Help me.

2
Should the second try have a -Wl,-lthread or similar to link in the thread library?cleblanc
What is the "-lthread"? And What is the correct value for "-lthread"? I mainly write my programs in GO & Now trying to learn c, because I like C too.samadadi
Building and installing an alternative glibc is not hard, but actually using it can be very troublesome, not least because in a glibc-based system, pretty much every tool and other library in the entire system depends on glibc.John Bollinger
Sorry didn't realize thrd_create ... came from libc. You might try installing into a docker containercleblanc
Are you sure glibc 2.29 on your machine comes with thrd_create? Can you check if /usr/local/glibc/lib/libc.so.6 has the symbol enabled? You might be more interested in the --sysroot option for gcc, or just create a chroot.KamilCuk

2 Answers

3
votes

First of all, don't put an alternate libc (or alternate version of your libc) in a path searched by the normal include and library search (both link-time and runtime library search) for your main system one. This is a recipe for disaster. Installing a different glibc in /usr/local/ does avoid clobbering your system one, but now you just have two installed in places where the same tools can see and use them.

To do this right, you really need a full separate toolchain (gcc, binutils) in some completely separate path (like ~/my_glibc_root/... or /opt/alt_glibc_root/...). I'm not sure if there's a recommended way to do this. The glibc build procedures in Linux From Scratch might be a good place to look for ideas. In theory it can be done in a single stage; I do that with musl libc in musl-cross-make by careful use of intermediate make rules in the gcc build system. But applying the same idea to glibc probably requires some extra care.

0
votes

Second Try Command: In second try, I am using "-I" & "-L" GCC command options.

gcc -Wall -g -I/usr/local/glibc/include -o main main.c -L/usr/local/glibc/lib -Wl,--rpath=/usr/local/glibc/lib -Wl,--dynamic-linker=/usr/local/glibc/lib/ld-linux-x86-64.so.2

This command is almost correct. The thrd_create and thrd_join functions are defined in libpthread, which you didn't link against.

Add -pthread to your compile command, and the link should succeed.

P.S. R's advice of not installing alternate GLIBC into /usr/local is also a good one.