MayureshG
MayureshG

Reputation: 364

How to link multiple .so files in a C++ Makefile

My Makefile looks as follows:

CXX = g++
CXXFLAGS = -g

INCLUDES = -Iinclude/

OBJS = a1.o \
   b1.o

LIBPATH= /usr/lib/<arch>

test-app:$(OBJS)
$(CXX) -o $@ $(OBJS)

%.o : %.cpp
$(CXX) $(INCLUDES) -c $(CXXFLAGS) $< -o $@

I want to link two files lib1.so and lib2.so present in LIBPATH? Can anyone please help me with the syntax?

Upvotes: 2

Views: 9904

Answers (2)

mdh.heydari
mdh.heydari

Reputation: 540

Try this one:

LIBRARIES= -llib1 -llib2

...
test-app:$(OBJS)
    $(CXX) -o $@ -L$(LIBPATH) $(LIBRARIES) $(OBJS)

Consider that the arguments order are most of times important since the gcc compiler/linker process the files just one time in the given order and if the order was wrong errors like "Symbol not find" and "undefined reference" will be produced.

Though, I strongly recommend CMake since it's syntax is so easier, more dynamic and It supports many build platforms (IDEs, Compilers, Makefiles, etc.)

Update: This configuration is likely more effective than the above:

SHARED_LIBRARIES= -L/path/to/shared_libs -llib1 -llib2
STATIC_LIBRARIES= -L/path/to/static_libs -llib1 -llib2 -L/another/path/to/static_libs -llib3

...
test-app:$(OBJS)
    $(CXX) -o $@ $(STATIC_LIBRARIES) $(SHARED_LIBRARIES) $(OBJS)

Upvotes: 1

izissise
izissise

Reputation: 943

The syntax is

test-app:$(OBJS)
    $(CXX) -o $@ $(OBJS) -Lpath_to_your_lib -lyour_libname

Also you should use pkg-config to find those variables value.

Upvotes: 1

Related Questions