如何在C ++ Makefile中链接多个.so文件

时间:2022-05-24 22:44:51

My Makefile looks as follows:

我的Makefile如下所示:

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?

我想链接LIBPATH中存在的两个文件lib1.so和lib2.so?任何人都可以帮我解释一下语法吗?

2 个解决方案

#1


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.

您还应该使用pkg-config来查找这些变量值。

#2


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.

考虑到参数顺序大多数时候都很重要,因为gcc编译器/链接器只按给定顺序处理文件一次,如果顺序错误,将产生“符号未找到”和“未定义引用”之类的错误。

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

虽然,我强烈推荐CMake,因为它的语法更容易,更动态,它支持许多构建平台(IDE,编译器,Makefile等)

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)

#1


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.

您还应该使用pkg-config来查找这些变量值。

#2


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.

考虑到参数顺序大多数时候都很重要,因为gcc编译器/链接器只按给定顺序处理文件一次,如果顺序错误,将产生“符号未找到”和“未定义引用”之类的错误。

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

虽然,我强烈推荐CMake,因为它的语法更容易,更动态,它支持许多构建平台(IDE,编译器,Makefile等)

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)