我已经在我的 ubuntu 机器上克隆并构建了 riscv-tools 存储库。你好世界程序运行良好。
现在,我正在尝试将应用程序从 X86 目标移植到 riscv (RV32IM) 目标。我的应用程序依赖于数学和 pthread 库。尝试在我的代码中使用 pthread.h 头文件中的声明时遇到问题。
我做了一个非常简单的示例代码来演示我的问题。
这是我的 example.c 文件内容
#include <stdio.h>
#include <math.h>
#include <pthread.h>
int main(void)
{
float my_float;
int rc;
pthread_t my_thread;
pthread_mutex_t my_lock;
printf("Example start!\n");
my_float = sqrt( 16.0 );
printf("sqrt(16.0) = %f\n", my_float);
rc = pthread_mutex_init(&my_lock, NULL);
printf("return code from pthread_mutex_init() is %d\n", rc);
printf("Example End!\n");
return 0;
}
好的,这是我为 RISCV 目标编译它的命令行
riscv64-unknown-elf-gcc -Wall -m32 -march=RV32IM -o example example.c -lm -lpthread
这是编译器输出:
example.c: In function 'main':
example.c:10:3: error: unknown type name 'pthread_t'
pthread_t my_thread;
^
example.c:11:3: error: unknown type name 'pthread_mutex_t'
pthread_mutex_t my_lock;
^
example.c:19:3: warning: implicit declaration of function 'pthread_mutex_init' [-Wimplicit-function-declaration]
rc = pthread_mutex_init(&my_lock, NULL);
^
example.c:10:13: warning: unused variable 'my_thread' [-Wunused-variable]
pthread_t my_thread;
^
注意数学库没有问题,但是 pthread 库的东西会产生错误。
显然,为 X86 目标编译这个简单的例子就像一个魅力。X86 目标的程序输出是:
> ./example
Example start!
sqrt(16.0) = 4.000000
return code from pthread_mutex_init() is 0
Example End!
这是我们在执行此操作时在 RISCV 目标上编译和运行时最终应该得到的结果:
spike pk ./example
RISCV 工具链中的 pthread 库有什么问题?RISCV社区的任何人都可以复制它吗?有人遇到同样的问题吗?
任何帮助表示赞赏!