Learning makefile at the beginning is a painful process. From chaotic rules to a simple makefile and then to a systematic makefile is not a one-stop process. Today
I will write a simple, standardized (relatively speaking) makefile with complete functions. Makefile for beginners. Hope to be useful to new Linux students;
1. source file preparation
Suppose we have the source file main. c com1.c com2.c com3.c com4.c in the project folder (pname ).
And their header files; there is a sub-directory mylib in the project folder; there is a display. C and its header files in pname/mylib.
Ii. Analysis
In Main. C, there is our main function, which calls a function of every other source file (let them concatenate ). We compile all the source files in mylib into a library.
(Libyuer. A), and finally connect to our project;
3. makefile example
# First, we define ourCommand variable
Cc = gcc
AR = ar
Rm = RM
CP = CP
# Then set ourParameter variables
Primary des =-I./mylib
Cflags + =-O2 $ (DES)
Lkflags + =-L./mylib
# Define what we needLocal variable
Libs =-lyuer
Objs = Main. O com1.o com2.o com3.o com4.o
Target = MyApp
Installdir =/user/bin/pname
# Use the various variables we have preparedDefine rules
ALL: $ (target)
$ (Target): $ (objs) $ (libs)
$ (CC) $ (lkflags) $ (objs) $ (libs)-o $ @
Mylib/libmylib. ::
$ (Make)-C mylib
%. O: %. c
$ (CC)-C $ (cflags) $ <-o $ @
# Installer
To the specified directory
Install:
$ (CP) $ (target) $ (installdir)
# Do not forget to clear intermediate files, target files, and subdirectory files
Oh
Clean: $ (subdir_clean)
-$ (RM) *. o $ (target)
$ (Subdir_clean ):
$ (RM)-C mylib clean
Iv. Description
This is my personal summary of some of the methods, not necessarily the best, but I still hope to help you, especially new users. At the beginning, do not ignore the norms. There are many other parameter variables that can be set (here only
Is an example), the compilation command must start with <tab>. Here I use spaces, so pay attention to copying and testing.
5. subdirectory mailefile
The subdirectory makefile is used to generate the specified library from the source files in the subdirectory to be used by upper-layer projects. The subdirectory makefile also follows the preceding format.
# First, we define ourCommand variable
Cc = gcc
AR = ar
Rm = RM
# Then set ourParameter variables
Primary des =-I ../
Cflags + =-O2 $ (DES)
# Define what we needLocal variable
Objs = display. o
Target = MyApp
# Use the various variables we have preparedDefine rules
ALL: $ (target)
$ (Target): $ (objs)
$ (AR)-RV $ @ $ (objs)
%. O: %. c
$ (CC)-C $ (cflags) $ <-o $ @
# Do not forget to clear intermediate files, target files, and subdirectory files
Clean:
-$ (RM) *. o $ (target)
In fact, CC, cflags, and libs in makefile are of the same nature, that is, variables. I just called them differently.
They are called command variables, parameter variables, and local variables.
Here is just a simple makefile. If you want to create a large project with hundreds of source files, it is not that simple. The best way is to divide make into several
Function. Mak and other sub-makefile files for ease of management, more use of the built-in functions in makefile.
I have not detected this makefile. I will check it when I am empty.