项目中需要编译好几个文件,用makefile管理比较方便。
g++命令:
g++ -I ../../include/ -o init.o init.cpp $(mysql_config --cflags) ../Sniffer/sqlToCache.cpp $(mysql_config --libs) ../Decoder/dataCache.cpp ../Preprocessor/process.cpp ../DetectionEngine/detect_acvl.cpp -lpthread
改为makefile后:
CFLAGS = -I ../../include/
HEADPATH =../../include/
MYSQL=-L /usr/include/mysql
helloworld: init.o sqlToCache.o dataCache.o process.o detect_acvl.o
g++ -o helloworld init.o sqlToCache.o dataCache.o process.o detect_acvl.o -lpthread -L/usr/lib/mysql -lmysqlclient -lz
init.o: init.cpp $(HEADPATH)sniffer_sqlToCache.h $(HEADPATH)decoder_dataCache.h $(HEADPATH)preprocessor_process.h $(HEADPATH)detect_acvl.h
g++ -c $(CFLAGS) init.cpp -lpthread
sqlToCache.o: ../Sniffer/sqlToCache.cpp $(HEADPATH)decoder_dataCache.h
g++ -c $(MYSQL) -lmysqlclient -lz $(CFLAGS) $(mysql_config --cflags) ../Sniffer/sqlToCache.cpp $(mysql_config --libs) -lpthread
dataCache.o: ../Decoder/dataCache.cpp $(HEADPATH)decoder_dataCache.h
g++ -c $(CFLAGS) ../Decoder/dataCache.cpp
process.o: ../Preprocessor/process.cpp $(HEADPATH)preprocessor_process.h $(HEADPATH)detect_acvl.h
g++ -c $(CFLAGS) ../Preprocessor/process.cpp
detect_acvl.o: ../DetectionEngine/detect_acvl.cpp $(HEADPATH)detect_acvl.h
g++ -c $(MYSQL) -lmysqlclient -lz $(CFLAGS) $(mysql_config --cflags) ../DetectionEngine/detect_acvl.cpp $(mysql_config --libs)
clean :
rm *.o
注意路径,特别是一些类库(mysql.h)的路径。