Reference to the online data written by a simple can compile C + + mixed engineering Makefile "online most of the general makefile very long and no explanation, so write this document"
Hello.h
/* File name:hello.h
* C header file */
#ifndef hello_h
#define HELLO_H
#ifdef _ _cplusplus
extern "C" {
#endif
void Print_hello ();
#ifdef __cplusplus
}
#endif
#endif
Main.cxx
/* File naem:main.cxx
* C + + source file *
/#include "hello.h"
int main ()
{
Print_hello ();
return 0;
}
Makefile
############################################################################# # # Generic Makefile for C + + program #
# author:yanbin Lee # date:2017/07/05 #============================================================================
# The C + + program compiler. CC=GCC cxx=g++ # compiler parameter settings at compile time, include header file path settings cflags:=-wall-o2-g cflags+=-i $ (shell pwd)/include cxxflags:=-wall-o2-g CXXFL
Ags+=-i $ (shell pwd)/include # library file add ldflags:= ldflags+= # Specifies where the source program is stored srcdirs:=. The Srcdirs+=dir # Setup program uses the file type srcexts:=.c. CXX # Set Run program name Program:=hello #t1 =$ (Addprefix $ (srcdirs)/*,$ (srcexts)) #t2 =$ (for Each d,$ (srcdirs), echo $ (d)) #t3 =$ (foreach d,$ (srcdirs), $ (wildcard $ (Addprefix $ (d)/*,$ (srcexts))) #all: # @echo $ (T3) S ources=$ (foreach d,$ (srcdirs), $ (wildcard $ (Addprefix $ (d)/*,$ (srcexts))) objs=$ (foreach x,$ (srcexts), $ (Patsubst%$ ( x),%.o,$ (filter%$ (x), $ (SOURCES))). Phony:all Clean Distclean Install%.O:%.C $ (CC)-C $ (cflags)-O $@ $<%.o:%.cxx $ (CXX)-C $ (cxxflags)-O $@ $& Lt
$ (Program): $ (OBJS) ifeq ($ (Strip $ (srcexts)),. c) $ (CC)-O $ (program) $ (OBJS) $ (ldflags) Else $ (CXX)-O $ (program) $ ( OBJS) $ (ldflags) endif install:install-m 755-d-P $ (program)./bin clean:rm-f $ (Shell find-name "*.O") rm-f $ (program) Distclean:rm-f $ (Shell find-name "*.O") Rm-f $ (Shell find-name "*.D") rm-f $ [Program] All: @echo $ (
OBJS)
This sample engineering code can be downloaded here: Click on the Open link
Get root permission, enter make to get the executable file Hello, and then run:
1. Partial makefile function
This section describes some of the functions that are used, and a complete description of the function can refer to:
"Makefile Common function Summary" http://blog.csdn.net/ustc_dylan/article/details/6963248
filter: Filtering functions
Sources: = foo.c bar.c baz.s ugh.h
$ (Filter%.c%.s,$ (sources))
Number return value is "foo.c bar.c baz.s"
patsubst: pattern substitution function
$ (patsubst%.c,%.o,x.c.c bar.c)
#把字串 "X.C.C bar.c" to replace the words ending with. C with characters ending with. O. The return result of the function is "X.C.O bar.o"
wildcard: pattern matching filename
In makefile, it is expanded to a list of all files that already exist, separated by spaces, that match this pattern
foreach: Loop function
$ (foreach Var,list,text)
When executed, the word in the "list" that is separated by a space is assigned to the variable "VAR" and then the "TEXT" expression is executed.
A.O B.O
string: Go to the space function, remove the null characters from the beginning and end of the <string> string
$ (Strip a B c)
The string "abc" goes to the opening and ending spaces, and the result is "ABC".
$ (findstring <find>,<in>)
addprefix : Add prefix function
$ (Addprefix src/,foo bar) return value is "Src/foosrc/bar"
2. Difficult explanation
It may be more difficult to understand in this makefile that "SOURCES = $ (foreach d,$ (srcdirs), $ (wildcard $ (Addprefix $ (d)/*,$ (srcexts)))" It is more difficult to understand purely from a grammatical understanding, can be gradually disassembled
Test 1
New File under Folder
root@ubuntu:/home/share/task002# ls
HELLO.C hello.h Makefile Mian.cxx
Makefile:
srcdirs:=.
Srcexts:=.c. cxx
t1=$ (Addprefix $ (srcdirs)/*,$ (srcexts))
All
@echo $ (T1)
Output:./hello.c./mian.cxx(first thought the output would be./*.c,./*cxx)
Test 2
Add a dir folder based on test 1
srcdirs:=.
Srcdirs+=dir
t2=$ (foreach d,$ (srcdirs), echo $ (d))
All
@echo $ (T2)
Output: Echo. echo dir "Note that the results are not obtained by all, but by a foreach rule."
Test 3
Add a A.C file based on test 2
srcdirs:=.
Srcdirs+=dir
Srcexts:=.c. cxx
t3=$ (foreach d,$ (srcdirs), $ (wildcard $ (Addprefix $ (d)/*,$ (srcexts)))
All
@echo $ (T3)
Output:./hello.c./mian.cxx dir/a.c ( simple to understand difficult, or test yourself more reasonable )
Test 4
similarly to objs=$ (foreach x,$ (srcexts), $ (Patsubst%$ (x),%.o,$ (filter%$ (x), $ (SOURCES))) is processed, and a new file is created in the dir directory a.o
Final output:./hello.o dir/a.o./MIAN.O
3. Matters needing attention
L Note the space when editing in Windows, pay attention to the TAB key, note the line break, these are pits
L time is limited, makefile files are not tested when adding dynamic libraries