Undefined reference to pthread_create in Linux

后端 未结 14 2319
梦毁少年i
梦毁少年i 2020-11-22 03:18

I picked up the following demo off the web from https://computing.llnl.gov/tutorials/pthreads/

#include 
#include 
#define NU         


        
相关标签:
14条回答
  • 2020-11-22 03:48

    Acutally, it gives several examples of compile commands used for pthreads codes are listed in the table below, if you continue reading the following tutorial:

    https://computing.llnl.gov/tutorials/pthreads/#Compiling

    enter image description here

    0 讨论(0)
  • 2020-11-22 03:51

    Running from the Linux terminal, what worked for me was compiling using the following command (suppose the c file I want to compile is called test.c):

    gcc -o test test.c -pthread
    

    Hope it helps somebody!

    0 讨论(0)
  • 2020-11-22 03:53

    Both answers to this question so far are incorrect.
    For Linux the correct command is:

    gcc -pthread -o term term.c
    

    In general, libraries should follow sources and objects on command line, and -lpthread is not an "option", it's a library specification. On a system with only libpthread.a installed,

    gcc -lpthread ...
    

    will fail to link.

    0 讨论(0)
  • 2020-11-22 03:54

    In Anjuta, go to the Build menu, then Configure Project. In the Configure Options box, add:

    LDFLAGS='-lpthread'
    

    Hope it'll help somebody too...

    0 讨论(0)
  • 2020-11-22 03:55

    check man page and you will get.

    Compile and link with -pthread.

    SYNOPSIS
           #include <pthread.h>
    
           int pthread_create(pthread_t *thread, const pthread_attr_t *attr,
                              void *(*start_routine) (void *), void *arg);
    
    
           Compile and link with -pthread.
           ....
    
    0 讨论(0)
  • 2020-11-22 03:56

    I believe the proper way of adding pthread in CMake is with the following

    find_package (Threads REQUIRED)
    
    target_link_libraries(helloworld
        ${CMAKE_THREAD_LIBS_INIT}
    )
    
    0 讨论(0)
提交回复
热议问题