gpt4 book ai didi

riscv - RISCV 工具链是否支持 pthread 库?

转载 作者:行者123 更新时间:2023-12-04 12:52:01 26 4
gpt4 key购买 nike

我已经在我的 ubuntu 机器上克隆并构建了 riscv-tools 存储库。 Hello world 程序运行良好。

现在,我正在尝试将应用程序从 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 社区的任何人都可以重现它吗?有人遇到同样的问题吗?

感谢任何帮助!

最佳答案

对于多线程应用程序,您需要升级到 linux 编译器和内核。代理内核 (pk) 在其运行的用户程序中不支持多线程。此外,我们还没有设置基于 newlib 的编译器来支持 pthreads。

步骤

  1. 构建riscv-linux编译器
  2. 构建 riscv-linux
  3. 使用新的编译器重新编译您的应用程序(可能使用 -static)
  4. 为 linux 构建磁盘镜像
  5. 将您的应用程序包含在磁盘镜像中
  6. 让我们知道进展如何

关于riscv - RISCV 工具链是否支持 pthread 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31595277/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com