在debian linux arm-none-eabi-g++ pthread.c -o pthread -lpthread
中运行会抛出编译错误。但是,如果我运行g++ pthread.c -o pthread -lpthread
,则没有编译错误。我从未使用过交叉编译器。我知道在库中链接存在问题。请帮我。我在网上搜索了很多,但没有运气。
我的pthread.c
计划:
include "pthread.h"
include "stdio.h"
include "stdlib.h"
void *worker_thread(void *arg)
{
printf("This is worker_thread()\n");
pthread_exit(NULL);
}
int main()
{
pthread_t my_thread;
int ret;
printf("In main: creating thread\n");
ret = pthread_create(&my_thread, NULL, &worker_thread, NULL);
if(ret != 0)
{
printf("Error: pthread_create() failed\n");
exit(EXIT_FAILURE);
}
pthread_exit(NULL);
}
编译错误:
pthread.c: In function 'void* worker_thread(void*)':
pthread.c:8:18: error: 'pthread_exit' was not declared in this scope
pthread_exit(NULL);
^
pthread.c: In function 'int main()':
pthread.c:15:1: error: 'pthread_t' was not declared in this scope
pthread_t my_thread;
^
pthread.c:15:11: error: expected ';' before 'my_thread'
pthread_t my_thread;
^
pthread.c:18:23: error: 'my_thread' was not declared in this scope
ret = pthread_create(&my_thread, NULL, &worker_thread, NULL);
^
pthread.c:18:60: error: 'pthread_create' was not declared in this scope
ret = pthread_create(&my_thread, NULL, &worker_thread, NULL);
^
pthread.c:24:21: error: 'pthread_exit' was not declared in this scope
} pthread_exit(NULL);
答案 0 :(得分:3)
我假设代码中的前三行是:
#include ...
而不是
include ...
你只是复制/粘贴错误。否则你没有显示你得到的所有错误。
除此之外。
问题很可能是由于您的pthread.h
文件出现了奇怪的问题。使用-E
选项编译程序(这将从预编译器输出结果):
arm-none-eabi-g++ -E pthread.c -o foobar -lpthread
然后查看输出文件foobar
,搜索pthread.h
或仅搜索:
grep 'pthread.h' foobar
这为您提供了交叉编译时包含的pthread.h
文件的完整路径。将其与常规g++
编译器中包含的头文件进行比较。这可能会给你一个关于发生了什么的线索。 (例如,如果它指向您可能已创建的本地pthread.h
。)
例如,在我的系统上,g++
找到了以下pthread.h
:
/usr/include/pthread.h
虽然arm-none-eabi-g++
使用:
/usr/include/newlib/pthread.h
此newlib/pthread.h
未声明任何pthread_exit
和pthread_t
,这是您的问题。
那么,newlib/pthread.h
来自哪里?使用apt-file search newlib/pthread.h
,它会显示这是包libnewlib-dev
的一部分,apt-cache show libnewlib-dev
表示这是:...library intended for use on embedded systems
,这显然是arm
的内容交叉编译器使用。
所以,长话短说:你的手臂交叉编译器不支持线程。
所以,长篇故事,不那么简短: arm-none-eabi-g++
不支持线程化。您可以通过调用arm-none-eabi-g++ -xc -E -v -
来确认编译器的编译方式。它将输出类似
Configured with: ...blablabla... --disable-threads
您确定要定位右臂架构吗?有几种不同臂架构的交叉编译器。