使用riscv64-unknown-elf-gcc编译多线程程序的编译错误
Compilation error for multi threaded programs compiled using riscv64-unknown-elf-gcc
/* Includes */
#include <unistd.h> /* Symbolic Constants */
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */
#include <pthread.h> /* POSIX Threads */
#include <string.h> /* String handling */
/* prototype for thread routine */
void print_message_function ( void *ptr );
/* struct to hold data to be passed to a thread
this shows how multiple data items can be passed to a thread */
typedef struct str_thdata
{
int thread_no;
char message[100];
} thdata;
int main()
{
pthread_t thread1, thread2; /* thread variables */
thdata data1, data2; /* structs to be passed to threads */
/* initialize data to pass to thread 1 */
data1.thread_no = 1;
strcpy(data1.message, "Hello!");
/* initialize data to pass to thread 2 */
data2.thread_no = 2;
strcpy(data2.message, "Hi!");
/* create threads 1 and 2 */
pthread_create (&thread1, NULL, (void *) &print_message_function, (void *) &data1);
pthread_create (&thread2, NULL, (void *) &print_message_function, (void *) &data2);
/* Main block now waits for both threads to terminate, before it exits
If main block exits, both threads exit, even if the threads have not
finished their work */
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
/* exit */
exit(0);
} /* main() */
/**
* print_message_function is used as the start routine for the threads used
* it accepts a void pointer
**/
void print_message_function ( void *ptr )
{
thdata *data;
data = (thdata *) ptr; /* type cast to a pointer to thdata */
/* do the work */
printf("Thread %d says %s \n", data->thread_no, data->message);
pthread_exit(0); /* exit */
} /* print_message_function ( void *ptr ) */
我遇到的错误是
thread-ex.c:24:5: 错误:未知类型名称 'pthread_t'
pthread_t线程1,线程2; /* 线程变量 */
^
thread-ex.c:36:5: 警告:函数的隐式声明 'pthread_create' [-Wimplicit-function-declaration]
pthread_create (&thread1, NULL, (void *) &print_message_function, (void ) &data1);
^
thread-ex.c:42:5: 警告:函数的隐式声明 'pthread_join' [-Wimplicit-function-declaration]
pthread_join(thread1, NULL);
^
thread-ex.c:在函数'print_message_function'中:
thread-ex.c:61:5: 警告:函数的隐式声明 'pthread_exit' [-Wimplicit-function-declaration]
pthread_exit(0); / 退出 */
pthreads 不适用于基于 newlib 的 RISC-V 编译器。您将需要使用基于 linux 的 RISC-V 编译器 (riscv64-unknown-linux-gnu-gcc)。您可以在同一 riscv-gcc 存储库 (https://github.com/riscv/riscv-gnu-toolchain) 中找到构建和安装基于 linux 的编译器的说明。
/* Includes */
#include <unistd.h> /* Symbolic Constants */
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <stdlib.h> /* General Utilities */
#include <pthread.h> /* POSIX Threads */
#include <string.h> /* String handling */
/* prototype for thread routine */
void print_message_function ( void *ptr );
/* struct to hold data to be passed to a thread
this shows how multiple data items can be passed to a thread */
typedef struct str_thdata
{
int thread_no;
char message[100];
} thdata;
int main()
{
pthread_t thread1, thread2; /* thread variables */
thdata data1, data2; /* structs to be passed to threads */
/* initialize data to pass to thread 1 */
data1.thread_no = 1;
strcpy(data1.message, "Hello!");
/* initialize data to pass to thread 2 */
data2.thread_no = 2;
strcpy(data2.message, "Hi!");
/* create threads 1 and 2 */
pthread_create (&thread1, NULL, (void *) &print_message_function, (void *) &data1);
pthread_create (&thread2, NULL, (void *) &print_message_function, (void *) &data2);
/* Main block now waits for both threads to terminate, before it exits
If main block exits, both threads exit, even if the threads have not
finished their work */
pthread_join(thread1, NULL);
pthread_join(thread2, NULL);
/* exit */
exit(0);
} /* main() */
/**
* print_message_function is used as the start routine for the threads used
* it accepts a void pointer
**/
void print_message_function ( void *ptr )
{
thdata *data;
data = (thdata *) ptr; /* type cast to a pointer to thdata */
/* do the work */
printf("Thread %d says %s \n", data->thread_no, data->message);
pthread_exit(0); /* exit */
} /* print_message_function ( void *ptr ) */
我遇到的错误是 thread-ex.c:24:5: 错误:未知类型名称 'pthread_t' pthread_t线程1,线程2; /* 线程变量 */ ^ thread-ex.c:36:5: 警告:函数的隐式声明 'pthread_create' [-Wimplicit-function-declaration] pthread_create (&thread1, NULL, (void *) &print_message_function, (void ) &data1); ^ thread-ex.c:42:5: 警告:函数的隐式声明 'pthread_join' [-Wimplicit-function-declaration] pthread_join(thread1, NULL); ^ thread-ex.c:在函数'print_message_function'中: thread-ex.c:61:5: 警告:函数的隐式声明 'pthread_exit' [-Wimplicit-function-declaration] pthread_exit(0); / 退出 */
pthreads 不适用于基于 newlib 的 RISC-V 编译器。您将需要使用基于 linux 的 RISC-V 编译器 (riscv64-unknown-linux-gnu-gcc)。您可以在同一 riscv-gcc 存储库 (https://github.com/riscv/riscv-gnu-toolchain) 中找到构建和安装基于 linux 的编译器的说明。