Description
My riscv64-unknown-elf-gcc configuration is
riscv-gnu-toolchain/gcc/configure --target=rble-threads --enable-languages=c,c++ --with-pkgversion=gc891d8dc23e --with-systiscv64-unknown-elf --with-native-system-header-dir=/include --disable-libmudflaable-nls --disable-tm-clone-registry --src=.././gcc --disable-multilib --with-aec=20191213 'CFLAGS_FOR_TARGET=-Os -mcmodel=medlow' 'CXXFLAGS_FOR_TARGET=-Os
Thread model: single
Compile multithreaded C program
#include <stdio.h>
#include <pthread.h>
#include <stdatomic.h>
#include <stdlib.h>
atomic_int x = 0;
atomic_int y = 0;
void* thread1(void* arg) {
atomic_store_explicit(&x, 1, memory_order_release);
int r1 = atomic_load_explicit(&y, memory_order_acquire);
printf("T1: x = %d r1 = %d ", x, r1);
return NULL;
}
void* thread2(void* arg) {
atomic_store_explicit(&y, 1, memory_order_release);
int r2 = atomic_load_explicit(&x, memory_order_acquire);
printf(" T2: y = %d r2 = %d ", y, r2);
return NULL;
}
int main() {
int i, n=20;
pthread_t t1, t2;
for (i = 0; i < n; i++) {
if (pthread_create(&t1, NULL, thread1, NULL)) {
perror("pthread_create:p0");
exit(1);
}
if (pthread_create(&t2, NULL, thread2, NULL)) {
perror("pthread_create:p1");
exit(1);
}
if (pthread_join(t1, NULL)) {
perror("pthread_join:p0");
exit(1);
}
if (pthread_join(t2, NULL)) {
perror("pthread_join:p1");
exit(1);
}
x = 0;
y = 0;
printf("%d\n", i);
}
return 0;
}
error
test_mem_S-L.c: In function 'main':
test_mem_S-L.c:41:11: warning: implicit declaration of function 'pthread_create' [-Wimplicit-function-declaration]
41 | if (pthread_create(&t1, NULL, thread1, NULL)) {
| ^~~~~~~~~~~~~~
test_mem_S-L.c:50:29: warning: implicit declaration of function 'pthread_join'; did you mean 'pthread_atfork'? [-Wimplicit-function-declaration]
50 | if (pthread_join(t1, NULL)) {
| ^~~~~~~~~~~~
| pthread_atfork
/opt/riscv64/lib/gcc/riscv64-unknown-elf/13.2.0/../../../../riscv64-unknown-elf/bin/ld: /tmp/ccD3qZ6r.o: in function main': test_mem_S-L.c:(.text+0x12c): undefined reference to
pthread_create'
/opt/riscv64/lib/gcc/riscv64-unknown-elf/13.2.0/../../../../riscv64-unknown-elf/bin/ld: test_mem_S-L.c:(.text+0x158): undefined reference to pthread_create' /opt/riscv64/lib/gcc/riscv64-unknown-elf/13.2.0/../../../../riscv64-unknown-elf/bin/ld: test_mem_S-L.c:(.text+0x17c): undefined reference to
pthread_join'
/opt/riscv64/lib/gcc/riscv64-unknown-elf/13.2.0/../../../../riscv64-unknown-elf/bin/ld: test_mem_S-L.c:(.text+0x1a0): undefined reference to `pthread_join'
How to make riscv64-unknown-elf-gcc support multi-threaded compilation