Find the answer to your Linux question:
Results 1 to 3 of 3
Ok so I was messing around trying to learn about threads and how to use them and now I'm trying to compile but I get an error like this: fatal ...
  1. #1
    Just Joined!
    Join Date
    Mar 2011
    Location
    pittsburgh
    Posts
    44

    [SOLVED] Problem with pthread.h, compiling with gcc

    Ok so I was messing around trying to learn about threads and how to use them and now I'm trying to compile but I get an error like this:

    fatal error: pthread: No such file or directory

    however I have included the pthread.h header in my source file and the arguments I'm passing to gcc goes as follows:

    Code:
    gcc thread.c  -o thread -lpthread
    and when I do a
    Code:
    whereis pthread.h
    I get /usr/include/pthread.h so it seems to be here. I'm rather confused.

  2. #2
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    Perhaps this sample will help:
    Code:
    #!/usr/bin/env bash
    
    # @(#) s1	Demonstrate compilation of pthreads code.
    
    # Utility functions: print-as-echo, print-line-with-visual-space.
    pe() { for i;do printf "%s" "$i";done; printf "\n"; }
    pl() { pe;pe "-----" ;pe "$*"; }
    
    FILE=${1-pt.c}
    pl " Sample of $FILE (local command specimen):"
    pe
    specimen $FILE
    
    pl " Results of compiling $( wc -l < $FILE ) lines of file $FILE:"
    gcc -lpthread $FILE
    ls -lgG a.out
    
    exit 0
    producing:
    Code:
    % ./s1
    
    -----
     Sample of pt.c (local command specimen):
    
    Edges: 5:0:5 of 36 lines in file "pt.c"
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    
    void *print_message_function( void *ptr );
       ---
    {
         char *message;
         message = (char *) ptr;
         printf("%s \n", message);
    }
    
    -----
     Results of compiling 36 lines of file pt.c:
    -rwxr-xr-x 1 9617 Mar 11 05:20 a.out
    Code from Linux Tutorial: POSIX Threads q.v.

    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  3. #3
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
    Posts
    8,974
    Quote Originally Posted by bleedingsamurai View Post
    Ok so I was messing around trying to learn about threads and how to use them and now I'm trying to compile but I get an error like this:

    fatal error: pthread: No such file or directory

    however I have included the pthread.h header in my source file and the arguments I'm passing to gcc goes as follows:

    Code:
    gcc thread.c  -o thread -lpthread
    and when I do a
    Code:
    whereis pthread.h
    I get /usr/include/pthread.h so it seems to be here. I'm rather confused.
    On Linux, you need to use -pthread in the compile flags. It will know what libraries to link. IE:
    Code:
    gcc -pthread thread.c -o thread
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...