Thread: What's wrong with sem_init()?

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    100

    Question What's wrong with sem_init()?

    I'm having a bit of an issue with this compiling in Linux with gcc. The command I am typing in is gcc -o <program name> <program name>.c. That has been working, even with these library-dependent data types and stuff, until I included sem_init in the middle.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>
    #include <semaphore.h>
            
    /************************************************************************
    * method:  int main()                                                   *
    * purpose: to serve as the driving/starting method for the program      *
    * returns: 0 to the operating system to signal normal completion        *
    *                                                                       *
    * approach:                                                             *
    ************************************************************************/
    int main()
    {
            /* the semaphores */
            sem_t lowercaseSemaphore; /* for the lowercase letters */
            sem_t capitalSemaphore;   /* for the capital letters */
            sem_t digitSemaphore;     /* for the digits */
    
            /* initializing all the semaphores basically to 1 */
            sem_init(&lowercaseSemaphore, 0, 1);
            sem_init(&capitalSemaphore, 0, 1);
            sem_init(&digitSemaphore, 0, 1);
    
            /* the three character buffers */
            char lowercase; /* for a lowercase letter */
            char capital;   /* for a capital letter */
            char digit;     /* for a digit 0-9 */
    
            return 0;
    }
    The error message ==
    Code:
    /tmp/ccChuvdk.o: In function `main':
    <program name>.c:(.text+0x28): undefined reference to `sem_init'
    <program name>.c:(.text+0x43): undefined reference to `sem_init'
    <program name>.c:(.text+0x5e): undefined reference to `sem_init'
    collect2: ld returned 1 exit status
    What's going on here? I've checked the instructor's example, I've checked the man page for sem_init several times, and I've checked my code several times.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The Linux man page says
    Programs using the POSIX semaphores API must be compiled with cc -lrt to link against the real-time library, librt.
    (by typing "man semaphores" -- edit, sorry: typing "man sem_overview", the "see also" at the bottom of the sem_init main page).

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    These are linking errors, pass "-lpthread" to gcc.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    100
    I looked at that a little bit after reading your reply, and this seems to compile:
    gcc -o <program name> <program name>.c -lrt. Is that the correct way to do it?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 10:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM