Undefined reference to semaphore functions even though semaphore header is included

This is a discussion on Undefined reference to semaphore functions even though semaphore header is included within the Linux Programming forums, part of the Platform Specific Boards category; Can't seem to figure out this error. I have the semaphore.h file included, but for some reason I keep getting ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    17

    Undefined reference to semaphore functions even though semaphore header is included

    Can't seem to figure out this error. I have the semaphore.h file included, but for some reason I keep getting compiler errors stating it can't identify a reference to functions sem_init(), sem_post(), and sem_wait(). Here's the code I have:

    Code:
    #include <stdlib.h>
    #include <semaphore.h>
    #include <pthread.h>
    #include "buffer.h"
    
    /* POSITION MARKERS (CONCURRENCY ISSUE BECAUSE GLOBAL DATA SHARED AMONG THREADS)*/
    int in_mrkr;                    /* in marker points to the next free position in the buffer */
    int out_mrkr;                   /* out marker points to the first full position in the buffer*/
    
    buffer_item buffer[BUFFER_SIZE];    /* buffer used by producer/consumer pair */
    
    /* SEMAPHORES */
    sem_t empty;                        /* counter indicating all positions of the buffer are available */
    sem_t full;                         /* counter indicating all positions of the buffer are unavailable */
    
    /* MUTEXES */
    pthread_mutex_t mutex;              /* lock for allowing consumer or producer to access buffer exclusively */
    
    void buffer_init(void) {
        /* initialize markers */
        in_mrkr = 0;
        out_mrkr = 0;
    
        /* initialize semphores */
        if (sem_init(&empty, 0, BUFFER_SIZE))
            print_err();
    
        if (sem_init(&full, 0, 0))
            print_err();
    
        /* initialize mutex lock */
        if (pthread_mutex_init(&mutex, NULL))
            print_err();
    }
    
    int insert_item(buffer_item item) {
        /* decrement empty semaphore for producer */
        sem_wait(&empty);
    
        /* lock thread so producer can add item to the buffer */
        pthread_mutex_lock(&mutex);
    
        //--- PRODUCER CRITICAL SECTION ---
        /* add item to buffer and move the in marker to next free position */
        buffer[in_mrkr] = item;
        in_mrkr = (in_mrkr + 1) % BUFFER_SIZE;
    
        /* release the lock on the producer thread */
        pthread_mutex_unlock(&mutex);
    
        /* increment full semaphore to inidicate buffer has a filled slot */
        sem_post(&full);
    
        return 0;       // NEED ERROR CHECKING -- indicate successfull return for now
    }
    
    int remove_item(buffer_item *item) {
        /* decrement full semaphore for consumer */
        sem_wait(&full);
    
        /* lock thread so consumer can pull the item off the buffer */
        pthread_mutex_lock(&mutex);
    
        //--- CONSUMER CRITICAL SECTION ---
        /* remove an item from the buffer and move the out marker to next full position */
        *item = buffer[out_mrkr];
        out_mrkr = (out_mrkr + 1) % BUFFER_SIZE;
    
        /* release the lock on the consumer thread */
        pthread_mutex_unlock(&mutex);
    
        /* increment empty semaphore to inidicate buffer has a empty slot */
        int a = sem_post(&empty);
    
        return 0;       // NEED ERROR CHECKING -- indicate successfull return for now
    }
    I checked the semaphore header and it contains the prototype definitions as follows:

    Code:
    /* Initialize semaphore object SEM to VALUE.  If PSHARED then share it
       with other processes.  */
    extern int sem_init (sem_t *__sem, int __pshared, unsigned int __value)
         __THROW;
    
    /* Wait for SEM being posted.
    
       This function is a cancellation point and therefore not marked with
       __THROW.  */
    extern int sem_wait (sem_t *__sem);
    
    /* Post SEM.  */
    extern int sem_post (sem_t *__sem) __THROW;
    I don't understand why I'm getting errors like this from the compiler

    In function `buffer_init':
    /home/jshowa/NetBeansProjects/producer_consumer_prob/buffer.c:40: undefined reference to `sem_init'
    /home/jshowa/NetBeansProjects/producer_consumer_prob/buffer.c:43: undefined reference to `sem_init'


    There's error for the insert_item and remove_item functions that contain sem_wait() and sem_post(), but the error is the same.

    If anyone could shed some light on this, I'd appreciate it, thanks.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,660
    With gcc, I would compile as:
    gcc foo.c -Wall -pedantic -std=c99 -D_XOPEN_SOURCE=700 -pthread

    On Linux, "-pthread" will link the libs needed for semaphores and pthreads, as well as any necessary platform-specific defines and libraries used in multi-threading.

    On other platforms just check the man pages for sem_init. On Linux, sem_init is in the "rt" library (-lrt).
    sem_init(3) - Linux manual page

    gg

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    17
    Wow, didn't even notice that. I was checking the description of sem_init() in the Open Group Base Specification document and it says nothing about having to link it with pthreads on compilation.

    The Open Group Base Specifications Issue 7

    It worked thought. Thanks for the help Codeplug.

  4. #4
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,660
    http://www.opengroup.org/onlinepubs/...ities/c99.html

    So there is a standard Posix interface for compilation, but I think most folks just use their native compiler interface directly - which may not be identical to the Posix interface.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern defined functions
    By williamsonj in forum C Programming
    Replies: 6
    Last Post: 04-23-2010, 11:05 PM
  2. Replies: 7
    Last Post: 10-09-2007, 02:37 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21