使用 riscv gnu 交叉编译器编译多线程 C 示例

compiling multithreading C example with riscv gnu cross compiler

我在 mac machine 上使用 homebrew-riscv 工具链。

我想使用 riscv gnu 交叉编译器编译一个简单的多线程程序,该程序是使用 C 中的 pthread 库编写的。因此,我使用了以下命令:

riscv64-unknown-elf-gcc -march=rv32i -mabi=ilp32 pthreadExample.c -o pthreadExample -lpthread

我收到以下警告和错误:

warning: implicit declaration of function 'pthread_create' [-Wimplicit-function-declaration]
   32 |         pthread_create(&tid, NULL, myThreadFun, (void *)&tid);
      |         ^~~~~~~~~~~~~~
pthreadExample.c:34:5: warning: implicit declaration of function 'pthread_exit' [-Wimplicit-function-declaration]
   34 |     pthread_exit(NULL);
      |     ^~~~~~~~~~~~
/opt/homebrew/Cellar/riscv-gnu-toolchain/master/lib/gcc/riscv64-unknown-elf/11.1.0/../../../../riscv64-unknown-elf/bin/ld: cannot find -lpthread
collect2: error: ld returned 1 exit status

有没有人有解决这个错误的想法?或者我应该使用另一个选项而不是 -lpthread 进行编译?

提前致谢

您使用的是裸机交叉编译器,需要的是:

riscv64-unknown-linux-gnu-gcc

此外,您使用的是 -march=rv32i 标志,但您使用的是 64 位版本的编译器,这对我来说似乎不正确。如果目标机器是 32 位机器,那么也许你应该使用 riscv32-unknown-linux-gnu-gcc