Linux Makefile (Chinese version 1)

Source: Internet
Author: User

#############################################################################
# Generic Makefile for C + + program
Usage
# ------
# 1. Copy makefile to your code directory
# 2. "Customizable section" customization only when necessary
# * In order to use nonstandard libraries, set up pre-compiler and compiler <my_cflags>, linker <MY_LIBS>
# * In order to search for more directory source code, settings <SRCDIRS>
# * Specify your favorite program name, Settings <PROGRAM>
# 3. Hit make to start compiling your program
#
# Make Target:
# ------------
# The Makefile provides the following targets to make:
# $ make compile and link
# $ make Nodep=yes compile and link without generating dependencies
# $ make Objs compile only (no linking)
# $ make tags Create tags for Emacs editor
# $ make ctags create ctags for VI editor
# $ make clean objects and the executable file
# $ make Distclean clean objects, the executable and dependencies
# $ make help get the usage of the makefile
#
#===========================================================================

# # Customizable Section:adapt those variables to suit your program.
##==========================================================================

# pre-compiler and compiler options
# my_cflags =-ggdb3-pipe-o2-wall-wextra-fopenmp-march=native-mfpmath=sse-dlinux-m64-std=c++0x
My_cflags =-g-dlinux-itest1/include-itest2/include-itest1/include/test1-itest2/include/test2

# Linker options, seemingly written Libs, such as Libs =-lmysqlclient-liconv, specify the library to link to
# my_libs =-lglew-lglut-lglu-lgl-lx11-lxmu-lxi-lm-l/usr/x11r6/lib-lgomp-lopenthreads-lpthread
My_libs =-lm

# CPP uses a pre-compiler, such as cppflags= '-i/usr/local/libjpeg/include-i/usr/local/libpng/include ' (man cpp for more).
Cppflags =

# Linker options and linker ld How to use, such as Ldflags =-l/var/xxx/lib-l/opt/mysql/lib-wl,r/var/xxx/lib-wl,r/opt/mysql/lib
Ldflags =

# Source Files Directory
# If not specified, only the current directory would be serached.
Srcdirs =


# executable file name
# If not specified, the current directory name or ' a.out ' would be used.
program =

# # Implicit section: Modify if necessary
##==========================================================================

# source file type (headers excluded).
#. C is a C language file and the others are C + + files.
Srcexts =. C. C. cc. cpp. CPP. C + +. Cxx. CP

# header File Type
Hdrexts =. H. H. HH. hpp. HPP h++. hxx. HP

# pre-compiler and compiler options
# users can override these variables in the command line U
CFLAGS =
# cxxflags=-std=c++0x
cxxflags=
# C Program Compiler
CC = gcc

# C + + program compiler
CXX = g++

# Uncomment the following line, C program will be compiled as C + +
#CC = $ (CXX)

# Delete File command
RM = Rm-f

Etags = Etags
Etagsflags =

CTAGS = CTAGS
Ctagsflags =

