g++编译指令备忘
-fpic:表明使用地址无关代码.适用于共享库(shared library).
-fPIC:表明使用地址无关代码,适用于动态连接(dynamic linking)。PIC:Position Independent Code.
-shared:表明产生共享库,Linux下编译共享库时,必须加上-fPIC参数(常用-shared -fPIC)
-I:指定额外编译时头文件目录(-I../include 或者 -I ../include)
-L:指定额外编译时链接库目录(-L../lib 或者 -L ../lib),与运行时库目录不同
-g:指示编译器,在编译的时候,产生调试信息
-w:关闭所有警告,不建议使用
-s:Remove all symbol table and relocation information from the executable.
-o:指定目标名称,缺省的时候,gcc/g++ 编译出来的文件是a.out
-O[0~3]:编译器优化选项,1为默认值,0没有优化,3优化最大,优化越强编译时间越长,运行时可能更快
-Wall:一般使用该选项,允许发出GCC能够提供的所有有用的警告。也可以用-W{warning}来标记指定的警告
-Wl,-Bstatic:表明后续所有库为静态库(动态库静态库混编结合-Wl,-Bdynamic使用)
-Wl,-Bdynamic: 表明后续所有库为动态库
-Wl,--as-needed:去除没有使用的动态库
-Wl,--no-as-needed:不去除没有使用的动态库
-Wl,--rpath:指定运行时额外库目录(-Wl,--rpath=../lib)
或者终端下export临时指定库目录
或者在/etc/ld.so.conf中添加库目录路径,然后sudo ldconfig使修改可用
-Wl,--no-copy-dt-needed-entries : binutils2.22开始,其中的ld开始把--no-copy-dt-needed-entries默认打开,这样一来,ld不会再自动递归地解析链接的lib,而需要由用户来一一指定。
因为链接库顺序错误示例:
/usr/bin/ld: /tmp/ccbb2XTW.o: undefined reference to symbol 'AdjustLogLevel'
../lib/liblogsystem.so: error adding symbols: DSO missing from command line
-Wl,--copy-dt-needed-entries: 与--no-copy-dt-needed-entries相反,递归的链接库,不需要关心链接库的顺序。
参考:
示例:
#made by dyan SOURCES = $(wildcard *.cpp) OBJS = $(patsubst %.cpp,build/%.o,$(SOURCES)) ../tool/database/DataBase.o SO = libvoiceInterface.so INCLUDE_TOOL = ../tool INCLUDE_DIR1 = ./include INCLUDE_DIR2 = ../include INCLUDE_DIR3 = ../tool/aiui INCLUDE_DIR4 = ../tool/database INCLUDE_DIR5 = ../tool/hpr BUILD_DIR = ./build LIBS_DIR = ../lib CXX = g++ INCLUDES = -I$(INCLUDE_TOOL) -I$(INCLUDE_DIR1) -I$(INCLUDE_DIR2) -I$(INCLUDE_DIR4) -I$(INCLUDE_DIR5) LIBS_COMPILE_DIR = -L$(LIBS_DIR) DEBUG = -g -Wall NODEBUG = -O2 -s SO_COMPILE_FLAG = -shared -fPIC SO_COMPILE = $(LIBS_COMPILE_DIR) $(SO_COMPILE_FLAG) SO_FLAGS = $(SO_COMPILE) $(NODEBUG) $(INCLUDES) #webtrc ns 源码 DIR_NS = ../tool/webrtc/webrtc_ns SPEECHNS = $(DIR_NS)/noise_suppression.c $(DIR_NS)/ns_core.c $(DIR_NS)/fft4g.c #编译libvoiceInterface.so所需的动态库 LIBS_INTERFACE = -lns -lvad -ljsoncpp -lmsc -llogsystem -lhpr -ldl -laiui -lpthread -lxml -lcurl -lasound -lcrypto -lmysqlclient -lnav#-ldl -lmysqlclient all: $(SO) $(SO): $(OBJS) $(CXX) $(SO_FLAGS) $^ $(LIBS_INTERFACE) -o $(LIBS_DIR)/$@ -std=c++11 ../tool/database/DataBase.o: ../tool/database/DataBase.cpp $(CXX) $(SO_FLAGS) -c $< -o $@ -std=c++11 build/%.o: %.cpp mkdir -p build $(CXX) $(SO_FLAGS) -c $< -o $@ -std=c++11 libns.so: $(SPEECHNS) $(CXX) $(SO_COMPILE_FLAG) -I$(INCLUDE_TOOL) $^ -o $(LIBS_DIR)/$@ -std=c++11 C_FLAGS = $(INCLUDES) $(LIBS_COMPILE_DIR) LIBS_RUN = -Wl,--rpath=$(LIBS_DIR) DEMO_DIR = ../demo LIBS_TEST = -ljsoncpp -ljson -llogsystem -lhpr -ldl -lmsc -laiui iFlyBase_LIB = -lmsc -ldl -llogsystem -ljsoncpp -ljson LIBS_DEMO_INTERFACE = -ljsoncpp -lvad -lmsc -llogsystem -lhpr -ldl -laiui -lpthread -lxml -lcurl -lasound -lcrypto -lmysqlclient #编译源码 SOURCES_INTERFACE = VoiceInterface1.cpp ReadConfig.cpp RecogIFlyOffline.cpp RecogIFlyOnline.cpp RecogBaiduOnline.cpp IFlyBase.cpp Text2pcmIFlyOffline.cpp ResponseTulingOnline.cpp ResponseOffline1.cpp ../tool/database/DataBase.cpp UdpClient.cpp Audio.cpp SpeechSample.cpp SpeechNS.cpp $(SPEECHNS) #完成:未编译链接库,使用源代码编译的demo demo_voiceInterface: $(DEMO_DIR)/demo_voiceInterface.cpp $(SOURCES_INTERFACE) mkdir -p bin $(CXX) -w $(C_FLAGS) $^ $(LIBS_DEMO_INTERFACE) $(LIBS_RUN) -o bin/$@ -std=c++11 #完成:使用libvoiceInterface.so的demo demo_VI: $(DEMO_DIR)/demo_voiceInterface.cpp mkdir -p bin $(CXX) -Wl,--copy-dt-needed-entries $(C_FLAGS) $^ -g -lvoiceInterface $(LIBS_RUN) -o bin/$@ -std=c++11 #完成 demo_iFlyRecogOnline: $(DEMO_DIR)/demo_iFlyRecogOnline.cpp ReadConfig.cpp RecogIFlyOnline.cpp IFlyBase.cpp#iFlyBase.cpp RegexMatch.cpp VoiceInterface.cpp RecogIFlyOffline.cpp ResponseTulingOnline.cpp ResponseOffline.cpp mkdir -p build $(CXX) -g $(C_FLAGS) $^ $(LIBS_TEST) $(LIBS_RUN) -o bin/$@ -std=c++11 #完成 demo_iFlyBase: $(DEMO_DIR)/demo_iFlyBase.cpp ReadConfig.cpp IFlyBase.cpp mkdir -p build $(CXX) -g $(C_FLAGS) $^ $(iFlyBase_LIB) $(LIBS_RUN) -o bin/$@ -std=c++11 #有问题 demo_iFlyResponseOnline: $(DEMO_DIR)/demo_iFlyResponseOnline.cpp ReadConfig.cpp IFlyBase.cpp ResponseIFlyOnline.cpp Listener.cpp mkdir -p build $(CXX) -g $(C_FLAGS) $^ $(LIBS_TEST) $(LIBS_RUN) -o bin/$@ -std=c++11 #完成 demo_text2wave:$(DEMO_DIR)/demo_text2wave.cpp IFlyBase.cpp ReadConfig.cpp Text2pcmIFlyOffline.cpp RecogIFlyOnline.cpp mkdir -p build $(CXX) -g $(C_FLAGS) $^ $(LIBS_TEST) $(LIBS_RUN) -o bin/$@ -std=c++11 #完成 demo_regexMatch: $(DEMO_DIR)/demo_regexMatch.cpp RegexMatch.cpp mkdir -p build $(CXX) -g $(C_FLAGS) $^ -lhpr -llogsystem $(LIBS_RUN) -o bin/$@ -std=c++11 #完成 LIBS_RECOGOFFLINE = -lxml -lmysqlclient -ljsoncpp -lhpr -llogsystem -lmysql -lmsc -lcurl# -ldataBaseInterface demo_iFlyRecogOffline: $(DEMO_DIR)/demo_iFlyRecogOffline.cpp IFlyBase.cpp RecogIFlyOffline.cpp ReadConfig.cpp# RegexMatch.cpp mkdir -p build $(CXX) -g -w $(C_FLAGS) $^ $(LIBS_RECOGOFFLINE) $(LIBS_RUN) -o bin/$@ -std=c++11 #完成 demo_tulingResponseOnline: $(DEMO_DIR)/demo_tulingResponseOnline.cpp ResponseTulingOnline.cpp ReadConfig.cpp mkdir -p build $(CXX) -g -w $(C_FLAGS) $^ -lcurl -ljsoncpp -ljson -lhpr -llogsystem $(LIBS_RUN) -o bin/$@ -std=c++11 #抛弃 demo_iFlyResponseOffline: $(DEMO_DIR)/demo_iFlyResponseOffline.cpp ResponseOffline.cpp ReadConfig.cpp RegexMatch.cpp IFlyBase.cpp mkdir -p build $(CXX) -g -w $(C_FLAGS) $^ -lcurl -ljson -ljsoncpp -lhpr -lmsc -lmysqlclient -lmysql -llogsystem -ldataBaseInterface $(LIBS_RUN) -o bin/$@ -std=c++11 #完成 demo_ResponseOffline1:$(DEMO_DIR)/demo_ResponseOffline1.cpp ResponseOffline1.cpp ReadConfig.cpp ../tool/database/DataBase.cpp mkdir -p build $(CXX) -g -w $(C_FLAGS) $^ -lhpr -ljsoncpp -ljson -lmysqlclient -llogsystem $(LIBS_RUN) -o bin/$@ -std=c++11 #完成 demo_capture_sound: $(DEMO_DIR)/demo_capture_sound.cpp Audio.cpp SpeechSample.cpp $(SPEECHNS) mkdir -p build $(CXX) -g $(C_FLAGS) $^ -lvad -lasound -lpthread -o bin/$@ -std=c++11 $(LIBS_RUN) #完成 demo_play_sound: $(DEMO_DIR)/demo_play_sound.cpp Audio.cpp mkdir -p bin $(CXX) -g $(C_FLAGS) $^ -lasound -lpthread -o bin/$@ -std=c++11 $(LIBS_RUN) # demo_udp_client: $(DEMO_DIR)/demo_udpClient.cpp UdpClient.cpp mkdir -p bin $(CXX) -g $(C_FLAGS) $^ -lhpr -o bin/$@ -std=c++11 $(LIBS_RUN) # demo_http_server: $(DEMO_DIR)/demo_httpServer.cpp HttpServer.cpp Chassis.cpp mkdir -p bin $(CXX) -g $(C_FLAGS) $^ -lnav -ljsoncpp -lpthread -o bin/$@ -std=c++11 $(LIBS_RUN) demo_chassis: $(DEMO_DIR)/demo_chassis.cpp Chassis.cpp mkdir -p bin $(CXX) -g $(C_FLAGS) $^ -lnav -lpthread -o bin/$@ -std=c++11 $(LIBS_RUN) # test: test.cpp ../tool/database/DataBase.cpp ReadConfig.cpp # mkdir -p build # $(CXX) -g -w $(C_FLAGS) $^ -lcurl -ljsoncpp -ljson -lhpr -lmysqlclient -lmysql -llogsystem $(LIBS_RUN) -o bin/$@ -std=c++11 # test2: test2.cpp #../tool/database/DataBase.cpp # $(CXX) -g $(C_FLAGS) $^ -ljsoncpp -ldataBaseInterface -lhpr -lmysqlclient $(LIBS_RUN) -o test2 -std=c++11 .PYHON: all clean #test demo_iFlyBase demo_iFlyResponseOnline demo_text2wave demo_iFlyRecogOnline demo_regexMatch demo_iFlyRecogOffline demo_tulingResponseOnline clean: rm -rf build/*.o ../lib/libvoiceInterface.so