makefile to create a shared object library

my makefile:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
DRIVER_SRC = PumaDriver.cpp PumaBody.cpp PumaMotor.cpp base_manipulation.cpp serialPort.cpp
DRIVER_INC = PumaDriver.h PumaBody.h PumaMotor.h base_manipulation.h serialPort.h silvermax_commands.h

All: $(DRIVER_SRC) $(DRIVER_INC)
        g++ -Wall -Wextra -g3 -fpic `pkg-config --cflags playercore` -c $(DRIVER_SRC)
        g++ -Wall -Wextra -nostartfiles -shared -rdynamic -o libPumaMotorDriver.so PumaDriver.o PumaBody.o PumaMotor.o serialPort.o base_manipulation.o

       
       
install:
        cp *.so /usr/local/lib

uninstall:
        rm -f *.o *.so
        rm /usr/local/lib/libPumaMotorDriver.so

clean:
        rm -f *.o *.so


This makefile is not properly creating the .so file. How do I write a makefile that works with all my dependencies. The dependencies: The driver file is PumaDriver.cpp and it includes PumaDriver.h. PumaDriver.h also includes PumaBody.h. PumaBody.cpp includes PumaBody.h. PumaBody.h includes PumaMotor.h. PumaMotor.cpp includes PumaMotor.h. PumaMotor.h includes serialPort.h, base_manipulation.h, silvermax_commands.h. serialPort.cpp includes serialPort.h. base_manipulation.cpp includes base_manipulation.h.

Thanks for the help.
willydlw
for shared library:


i think you are missing -shared(needed during linking time) and -fPIC (needed at compile time and linking time)

for dependencies:


you need seperate .o tags for each .cpp file.
like:

1
2
3
4
5
6
7
8
main.o: main.cpp
g++ -Wall -c -o main.o main.cpp

file1.o: file1.cpp
g++ -Wall -c -o file1.o file1.cpp

prog: main.o file1.o
g++ -o myprog main.o file1.o


apart from that the makefile will have flags for include paths, library paths.
now to create a shared library you will use -fPIC option during compilation and -fPIC and -shared option during linking(when you make your main binary).


ok. i missed on thing.. dependencies of the headers.. lets say main.cpp is using header1.h and header2.h

and file1.cpp is using header1.h then you do this:

main.o: main.cpp header1.h header2.h
g++ -Wall -c -o main.o main.cpp

file1.o: file1.cpp header1.h
g++ -Wall -c -o file1.o file1.cpp

prog: main.o file1.o
g++ -o myprog main.o file1.o
Topic archived. No new replies allowed.