Why do I get “undefined reference” errors even when I include the right header files?

前端 未结 5 1437
遇见更好的自我
遇见更好的自我 2021-01-04 02:51

When I tried to compile this program, it failed:

#include 
#include 
#include 
#include 

voi         


        
相关标签:
5条回答
  • 2021-01-04 03:24

    Do

    gcc -pthread -o name filename.c (cpp)
    

    to compile the program, then

    ./name
    

    to run the program.

    0 讨论(0)
  • You need to link pthread library to your binary, like this:

    cc -o myapp myapp.c -lpthread
    
    0 讨论(0)
  • 2021-01-04 03:35

    You probably forgot to link with the Pthreads library (using -lpthread on the command line).

    0 讨论(0)
  • 2021-01-04 03:35

    For folks looking for the csapp solution. Compile "csapp.c" first, then

    gcc -o filename filename.c csapp.o -lpthread
    
    0 讨论(0)
  • 2021-01-04 03:46

    Others have mentioned that you haven't linked with the pthread library using the -lpthread flag. Modern GCC (not sure how modern, mine is 4.3.3) allows you to use just -pthread. From the man page:

    -pthread
    Adds support for multithreading with the pthreads library. This option sets flags for both the preprocessor and linker.

    0 讨论(0)
提交回复
热议问题