Linux下編譯tinyxml生成動態庫
首先去到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