刚才编译一个pthread的单文件程序, 使用的命令行是:

gcc -o thread1 -lpthread thread1.c 

    结果报错:

1 $ gcc -o thread1 -lpthread thread1.c
2 /tmp/ccNqs6Bh.o: In function `main':
3 thread1.c:(.text+0x49): undefined reference to `pthread_create'
4 thread1.c:(.text+0x5f): undefined reference to `pthread_join'
5 collect2: error: ld returned 1 exit status

   仔细看了一下, 代码编译过了, 链接的时候出的错. 但pthread库是真实存在的.

   而且gcc的语法是:
     Usage: gcc [options] file... 

   是我平常最喜欢的传参方式, 也是按规定的方式传入的, 咋个就不行了呢? 不带这样玩的!

   仔细看了看, 发现一个 `-v --help' 的选项, 于是 gcc -v --help, ~!@#$%^&

   输出结果将近3000行!!!, 吓尿~~~~~~~

   没办法, Google之, 得到了相关说明:
       1. Link order of libraries
       2. C++ shared library - undefined reference

   解决办法:
        把对库的引用放在源文件后面, 因为那是传给链接器的参数!

   要改成这样:
       gcc -o thread1 thread1.c -lpthread 

 

女孩不哭 @ cnblogs.com/memset @ 2014.11.04

 

相关文章:

  • 2021-07-01
  • 2021-09-18
  • 2021-07-04
  • 2021-09-01
  • 2021-08-22
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-10
  • 2021-08-25
  • 2021-08-02
相关资源
相似解决方案