Here are five steps to compile the Linux makefile:
compile total of five parts
1. Setting up the compilation environment set compile environment
2. Get the source file to compile and convert the source file to the destination file (source and destination files) source to obj
3. Compile the file as the target file objing
4. Link the target file to an executable file or (packaged as) a dynamic library, or a static library linking
5. Other aspects such as clean
The following is a simple makefile template (which can be extended in the actual project according to this template):
take a look at this template:
# # Set up the compilation environment
cc:=gcc
cxx:=g++
bin:= libdemo.so
FLAGS: =-wall-g-fpic
Cxx_flags: =-wall-g-pipe-fpic-shared-export-dynamic
LIB: =-L.. /common/test/-ltest
Include_path: =-i/common/-I.. /common/test/
# # Get the source file to compile and convert the source file to the destination file
SOURCES: = $ (wildcard *.c *.cpp)
OBJS: = $ (Patsubst%.c,%.O, $ (patsubst%.cpp,%.o,$ (SOURCES)))
# # Compile the source file as the destination file
%.O:%.c
$ (CC) $ (FLAGS)-C $<-o $@ $ (include_path)
%.o:%.cpp
$ (CXX) $ (FLAGS)-C $<-o $@ $ (include_path)
# # Link The target file to an executable file or dynamic library, or a static library
$ (BIN): $ (OBJS)
$ (CXX) $ (cxx_flags) $ (OBJS) $ (include_path) $ (LIB)-O $ (BIN)
chmod a+x $ (BIN)
# # Other
Clean
RM-RF *.O $ (BIN)
Install
CP $ (BIN). /common/bin
Linux Common makefile template writing
Under Linux, we have to write our own makefile,makefile is indeed profound, but in fact for day-to-day use, nothing but
1: Compile the executable program. 2: Compiling Lib Library 3: compiling so Library
This Bo for the above three kinds of purposes each wrote a makefile template, I hope to help you.
I. Compiling an executable program
The current directory to compile the file into an executable file (to connect to the external library only need to change the Inc and Lib)
CXX = g++
TARGET = Bitmaploctest
C_flags + +-g-wall
Lib_flags =-pthread
All: $ (TARGET)
BITMAPLOCTEST:BITMAPLOCTEST.O BITMAPLOC.O FILE_LOCK.O
$ (CXX)-O $@ $^ $ (lib_flags) $ (LIB) $ (c_flags)
. CPP.O:
$ (CXX)-c-o $*.O $ (INC) $ (c_flags) $*.cpp
. CC.O:
$ (CXX)-c-o $*.O $ (INC) $ (c_flags) $*.cc
Clean
-rm-f *.O $ (TARGET)
two. Compile into Lib library
The specified file is compiled into a Lib library under the current directory (the General Lib Library compiles without compiling the external libraries used, but when compiled into executable programs or. So)
Inc_dir=./
Src_dir=./
Obj_dir=./
Lib_dir=./
H_dir=./
obj_ext=. O
cxxsrc_ext=. cpp
Csrc_ext=. C
Lib_ext=. A
h_ext=. h
OBJECTS = $ (obj_dir) bitmaploc$ (obj_ext) \
$ (Obj_dir) file_lock$ (Obj_ext)
Lib_target = $ (lib_dir) libbitmaploc$ (Lib_ext)
$ (Obj_dir)%$ (obj_ext): $ (Src_dir)%$ (Cxxsrc_ext)
@echo
@echo "Compiling $< ==> $@ ..."
$ (CXX) $ (INC) $ (c_flags)-C $<-O $@
$ (Obj_dir)%$ (obj_ext): $ (Src_dir)%$ (Csrc_ext)
@echo
@echo "Compiling $< ==> $@ ..."
$ (CC)-i./$ (INC) $ (c_flags)-C $<-O $@
all:$ (Lib_target)
$ (Lib_target): $ (OBJECTS)
All: $ (OBJECTS)
@echo
$ (AR) RC $ (lib_target) $ (OBJECTS)
@echo "OK"
Clean
Rm-f $ (lib_target) $ (OBJECTS)
three. Compile into so library
The specified file is compiled into so library under the current directory (all referenced external libraries must be compiled in)
CC = gcc
CXX = g++
Cflags =-wall-pipe-ddebug-d_new_lic-g-d_gnu_source-shared-d_reentrant
LIB =-lconfig-ldl-lrt-l. /.. /lib-lttc-g
INCLUDE =-I.. /spp_inc
OO = service.o tinystr.o tinyxml.o tinyxmlerror.o tinyxmlparser.o uin_conf.o STAT.O
TARGETS =.. /.. /lib/libranch.so
All: $ (TARGETS)
Stat:tool_stat.cpp
$ (CXX) $ (INCLUDE) tool_stat.cpp-o tool_stat stat.o tinystr.o TINYXML.O tinyxmlerror.o
CP Tool_stat. /.. /bin
$ (TARGETS): $ (OO)
$ (CXX) $ (cflags) $ (INCLUDE) $ (OO)-O $@ $ (libdir) $ (LIB)
. C.O:
$ (CC) $ (cflags)-C $ (INCLUDE) $<
Echo $@
. CPP.O:
$ (CXX) $ (cflags)-C $ (INCLUDE) $<
Echo $@
%:%.c
$ (CC) $ (cflags)-O $@ $< $ (OO) $ (ldflags)
Echo $@
Clean
Rm-f *.O
Rm-f $ (TARGETS)
Rm-f Tool_stat
OK, my usual makefile also in these three kinds of formats, hope to be useful to everybody.