Linux Common makefile Template writing

Source: Internet
Author: User

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.

All rights reserved, reprint please indicate the source. http://www.vimer.cn

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.