使用pthread编译/链接错误

4
我尝试编写一个使用线程对数组进行排序的小程序,但是我无法使用线程支持将其编译。
错误信息:
sortieren.c:(.text+0xd7): undefined reference to `ptread_create'

我使用了一个make文件来方便编译,但是在命令行中我也不能让它工作。
基本代码:
#include <pthread.h>
int main(int argc, char **argv) {
    pthread_t threads[2];
    // code snipped
    int ret = ptread_create(&threads[0], NULL, threadOne(), NULL);
    printf("ret: %d\n", ret);
    // code snipped
}

制作文件:
sortieren : sortieren.o
    gcc sortieren.o

sortieren.o : sortieren.c 
    gcc -pthread -c sortieren.c

使用 make sortieren 命令会产生以下输出。
gcc -pthread -c sortieren.c
gcc sortieren.o
sortieren.o: In function `main':
sortieren.c:(.text+0xd7): undefined reference to `ptread_create'
collect2: ld returned 1 exit status
make: *** [sortieren] Fehler 1

当然,我尝试了谷歌搜索,但我发现所有找到的“解决方案”对我都不起作用。我在 makefile 中到处尝试使用 -pthread-lpthread。为确保我没有在代码中做错任何事情,我还尝试了一个公共示例
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#define NUM_THREADS 5

void *PrintHello(void *threadid)
{
   long tid;
   tid = (long)threadid;
   printf("Hello World! It's me, thread #%ld!\n", tid);
   pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
  pthread_t threads[NUM_THREADS];
  int rc;
  long t;
  for(t=0;t<NUM_THREADS;t++){
    printf("In main: creating thread %ld\n", t);
    rc = pthread_create(&threads[t], NULL, PrintHello, (void *)t);
    if (rc){
      printf("ERROR; return code from pthread_create() is %d\n", rc);
      exit(-1);
    }
  }
  pthread_exit(NULL);
}

错误与那里相同。
系统:Ubuntu 11.04 64位,GCC版本4.5.2
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/x86_64-linux-gnu/gcc/x86_64-linux-gnu/4.5.2/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.5.2-8ubuntu4' --with-bugurl=file:///usr/share/doc/gcc-4.5/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.5 --enable-shared --enable-multiarch --with-multiarch-defaults=x86_64-linux-gnu --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib/x86_64-linux-gnu --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.5 --libdir=/usr/lib/x86_64-linux-gnu --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-gold --enable-ld=default --with-plugin-ld=ld.gold --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4)

更新:
使用@Banthar提到的方法也不起作用。
$ make sortieren
gcc -c sortieren.c
gcc -lpthread sortieren.o
sortieren.o: In function `main':
sortieren.c:(.text+0xd7): undefined reference to `ptread_create'
collect2: ld returned 1 exit status
make: *** [sortieren] Fehler 1

1
请尝试使用pthread_create而不是ptread_create。 - Shweta
可能是 使用 pthread 在 c++ 中的问题 的重复。 - jww
真是一场灾难!!原本应该是“未声明符号错误”(因为有个打字错误),本应该由编译器自己抛出,但它竟然通过了编译阶段...在链接时,链接器却在抱怨!!这是编译器的不可接受行为!! - Nawaz
2个回答

4
sortieren : sortieren.o
    gcc sortieren.o

sortieren.o : sortieren.c 
    gcc -pthread -c sortieren.c

应该是:

sortieren : sortieren.o
    gcc -lpthread sortieren.o

sortieren.o : sortieren.c 
    gcc -c sortieren.c

但是在此之后运行 gcc sortieren.o 仍然会出现相同的错误。 - WarrenFaith
@Fred Larson: 我已经做了那个,读一下我的问题更新,谢谢迄今为止的帮助! - WarrenFaith
4
你的一处拼写错误,应该是pthread_create而不是ptread_create - Piotr Praszmo
这就解释了为什么你的解决方案在示例代码上起作用但在我的代码上不起作用。谢谢! - WarrenFaith
2
"-lpthread"和其他"-l"选项必须放在链接器命令行的末尾,而不是开头。 - R.. GitHub STOP HELPING ICE
显示剩余2条评论

0
你的拼写错误了,应该是 pthread_create(),我想你可能写成了 ptread_create()。 至少你的错误提示是这样的:undefined reference to ptread_create

在你的主函数中,这一行代码是:int ret = ptread_create(&threads[0], NULL, threadOne(), NULL); - Shweta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接