I. usage of make in linux objective: to master the usage of make and to program on Linux. Environment: Linux system preparation: prepare three files: file1.c, file2.c, and file2.hfile1. c: # include & lt; stdio. h & gt; # include & quot; file2.h & quot; in... i. usage of make in linux
Purpose:
I have mastered the usage of make and can program on Linux.
Environment:
Linux
Preparation:
Prepare three files: file1.c, file2.c, and file2.h.
File1.c:
# Include
# Include "file2.h"
Int main ()
{
Printf ("print file1 $ \ n ");
File2Print ();
Return 0;
}
File2.h:
# Ifndef FILE2_H _
# Define FILE2_H _
# Ifdef _ cplusplus
Extern "C "{
# Endif
Void File2Print ();
# Ifdef _ cplusplus
}
# Endif
# Endif
File2.c:
# Include "file2.h"
Void File2Print ()
{
Printf ("Print file2 *********************** \ n ");
}
Basics:
Here is an example:
There is such a Makefile. (The file and Makefile are in the same directory)
=== Makefile start ===
Helloworld: file1.o file2.o
Gcc file1.o file2.o-o helloworld
File1.o: file1.c file2.h
Gcc-c file1.c-o file1.o
File2.o: file2.c file2.h
Gcc-c file2.c-o file2.o
Clean:
Rm-rf *. o helloworld
=== Makefile ended ===
A makefile mainly contains a series of rules as follows:
A: B
(Tab)
(Tab)
Each command line must have a tab.
The makefile above aims to compile an executable helloworld file. Let's explain in one sentence:
Helloworld: file1.o file2.o: helloworld depends on file1.o file2.o.
Gcc File1.o File2.o-o helloworld: compile the helloworld executable file. -O indicates the target file name you specified.
File1.o: file1.c: file1.o depends on the file1.c file.
Gcc-c file1.c-o file1.o: compile the file1.o file. -C indicates that gcc only compiles the file for it into the target file. It is named by the source file name, but its suffix is changed from ". c "or". cc "to". o ". -O file1.o can be omitted in this sentence. by default, the compiler generates the file1.o file, which is the role of-c.
File2.o: file2.c file2.h
Gcc-c file2.c-o file2.o
These two sentences are the same as the previous two sentences.
Clean:
Rm-rf *. o helloworld
When you type the make clean command, the *. o and helloworld files are deleted.
To compile the cpp file, you only need to change gcc to g ++.
Write the Makefile file, and directly type the make command in the command line to execute the content in the Makefile.
In this step, I think you can compile a Helloworld program.
Previous floor: use variables
As mentioned above, if you want to compile the cpp file, you just need to change gcc to g ++. However, if there are many gcc in Makefile, it will not be very troublesome.
Here we apply the variable. To set a variable, you only need to write the name of the variable at the beginning of a line, followed by a = sign, followed by the value of the variable you want to set. You will reference this variable in the future and write a $ symbol, followed by the variable name enclosed in brackets.
CFLAGS =-Wall-O-g. This is to configure the compiler settings and assign it to the CFFLAGS variable.
-Wall: outputs all warning information.
-O: Optimized during compilation.
-G: indicates compiling the debug version.
In this way, the Makefile file is relatively simple, but it is easy to find the disadvantage, that is, to list all the c files. If you add a c file, you need to modify the Makefile file, which is still troublesome in project development.
Next Floor: use functions
After learning this, you may say, is it like programming? There are variables and functions. In fact, this is programming, but it only uses different languages.
Example 3:
=== Makefile start ===
CC = gcc
XX = g ++
CFLAGS =-Wall-O-g
TARGET =./helloworld
%. O: %. c
$ (CC) $ (CFLAGS)-c $ <-o $ @
%. O: %. cpp
$ (XX) $ (CFLAGS)-c $ <-o $ @
SOURCES = $ (wildcard *. c *. cpp)
OBJS =$ (patsubst %. c, %. o, $ (patsubst %. cpp, %. o, $ (SOURCES )))
$ (TARGET): $ (OBJS)
$ (XX) $ (OBJS)-o $ (TARGET)
Chmod a + x $ (TARGET)
Clean:
Rm-rf *. o helloworld
=== Makefile ended ===
Function 1: wildcard
Generates a list of all objects ending with '. C.
SOURCES = $ (wildcard *. c *. cpp) indicates to generate a list of all files ending with. c and. cpp and store them in the SOURCES variable.
Function 2: patsubst
Match and replace. There are three parameters. The first is a pattern to be matched, the second represents what to replace it with, and the third is a list to be processed separated by spaces.
OBJS = $ (patsubst %. c, %. o, $ (patsubst %. cc, %. o, $ (SOURCES) indicates all files in the list. c ,. cpp character. o to form a new file list and store it in the OBJS variable.
%. O: %. c
$ (CC) $ (CFLAGS)-c $ <-o $ @
%. O: %. cpp
$ (XX) $ (CFLAGS)-c $ <-o $ @
These commands compile all. c and. cpp into a. o file.
There are three useful internal variables. $ @ Extended to the target file name of the current rule, $ <extended to the first dependent file in the dependency list, $ ^ is extended to the entire dependent list (removing all duplicated file names ).
Chmod a + x $ (TARGET) indicates that helloworld is forced into an executable file.
Now, I think you can write a simple and general Makefile file. all the examples above assume that all files are in the same directory, excluding subdirectories.
II. make usage
I have searched for a lot of materials and I am quite familiar with makefile writing, but I have no idea how to use makefile. My brother, the program I wrote does not have any problem when gcc runs in a single step. it passes through the process smoothly. it means that when it is put in makefile, it keeps reporting errors and makes me like a fireman. So I made up my mind to make it a super simple one. I will turn it off first. of course, this is for people like me who do not have abstract thinking. if the experts come here, they can despise it by the way.
First, write a func. h file. ######################################## ########## // Func. hvoid func (); ######################################## ######### again, write a func. c file. ######################################## ########## // Func. c # define your ID func () {printf ("hello world! \ N ");} ######################################## ######### then, compile the main file test. c. ######################################## ########## Include # Upload ID main () {func ();} ######################################## ######### after writing the program, in linux, execute the following statement in one step: gcc-c func. c-o func. o gcc-c test. c-o test. o gcc func. o test. o-o test is successfully compiled. Then execute./test to output hello world! However, according to the write statement above, the makefile file is compiled: **************************************** * ************************* // makefiletest: test. o func. o gcc test. o func. o-o testtest. o: test. c gcc-c test. c-o test. ofunc. o: func. c gcc-c func. c-o func. o *************************************** after **************************** make, the output is 'test' is up to date, indicating that there is an error or test does not exist. Otherwise, it is overwritten.
(Actually not, it only indicates that the test file is already in the latest version and does not need to be re-compiled. if it is forced to compile, you can touch test. c, Xiangbala)
Then, compile the makefile as follows: **************************************** * ************************* // makefileall: testbuild echo "build OK! "Testbuild: test. o func. o gcc test. o func. o-o testtest. o: test. c gcc-c test. c-o test. ofunc. o: func. c gcc-c func. c-o func. o *************************************** after **************************** make, run successfully. the output result is: **************************************** * ************************* gcc-c test. c-o test. ogcc-c func. c-o func. ogcc test. o func. o-o testecho "build OK! "Build OK! **************************************** ***************************. /Test. the output result is hello world! Makefile is simplified: **************************************** * ************************* // makefileall: test. o func. o gcc test. o func. o-o testtest. o: test. c gcc-c test. c-o test. ofunc. o: func. c gcc-c func. c-o func. o *************************************** * ****************************. Therefore, an hour is wasted to verify that all instead of test, ^ _ ^.
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