0%

GCC静态库链接顺序

工程中遇到问题,修改配置文件之后发现编译不过,部分符号未定义,检查Makefile如下:

LDFLAGS += -lui -lenca -lxml -lgxbox -lstdc++ -ljansson -lcurl
...
LDFLAGS += -L$(GXSRC_PATH)/lib/$(THIRD_LIB)/ -lkolatv -lpcre

其中kolatv库需要调用jansson库gxbox库也需要调用jansson库

在修改配置文件之前,开启了gxbox的支持,在编译中需要链接gxbox库,从而jansson库可以链接成功。

在修改配置文件之后,关闭了gxbox的支持,在编译中不会编译gxbox代码,gxbox库链接失败,
从而jansson库链接失败,在载入kolatv库时不存在jansson库的符号表。

GCC:

-l library

Search the library named library when linking.
(The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)

It makes a difference where in the command you write this option;
the linker searches and processes libraries and object files in the order they are specified.
Thus, foo.o -lz bar.o' searches libraryz’ after file foo.o but before bar.o.
If bar.o refers to functions in `z’, those functions may not be loaded.

因此,在链接过程中,链接的库,一定要在使用该库的目标被连接之后,基本的库要放在最后。

修改如下:

LDFLAGS += -lui -lenca -lxml -lkolatv -lgxbox -lstdc++ -ljansson -lcurl