【问题标题】:Issue compiling program utilizing pthread.h使用 pthread.h 发布编译程序
【发布时间】:2013-10-08 16:04:04
【问题描述】:

我正在尝试在 Linux (Ubuntu) 机器上编译以下程序:

#include <pthread.h>
#include <stdio.h>
int sum; /* this data is shared by the thread(s) */
void *runner(void *param); /* threads call this function */
int main(int argc, char *argv[])
{
pthread t tid; /* the thread identifier */
pthread attr t attr; /* set of thread attributes */
if (argc != 2) {
fprintf(stderr,"usage: a.out <integer value>\n");
return -1;
}
if (atoi(argv[1]) < 0) {
fprintf(stderr,"%d must be >= 0\n",atoi(argv[1]));
return -1;
}
/* get the default attributes */
pthread attr init(&attr);
/* create the thread */
pthread create(&tid,&attr,runner,argv[1]);
/* wait for the thread to exit */
pthread join(tid,NULL);
printf("sum = %d\n",sum);
}
/* The thread will begin control in this function */
void *runner(void *param)
{
int i, upper = atoi(param);
sum = 0;
for (i = 1; i <= upper; i++)
sum += i;
pthread exit(0);
}

但是,每当我尝试这样做时: gcc -o runner runner.c 我收到一个错误“未知类型名称‘pthread’。 现在我一直在环顾四周,发现我必须包含 -lpthread: gcc -o runner runner.c -lpthread 但是它会产生同样的错误。

我运行了以下命令:whereis pthread,它返回文件位置,所以我知道我有文件。

【问题讨论】:

    标签: c linux ubuntu header-files


    【解决方案1】:

    您缺少一些下划线。 pthread t 应该是 pthread_tpthread_attr_t 应该是 pthread_attr_t 等等。

    【讨论】:

      猜你喜欢
      • 2019-07-27
      • 1970-01-01
      • 1970-01-01
      • 2019-08-03
      • 2012-08-17
      • 1970-01-01
      • 2011-01-10
      • 1970-01-01
      • 2014-08-25
      相关资源
      最近更新 更多