首先去到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即可完成編譯,如下圖所示:

參考資料:
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
