Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile for creating a library in Linux doesn't compile

I have 3 files , my_pipe.h , my_pipe.c , and main.c , where my_pipe is supposed to be a library .

When I compile it in Eclipse , it compiles great , with no errors , but when I run the following makefile in terminal and hit make :

exer3:  main.o libmywrapper.a

    gcc main.c libmywrapper.a -o exer3 -static -lrt



libmywrapper.a: my_pipe.o

    ar rcs libmywrapper.a my_pipe.o



main.o: main.c my_pipe.h

    gcc -lpthread -lrt -c main.c



my_pipe.o:  my_pipe.c my_pipe.h

    gcc -lpthread -lrt -c my_pipe.c

I get this :

a@ubuntu:~/Desktop/myExer$ make
gcc -lpthread -lrt -c main.c
gcc -lpthread -lrt -c my_pipe.c
ar rcs libmywrapper.a my_pipe.o
gcc main.c libmywrapper.a -o exer3 -static -lrt
libmywrapper.a(my_pipe.o): In function `shm_pipe_init':
my_pipe.c:(.text+0x61): undefined reference to `sem_init'
libmywrapper.a(my_pipe.o): In function `shm_pipe_read':
my_pipe.c:(.text+0x17f): undefined reference to `sem_wait'
my_pipe.c:(.text+0x196): undefined reference to `sem_getvalue'
my_pipe.c:(.text+0x1ba): undefined reference to `sem_wait'
libmywrapper.a(my_pipe.o): In function `shm_pipe_write':
my_pipe.c:(.text+0x4b7): undefined reference to `sem_post'
collect2: ld returned 1 exit status
make: *** [exer3] Error 1

Any idea what's wrong with the makefile ?

Thanks

UPDATED , above!

like image 385
JAN Avatar asked Jun 20 '26 08:06

JAN


2 Answers

Linker options such as -lpthread and -lrt must go last in you compile line. Try:

gcc main.o libmywrapper.a -o exer3 -static -lrt

When compiling, you don't need linker flags. E.g.:

main.o: main.c my_pipe.h
  gcc -c main.c
like image 141
steffen Avatar answered Jun 22 '26 22:06

steffen


Here is the solution :

exer3:  main.o sharedMemoryLib.a

    gcc main.o sharedMemoryLib.a -o exer3 -static -lrt -lpthread



sharedMemoryLib.a:  my_pipe.o

    ar rcs sharedMemoryLib.a my_pipe.o



main.o: main.c my_pipe.h

    gcc -c main.c



my_pipe.o:  my_pipe.c my_pipe.h

    gcc -c my_pipe.c
like image 33
JAN Avatar answered Jun 22 '26 22:06

JAN



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!