About linking shared libraries on linux

Question: Question:

When writing a makefile on linux, I'm having trouble because I don't know how to link the following shared libraries.

【Thing you want to do】
[Executable file]-> [libA.so]-> [libB.so] dependency
I want to write -lB in the Makefile for creating libA.so, and write only -lA in the Makefile for creating the executable file side.

【Current status】
With the Makefile I'm writing, if you don't write -lA -lB in the Makefile to create the executable file, you will get a link error.

Would you please tell me if you have a simple Makefile sample?
Thank you.

Answer: Answer:

I added -rpath as prompted by the ld error message and it passed.
Like this.

all: exe

exe: main.c liba.so
    gcc -o exe main.c -L. -la

liba.so: a.c libb.so
    gcc -c -fPIC a.c
    gcc a.o -shared -o liba.so -L. -Wl,-rpath,. -lb

libb.so: b.c
    gcc -c -fPIC b.c
    gcc b.o -shared -o libb.so

    -rm -f exe liba.so libb.so

It is gcc 5.2.0, ld 2.25.1.

Scroll to Top