Undefined: sem_init, sem_post, sem_wait


 
Thread Tools Search this Thread
Top Forums Programming Undefined: sem_init, sem_post, sem_wait
# 1  
Old 12-18-2011
Undefined: sem_init, sem_post, sem_wait

Hi friends,
I am using semaphores in my program, but when I compile the program, it gives the following error


Code:
 
$ gcc sem.c -o sem -lpthread
Undefined                       first referenced
 symbol                             in file
sem_init                            /var/tmp//ccAGJZdT.o
sem_post                            /var/tmp//ccAGJZdT.o
sem_wait                            /var/tmp//ccAGJZdT.o
ld: fatal: Symbol referencing errors. No output written to sem
collect2: ld returned 1 exit status

Or
Code:
$ gcc sem.c -o sem
Undefined first referenced
symbol in file
sem_init /var/tmp//cc3WmF9R.o
sem_post /var/tmp//cc3WmF9R.o
sem_wait /var/tmp//cc3WmF9R.o
ld: fatal: Symbol referencing errors. No output written to sem
collect2: ld returned 1 exit status
$

Here is my program's full source

Code:
 
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <pthread.h>
#include <semaphore.h>
#include <sys/sem.h>
#define MAX_THREAD 100
typedef struct
{
    int start,end;
} param;
sem_t mysem;
void *count(void *arg)
{
    sem_wait(&mysem);
    int i =0;
    param *p=(param *)arg;
    printf("\nprintfrom %d  to  %d\n",p->start,p->end);
    for(i =p->start ; i< p->end ; i++)
    {
        printf(" i = %d",i);sleep(1);
    }
    sem_post(&mysem);
    return (void*)(1);
}
int main(int argc, char *argv[])
{
    if (sem_init(&mysem,0,1))
    {
    perror("init");
    }
    int n,i;
    pthread_t *threads;
    param *p;
    if (argc != 2)
    {
        printf ("Usage: %s n\n",argv[0]);
        printf ("\twhere n is no. of threads\n");
        exit(1);
    }
    n=atoi(argv[1]);
    if ((n < 1) || (n > MAX_THREAD))
    {
        printf ("arg[1] should be 1 - %d.\n",MAX_THREAD);
        exit(1);
    }
    threads=(pthread_t *)malloc(n*sizeof(*threads));
    p=(param *)malloc(sizeof(param)*n); /* Assign args to a struct and start thread */
    for (i=0; i<n; i++)
    {
        p[i].start=i*100;
        p[i].end=(i+1)*100;
        pthread_create(&threads[i],NULL,count,(void *)(p+i));
    }
    printf("\nWait threads\n");
    sleep(1); /* Wait for all threads. */
    int *x = malloc(sizeof(int));
    for (i=0; i<n; i++)
    {
        pthread_join(threads[i],(void*)x);
    }
        free(p);
        exit(0);
return 0;
}

---------- Post updated at 09:34 AM ---------- Previous update was at 02:28 AM ----------

Nobody seems to bother replying to this query, I guess. Anyways, I found the solution to my problem. This commands works now on my Solaris 10 machine.

Code:
gcc sem.c -o sem -lrt

Thanks anyways, and keep up the good work!
# 2  
Old 12-18-2011
For those of you who do not bother to read the man page:
On solaris 10 (not open solaris), the man pages for any C library call will give you the extra required compilation command like -lrt
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Semaphores sem_wait sem_post problem

hallo! if there are many processes running and I initialize the semaphore like this: my_sem = sem_open(SEM_NAME, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 10); (the last argument is 10) and then i use sem_wait(my_sem); sleep(5); sem_post; Will 10 processes be able to access the... (1 Reply)
Discussion started by: whatevernever
1 Replies

2. UNIX for Advanced & Expert Users

Undefined Symbol

When I try to link a .so file I get the undefined symbol error. Though I have the library file in my system. The compile and linking commands used are as follows g++ -m64 -g -Wall -I./include -c dxl_sample.c g++ -o firstprogram -m64 -g -Wall -I./include -Bdynamic... (1 Reply)
Discussion started by: Ranadeep Ghosal
1 Replies

3. Programming

Undefined Symbol

When I try to link a .so file I get the undefined symbol error. Though I have the library file in my system. The compile and linking commands used are as follows g++ -m64 -g -Wall -I./include -c dxl_sample.c g++ -o firstprogram -m64 -g -Wall -I./include -Bdynamic... (1 Reply)
Discussion started by: Ranadeep Ghosal
1 Replies

4. Programming

sem_wait, sem_post confusion?

Hi friends, I have written this small program using the concept of semaphores. I am a bit confused, hope u can help me with it. The idea is that 1. Two threads are created 2. First will be displaying 0(zero) on the screen 3. Second will be displaing 1(one) on the screen 4. This process... (2 Replies)
Discussion started by: gabam
2 Replies

5. Homework & Coursework Questions

undefined reference help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: The problem is a function which i typed although it kept saying that it is a undefined reference still. other... (1 Reply)
Discussion started by: mgyeah
1 Replies

6. Homework & Coursework Questions

undefined reference help

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: i know when undefined reference shows up the program is saying it is not link to that function but the problem... (1 Reply)
Discussion started by: mgyeah
1 Replies

7. Shell Programming and Scripting

undefined error

Hi following is a simple shell script, -------------------------------- #!/usr/bin/tcsh u=$USER echo $u string="cp c2960-lanbase-mz /tftpboot/$u/c2960-lanbase-mz" string2="cp c2960-lanlite-mz /tftpboot/$u/c2960-lanlite-mz" output=/users/$u/c2960-log.txt if && then echo... (1 Reply)
Discussion started by: amitrajvarma
1 Replies

8. Programming

pthread_mutex_init v. sem_init v. sem_get

Apparently there are three similar features in the linux API. Can someone explain the difference between the data structures that are initialized by these functions: (1) pthread_mutex_init (2) sem_init (3) sem_get Thanks, Siegfried (5 Replies)
Discussion started by: siegfried
5 Replies

9. Programming

undefined symbols

i am trying to compile transcode on AIX v 4.3.3 with gcc 3.0.1 i am getting this error: gcc -shared -o .libs/libexport_null.so.0 export_null.o -lpthread -ldl -lc -Wl,-bnoentry -Wl,-bexport:.libs/libexport_null.exp ld: 0711-317 ERROR: Undefined symbol: capability_flag ld: 0711-317... (0 Replies)
Discussion started by: thalex
0 Replies
Login or Register to Ask a Question