Question : C code linking



hi people
I was trying to compile a code using some
user defined shared libraries.
the name of the library he has kept is as

---------------------------------------------------
g++ -lsemphore -L./fifoimplementation semcreate.c

->undefined reference to sem_open(const char *, int. ...)
->undefined referenc to sem_close(sem_t *)

---------------------------------------------------

directory : ./fifoimplementation
***** libsemaphore.so.1,  
      libsemaphore.so -> libsemaphore.so.1

---------------------------------------------------


I used nm command on libsemaphore.so.1
(which is placed in ./fifoimplementation directory)
and it says "sem_close" and "sem_open" declared
with a T (normal) status.

I used gcc -c sem_open.c [to produce the sem_open.o file]
 gcc -c sem_close.c [to produce the sem_close.o file]
 gcc -shared -o libsemaphore.so.1 *.o [to produce
         the .so file]


below are the .c files .. u can cut these and try to
compile the library and use it
---------------------------------------------------

 

 


********************
/*semcreate.c - uses sem_open and sem_close :*/

#include
#include
#include
#include
#include
#include

#include "./fifoimplementation/semaphore.h"

int
main(int argc, char **argv)
{
 int  c, flags;
 sem_t *sem;
 unsigned int value;

 flags = O_RDWR | O_CREAT;
 value = 1;
 while ( (c = getopt(argc, argv, "ei:")) != -1) {
  switch (c) {
  case 'e':
   flags |= O_EXCL;
   break;

  case 'i':
   value = atoi(optarg);
   break;
  }
 }
 if (optind != argc - 1)
  {printf("usage: semcreate [ -e ] [ -i initalvalue ] ");
  exit(0);}

 sem = sem_open(argv[optind], flags, S_IRWXU | S_IROTH | S_IRGRP, value);

 sem_close(sem);
 exit(0);
}
******************************


------------------------------------
when I try to compile it with
g++ -lsemaphore -L./fifoimplementation semcreate.c
is says undefined reference to sem_open and sem_close.
but they are there in the library and also in the header
file included.
-----------------------------------------

 

 

****************************
/*semaphore.h*/
 
typedef struct {
 int sem_fd[2];  /*2 fd's for reading and writing from the FIFO*/
 int sem_magic;  /*magic no*/
} sem_t;


#define SEM_MAGIC 0x92843

#ifdef SEM_FAILED
#undef SEM_FAILED
#define SEM_FAILED ((sem_t *) (-1)) /*this aviods compiler warnings ... I dont know*/
#endif


sem_t * sem_open(const char *, int, ...);
int sem_close(sem_t *);
int sem_wait(sem_t *);
int sem_post(sem_t *);
int sem_unlink(const char *);
***********************

 

 

*********************
/*sem_open.c
opening a semaphore in FIFO implementation
sem_open(const char *pathname, int oflags, ...)
if the O_CREAT flag is there in oflags we need
the rest of the two arguments namely int mode, int value*/

#include
#include
#include
#include
#include
#include
#include
#include


sem_t *sem_open(const char *pathname, int oflags, ...)
{

int save_errno;
va_list ap;
mode_t mode;  /*mode of the file : O_RDWR, O_RDONLY etc*/
unsigned int value;
sem_t *sem;
int flags;
char c;
int i;

if (oflags & O_CREAT ==0) /*we are creating the semaphore */
 {
 va_start(ap, oflags);
 mode = va_arg(ap, mode_t);
 value = va_arg(ap, unsigned int);
 va_end(ap);

 if (mkfifo(pathname, mode)<0) /*the FIFO is created*/
 
if ( (errno ==EEXIST) && (oflags & O_EXCL)==0) /*file already exists and we have not set the O_EXEL flag*/
   oflags = ~O_CREAT;  /* we dont want to write the value of the semaphore */
                                   /*in the fifo as it is already there*/
  else
   return ((sem_t *) -1);
 }
 
 

/*allocate memory to the sem_t structure*/
sem = (sem_t *) malloc(sizeof(sem_t));

/*fifo created*/
( sem->sem_fd[0] = open(pathname, O_RDONLY | O_NONBLOCK) <0)  /*an opening of the FIFO will block if */
 goto error;       /*i.e unlink the fifo created the other end hasen't opened it */
         /*we specify O_NONBLOCK to avoid the deadlock */
         /*and then turn off the O_NONBLOCK option */
         /*to wait for the semaphore values see fifo(4)*/
           

if (sem->sem_fd[1] = open(pathname, O_WRONLY | O_NONBLOCK) <0)
 goto error;

flags = fcntl(sem->sem_fd[0], F_GETFL);     /*get the current flags fot this file*/
flags &= ~O_NONBLOCK;                          /*reset O_NONBLOCK*/
fcntl(sem->sem_fd[0], F_SETFL, flags);       /*reset the flags*/

 

if (oflags & O_CREAT)        /*first time ... initialize the semaphore*/
 {
 for (i=0;i  if (write(sem->sem_fd[1], &c, 1)<0)
   goto error;
 }

sem->sem_magic = SEM_MAGIC;
return (sem);

error:
 save_errno = errno;      /*save errno lest it should get modified */
 if (oflags & O_CREAT ) unlink(pathname);
 close(sem->sem_fd[0]);     /*in this call*/
 close(sem->sem_fd[1]);
 free((void *) sem);
 errno = save_errno;      /*restore it*/
 return ((sem_t *) (-1));


}
********************

 

***********************
/*sem_close.c*/
#include
#include
#include
#include


int sem_close(sem_t *sem)
{

if (sem->sem_magic != SEM_MAGIC)
 {
 errno = EINVAL;
 return (-1);
 }

sem->sem_magic = 0;  /*reset the magic number ... semaphore is now closed*/

if ( close(sem->sem_fd[0] ==-1 ) || close(sem->sem_fd[1] == -1) )
 {
 free(sem);
 return (-1);
 }

free(sem);
return (0);
}

*********************

 

 


-----------------------------------------------------------
if anybody can tell me the commands of how to
make libraries and also how to use the built in libraries,
I will be highly obliged !!!!!!!

Thankzzz
Gaurav.
-------------------------------------------------------------

 

Answer : C code linking

semcreate.o links with the routines in libpthread.so just fine for me.

To compile semcreate.c for the libpthread versions, I would need the interface header files for sem_open() and sem_close() and I either don't have them or don't know what they are.  In fact, I can't find any evidence that those are real interfaces.  The process synchronization calls in Posix threads are the "mutex" routines.  Are you sure these aren't internal library subroutines?
Random Solutions  
 
programming4us programming4us