1. Generate the makefile of the executable file
###################################### # # ##################################### # source file #Source file, automatically find all .c and .cpp files and define the target as a .o file of the same name SOURCE := $(wildcard *.c) $(wildcard * .cpp) OBJS: = $ (patsubst% .c,% .o, $ (patsubst% .cpp,% .o, $ (SOURCE))) # target you can change test to what you want #target file name, enter any executable file name you want TARGET := test # compile and lib parameter #Compile parameter CC := gcc LIBS := LDFLAGS := DEFINES := INCLUDE := -I. CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE) CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H # i think you should do anything here #The following basically does not need to make any changes.PHONY : everything objs clean veryclean rebuild everything : $(TARGET) all : $(TARGET) objs: $ (OBJS) rebuild: veryclean everything clean : rm -fr *.so rm -fr * .o veryclean : clean rm -fr $(TARGET) $(TARGET) : $(OBJS) $(CC) $(CXXFLAGS) -o $@ $(OBJS) $(LDFLAGS) $(LIBS)
2. Generate the makefile of the static link library
###################################### # # ###################################### #target you can change test to what you want #共享库文件名,lib*.a TARGET := libtest.a #compile and lib parameter #编译参数 CC := gcc AR = ar RANLIB = ranlib LIBS := LDFLAGS := DEFINES := INCLUDE := -I. CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE) CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H #i think you should do anything here #下面的基本上不需要做任何改动了 #source file #源文件,自动找所有.c和.cpp文件,并将目标定义为同名.o文件 SOURCE := $(wildcard *.c) $(wildcard *.cpp) OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE))) .PHONY : everything objs clean veryclean rebuild everything : $(TARGET) all : $(TARGET) objs : $(OBJS) rebuild: veryclean everything clean : rm -fr *.o veryclean : clean rm -fr $(TARGET) $(TARGET) : $(OBJS) $(AR) cru $(TARGET) $(OBJS) $(RANLIB) $(TARGET)
3、生成动态链接库的makefile
###################################### # # ###################################### #target you can change test to what you want #共享库文件名,lib*.so TARGET := libtest.so #compile and lib parameter #编译参数 CC := gcc LIBS := LDFLAGS := DEFINES := INCLUDE := -I. CFLAGS := -g -Wall -O3 $(DEFINES) $(INCLUDE) CXXFLAGS:= $(CFLAGS) -DHAVE_CONFIG_H SHARE := -fPIC -shared -o #i think you should do anything here #下面的基本上不需要做任何改动了 #source file #源文件,自动找所有.c和.cpp文件,并将目标定义为同名.o文件 SOURCE := $(wildcard *.c) $(wildcard *.cpp) OBJS := $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCE))) .PHONY : everything objs clean veryclean rebuild everything : $(TARGET) all : $(TARGET) objs : $(OBJS) rebuild: veryclean everything clean : rm -fr *.o veryclean : clean rm -fr $(TARGET) $(TARGET) : $(OBJS) $(CC) $(CXXFLAGS) $(SHARE) $@ $(OBJS) $(LDFLAGS) $(LIBS)
makefile 的选项解释:
CFLAGS 表示用于 C 编译器的选项,
CXXFLAGS 表示用于 C++ 编译器的选项。
CFLAGS: 指定头文件(.h文件)的路径,如:CFLAGS=-I/usr/include -I/path/include。同样地,安装一个包时会在安装路径下建立一个include目录,当安装过程中出现问题时,试着把以前安装的包的include目录加入到该变量中来。
LDFLAGS: Some optimization parameters used by compilers such as gcc, and the location of library files can also be specified in it. Usage: LDFLAGS=-L/usr/lib -L/path/to/your/lib. Every installation of a package will almost certainly create a lib directory in the installation directory. If a package is clearly installed, and another package is installed, it says that it cannot be found, you can try it in LDFALGS added to the lib path of that package.
LIBS: tell the linker which library files to link, like LIBS=-lpthread -liconv
Simply put, LDFLAGS tells the linker where to look for library files, and LIBS tells the linker which library files to link. However, these two parameters will be added at the link stage when used, so even if you exchange the values of these two, there is no problem.
Sometimes LDFLAGS specifies -L, although the linker can find the library for linking, but the runtime linker cannot find the library. If the path of the software runtime library file is to be expanded, then we need to add these two libraries. Give "-Wl,R":
LDFLAGS = -L/var/xxx/lib -L/opt/mysql/lib -Wl,R/var/xxx/lib -Wl,R/opt/mysql/lib
If you set the environment variable export LDFLAGS="-L/var/xxx/lib -L/opt/mysql/lib -Wl,R/var/xxx/lib -Wl,R/opt/mysql/lib before executing ./configure " , note that there can be no spaces on both sides of the equal sign when setting the environment variable, and quotation marks must be added (the usage of the shell). Then after executing configure, the Makefile will set this option, and this parameter will be present when linking, and the library file search path of the compiled executable program will be expanded.