# # Stable section: Generally no change is required. But you can add more
##===================================================================== =====
SHELL =/bin/sh
EMPTY =
SPACE = $ (empty) $ (empty)
Ifeq ($ (program),
Cur_path_names = $ (subst/,$ (S PACE), $ (subst $ (SPACE), _,$ (CURDIR)))
Program = $ (Word $ (words $ (cur_path_names)), $ (cur_path_names))
Ifeq ($ ( Program),
program = a.out
endif
endif
ifeq ($ (srcdirs),)
Srcdirs =.
endif
SOURCES = $ (foreach d,$ (srcdirs), $ (wildcard $ (Addprefix $ (d)/*,$ (srcexts)))
HEADERS = $ (foreach d,$ ( Srcdirs), $ (wildcard $ (Addprefix $ (d)/*,$ (hdrexts))))
Src_cxx = $ (filter-out%.c,$ (SOURCES))
Objs = $ (addsuffix. O, $ (basename $ (SOURCES)))
DEPS = $ (OBJS:.O=.D)

# # define some useful variables
dep_opt = $ (Shell if ' $ (CC)--version | grep "GCC" >/dev/null "; then \
echo "-MM-MP"; else echo "-M"; FI)
DEPEND = $ (CC) $ (dep_opt) $ (my_cflags) $ (CFLAGS) $ (cppflags)
DEPEND.D = $ (Subst-g,, $ (DEPEND))
compile.c = $ (CC) $ (my_cflags) $ (CFLAGS) $ (cppflags)-C
Compile.cxx = $ (cxx) $ (my_cflags) $ (cxxflags) $ (cppflags)-C
LINK.C = $ (CC) $ (my_cflags) $ (CFLAGS) $ (cppflags) $ (ldflags)
Link.cxx = $ (cxx) $ (my_cflags) $ (cxxflags) $ (cppflags) $ (ldflags)

. Phony:all objs Tags ctags clean distclean help Show

# Remove some of the default suffixes
. Suffixes:

All: $ (program)

# Create a rule with dependent files (. d).
#------------------------------------------

%.d:%.c
@echo-N $ (dir {1}lt;) > [Email protected]
@$ (DEPEND.D) {1}lt; >> [email protected]

%.d:%. C
@echo-N $ (dir {1}lt;) > [Email protected]
@$ (DEPEND.D) {1}lt; >> [email protected]

%.d:%.cc
@echo-N $ (dir {1}lt;) > [Email protected]
@$ (DEPEND.D) {1}lt; >> [email protected]

%.d:%.cpp
@echo-N $ (dir {1}lt;) > [Email protected]
@$ (DEPEND.D) {1}lt; >> [email protected]

%.d:%. Cpp
@echo-N $ (dir {1}lt;) > [Email protected]
@$ (DEPEND.D) {1}lt; >> [email protected]

%.d:%.c++
@echo-N $ (dir {1}lt;) > [Email protected]
@$ (DEPEND.D) {1}lt; >> [email protected]

%.d:%.cp
@echo-N $ (dir {1}lt;) > [Email protected]
@$ (DEPEND.D) {1}lt; >> [email protected]

%.d:%.cxx
@echo-N $ (dir {1}lt;) > [Email protected]
@$ (DEPEND.D) {1}lt; >> [email protected]

# The rule (. O) that generated the target file.
#----------------------------------------
objs:$ (OBJS)

%.o:%.c
$ (compile.c) {1}lt; -O [email protected]

%.o:%. C
$ (compile.cxx) {1}lt; -O [email protected]

%.o:%.cc
$ (compile.cxx) {1}lt; -O [email protected]

%.o:%.cpp
$ (compile.cxx) {1}lt; -O [email protected]

%.o:%. Cpp
$ (compile.cxx) {1}lt; -O [email protected]

%.o:%.c++
$ (compile.cxx) {1}lt; -O [email protected]

%.o:%.cp
$ (compile.cxx) {1}lt; -O [email protected]

%.o:%.cxx
$ (compile.cxx) {1}lt; -O [email protected]

# Create rules for tags.
#-------------------------------------
Tags: $ (HEADERS) $ (SOURCES)
$ (etags) $ (etagsflags) $ (HEADERS) $ (SOURCES)

Ctags: $ (HEADERS) $ (SOURCES)
$ (CTAGS) $ (ctagsflags) $ (HEADERS) $ (SOURCES)

# Generate rules for executable.
#-------------------------------------
$ (Program): $ (OBJS)
Ifeq ($ (Src_cxx),) # C Program
$ (LINK.C) $ (OBJS) $ (my_libs)-o [email protected]
@echo Type./[email protected] to execute the program.
else # C + + program
$ (link.cxx) $ (OBJS) $ (my_libs)-o [email protected]
@echo Type./[email protected] to execute the program.
endif

# TANG MODIFY START
#ifndef NODEP
Ifdef NODEP
# TANG MODIFY END
Ifneq ($ (DEPS),)
Sinclude $ (DEPS)
endif
endif

Clean
$ (RM) $ (OBJS) $ (program) $ (program). exe

Distclean:clean
$ (RM) $ (DEPS) TAGS
#############################################################################

##############################################################################
# Display Help
Assist:
@echo ' Generic Makefile for C + + Programs (gcmakefile) version 0.5 '
@echo ' Copyright (C) + Whyglinux <[email&nbs P;protected]> '
@echo
@echo ' usage:make [TARGET] '
@echo ' TARGETS: '
@echo ' All (=make) compile and link .‘
@echo ' nodep=yes make without generating dependencies. '
@echo ' Objs compile only (no linking). '
@echo ' tags create tags for Emacs editor. '
@echo ' ctags create ctags for VI editor. '
@echo ' Clean clean objects and the executable file. '
@echo ' Distclean clean objects, the executable and dependencies. '
@echo ' Show show variables (for debug with only). '
@echo ' help print this message. '
@echo
@echo ' report bugs to <whyglinux at Gmail DOT com>. '

# Display variables (for debugging purposes only)
Show
@echo ' program: ' $ (program)
@echo ' Srcdirs: ' $ (srcdirs)
@echo ' HEADERS: ' $ (HEADERS)
@echo ' SOURCES: ' $ (SOURCES)
@echo ' Src_cxx: ' $ (src_cxx)
@echo ' OBJS: ' $ (OBJS)
@echo ' DEPS: ' $ (DEPS)
@echo ' DEPEND: ' $ (DEPEND)
@echo ' compile.c: ' $ (compile.c)
@echo ' Compile.cxx: ' $ (compile.cxx)
@echo ' LINK.C: ' $ (LINK.C)
@echo ' Link.cxx: ' $ (link.cxx)
#############################################################################

Linux Makefile (Chinese version 1)

Related Article

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.