Linux下编译tinyxml生成动态库

时间:2023-12-24 16:41:31

首先去到sourceforge下载tinyxml的源码,https://sourceforge.net/projects/tinyxml/?source=dlp,最新版本是2.6.2。

将下载成功的tinyxml_2_6_2.zip解压,接下来对Makefile进行修改,下方仅列出需要调整的部分:

DEBUG          := YES

DEBUG设置为YES后,下方命令中可以看到在编译生成中间文件以及最终的可执行文件时会添加-g选项,这样产生core时函数调用栈里可以看到详细的代码行号。

TINYXML_USE_STL := YES

从默认Makefile中的注释就不难看出,该项设置为YES后就会引用STL库,这个没什么疑问。

OUTPUT := libtinyxml.so

现在的目标就是生成动态库,供其他项目使用,这里也没有疑问,将 xmltest 替换为动态库的名称 libtinyxml.so。

SRCS := tinyxml.cpp tinyxmlparser.cpp tinyxmlerror.cpp tinystr.cpp

现在仅需编译提供功能的动态库,不需要编译测试文件,去掉xmltest.cpp。需要注意的是,移除源文件后需要将中间文件生成的指令也同步移除。

${OUTPUT}: ${OBJS}
${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS} -fPIC -shared

在将中间文件.o通过g++ -o生成目标动态库的命令中,添加选项-fPIC -shared。两个选项分别表示生成位置无关代码以及生成动态库。

DEBUG_CFLAGS     := -Wall -Wno-format -g -DDEBUG -fPIC

需要注意的是,如果生成.o文件时漏了加上-fPIC选项,那么在g++ -o生成动态库时,会提示中间文件没有通过-fPIC编译生成,需要重新编译。

下面给出此时完整的Makefile文件内容:

#****************************************************************************
#
# Makefile for TinyXml test.
# Lee Thomason
# www.grinninglizard.com
#
# This is a GNU make (gmake) makefile
#**************************************************************************** # DEBUG can be set to YES to include debugging info, or NO otherwise
DEBUG := YES # PROFILE can be set to YES to include profiling info, or NO otherwise
PROFILE := NO # TINYXML_USE_STL can be used to turn on STL support. NO, then STL
# will not be used. YES will include the STL files.
TINYXML_USE_STL := YES #**************************************************************************** CC := gcc
CXX := g++
LD := g++
AR := ar rc
RANLIB := ranlib DEBUG_CFLAGS := -Wall -Wno-format -g -DDEBUG -fPIC
RELEASE_CFLAGS := -Wall -Wno-unknown-pragmas -Wno-format -O3 LIBS := DEBUG_CXXFLAGS := ${DEBUG_CFLAGS}
RELEASE_CXXFLAGS := ${RELEASE_CFLAGS} DEBUG_LDFLAGS := -g
RELEASE_LDFLAGS := ifeq (YES, ${DEBUG})
CFLAGS := ${DEBUG_CFLAGS}
CXXFLAGS := ${DEBUG_CXXFLAGS}
LDFLAGS := ${DEBUG_LDFLAGS}
else
CFLAGS := ${RELEASE_CFLAGS}
CXXFLAGS := ${RELEASE_CXXFLAGS}
LDFLAGS := ${RELEASE_LDFLAGS}
endif ifeq (YES, ${PROFILE})
CFLAGS := ${CFLAGS} -pg -O3
CXXFLAGS := ${CXXFLAGS} -pg -O3
LDFLAGS := ${LDFLAGS} -pg
endif #****************************************************************************
# Preprocessor directives
#**************************************************************************** ifeq (YES, ${TINYXML_USE_STL})
DEFS := -DTIXML_USE_STL
else
DEFS :=
endif #****************************************************************************
# Include paths
#**************************************************************************** #INCS := -I/usr/include/g++-2 -I/usr/local/include
INCS := #****************************************************************************
# Makefile code common to all platforms
#**************************************************************************** CFLAGS := ${CFLAGS} ${DEFS}
CXXFLAGS := ${CXXFLAGS} ${DEFS} #****************************************************************************
# Targets of the build
#**************************************************************************** OUTPUT := libtinyxml.so all: ${OUTPUT} #****************************************************************************
# Source files
#**************************************************************************** SRCS := tinyxml.cpp tinyxmlparser.cpp tinyxmlerror.cpp tinystr.cpp # Add on the sources for libraries
SRCS := ${SRCS} OBJS := $(addsuffix .o,$(basename ${SRCS})) #****************************************************************************
# Output
#**************************************************************************** ${OUTPUT}: ${OBJS}
${LD} -o $@ ${LDFLAGS} ${OBJS} ${LIBS} ${EXTRA_LIBS} -fPIC -shared #****************************************************************************
# common rules
#**************************************************************************** # Rules for compiling source files to object files
%.o : %.cpp
${CXX} -c ${CXXFLAGS} ${INCS} $< -o $@ %.o : %.c
${CC} -c ${CFLAGS} ${INCS} $< -o $@ dist:
bash makedistlinux clean:
-rm -f core ${OBJS} ${OUTPUT} depend:
#makedepend ${INCS} ${SRCS} tinyxml.o: tinyxml.h tinystr.h
tinyxmlparser.o: tinyxml.h tinystr.h
tinyxmlerror.o: tinyxml.h tinystr.h

将整个目录(或者可以剔除一些无关的工程结构文件以及说明文档)上传到Linux服务器,执行make即可完成编译,如下图所示:

Linux下编译tinyxml生成动态库

参考资料:

https://www.cnblogs.com/fengliu-/p/10216878.html

https://www.cnblogs.com/chutianyao/archive/2012/07/09/2582941.html

https://blog.csdn.net/linchuran/article/details/52702640