Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the Makefile of C++

2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

Most people do not understand the knowledge points of this article "how to use C++ 's Makefile", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use C++ 's Makefile" article.

1. Basic syntax and execution of Makefile

Why use Makefile?

The Makefile file describes the rules for compiling and linking the whole project.

The advantage of writing Makefile for a project is the ability to "automate compilation" with an one-line command. Just provide one (usually multiple for a project) correct Makefile, and then you only need to type the "make" command into the terminal for each compilation, and the whole project will be compiled automatically, greatly improving the efficiency. Especially when compiling a large project where only a small portion of the files have been changed.

Most IDE development environments write Makefile automatically for users.

How does Make work?

The principles of Make's work are:

An object file needs to be recompiled if and only if the change timestamp of its dependent file (dependencies) is newer than the creation timestamp of the target file.

The Make tool traverses all dependent files and updates their corresponding target files. The relationships between compiled commands and these target files and their corresponding dependent files are all stored in Makefile.

Makefile also specifies information such as how to create and what kind of object files and executables should be created.

In addition, you can even store some commands from the system terminal you want to call in Makefile and use it like a Shell script.

Function:

The Makefile file tells Make how to compile and link into a program

You can install the make feature with the command dnf install make

Format:

Write Makefile in the following format

Goal (target): dependence (prerequiries).

Command (command)

Note: each command line must be preceded by a Tab character, that is, the first character on the command line is Tab

Experiment:

Vim Makefile edit the file:

Simpletest:simple.o simpletest.o gathers + simple.osimpletest.o-o simpletestsimple.o:simple.cpp gathers +-c simple.cpp-o simple.osimpletest.o:simpletest.cpp gathers +-c simpletest.cpp-osimpletest.o

The result is:

[root@foundation1 shishi] # make

Gmail +-c simple.cpp-o simple.o

Gmail +-c simpletest.cpp-o simpletest.o

Gmail + simple.o simpletest.o-o simpletest

Commands are ordered up and down, and the previous line is dependent on the next line.

If there is already a .o file in the folder, make will prompt you that it has been generated. You need to delete the .o file before make.

The role of clean: when specified, the corresponding .o file will be deleted to avoid the above situation

Simpletest:simple.o simpletest.o gathers + simple.osimpletest.o-o simpletestsimple.o:simple.cpp gathers +-c simple.cpp-o simple.osimpletest.o:simpletest.cpp gathers +-c simpletest.cpp-o simpletest.oclean: rm simpletestsimple.o simpletest.o

The result is:

[root@foundation1 shishi] # make clean

Rm simpletest simple.o simpletest.o

[root@foundation1 shishi] # make

Gmail +-c simple.cpp-o simple.o

Gmail +-c simpletest.cpp-o simpletest.o

Gmail + simple.o simpletest.o-o simpletest

2. Makefile simplification process

Use variables: if you call a file more often, you can use variables instead, and variables can be replaced directly

Variable definition: variable = string

Variable use: $(variable name)

The makefile file can be changed to:

TARGET = simpletestOBJS = simple.osimpletest.o $(TARGET): $(OBJS) gathers + $(OBJS)-o $(TARGET) simple.o:simple.cpp gathers +-c simple.cpp-o simple.osimpletest.o:simpletest.cpp gathers +-c simpletest.cpp-osimpletes t.oclean: rm $(TARGET) $(OBJS)

Command automatic derivation: we can find that since .cpp files generate corresponding .o files, makefile files can be recognized automatically.

The makefile file can be changed to:

TARGET = simpletestOBJS = simple.o simpletest.o$ (TARGET): $(OBJS) gathers + $(OBJS)-o $(TARGET) simple.o:simple.cppsimpletest.o:simpletest.cppclean: rm $(TARGET) $(OBJS)

Predefined variables: some variables are also defined in the system.

Variable program default value CCC language compiler ccCXXC++ compiler arCPP C language preprocessor with standard output $(CC)-ERM delete command rm

The makefile file can be changed to:

TARGET = simpletestOBJS = simple.o simpletest.o$ (TARGET): $(OBJS) $(CXX) $(OBJS)-o $(TARGET) simple.o:simple.cppsimpletest.o:simpletest.cppclean: $(RM) $(TARGET) $(OBJS)

Imaginary target: if there are clean files in the folder, then make clean cannot be used. You need to use the imaginary target. You can take effect without looking at the files in the folder when executing the command.

The makefile file can be changed to:

TARGET = simpletestOBJS = simple.o simpletest.o.PHONY: clean$ (TARGET): $(OBJS) $(CXX) $(OBJS)-o $(TARGET) simple.o:simple.cppsimpletest.o:simpletest.cppclean: $(RM) $(TARGET) $(OBJS)

It is recommended that commands that do not generate target files are set to hypothetical targets.

3. Makefile generates and uses libraries

3.1 Establishment and use of dynamic Library

Vim Makefile edit the file:

TARGET = simpletestOBJS = simple.oLIB = libsimple.soCXXFLAGS =-c-fPIC.PHONY: clean$ (TARGET): $(LIB) simpletest.o $(CXX) simpletest.o-o $(TARGET)-L. -lsimple$ (LIB): $(OBJS) $(CXX)-shared $(OBJS)-o $(LIB) simple.o:simple.cpp $(CXX) $(CXXFLAGS) simple.cpp-o $(OBJS) simpletest.o:simpletest.cpp $(CXX) $(CXXFLAGS) simpletest.cpp-o simpletest.oclean: $(RM) $(TARGET) $(OBJS) $(LIB)

The result is:

[root@foundation1 shishi] # make clean

Rm-f simpletest simple.o libsimple.so

[root@foundation1 shishi] # make

Gmail +-c-fPIC simple.cpp-o simple.o

Gmail +-shared simple.o-o libsimple.so

Gmail +-c-fPIC simpletest.cpp-o simpletest.o

Glover + simpletest.o-o simpletest-L. -lsimple

Predefined variables:

It still needs to be defined earlier.

Variable program parameters CFLAGS extra flags for C compilers CXXFLAGS extra flags for C++ compilers extra flags ARFLAGS for CumberCandler extra flags LDFLAGS link library path-LLDLIBS link library-l

The makefile file can be changed to:

TARGET = simpletestOBJS = simple.oLIB = libsimple.soCXXFLAGS =-c-fPICLDFLAGS =-L.LDLIBS =-lsimple.PHONY: clean$ (TARGET): $(LIB) simpletest.o $(CXX) simpletest.o-o $(TARGET) $(LDFLAGS) $(LDLIBS) $(LIB): $(OBJS) $(CXX)-shared $(OBJS)-o $(LIB) simple.o:simple.cpp $(CXX) $(CXXFLAGS) simple.cpp-o $(OBJS) ) simpletest.o:simpletest.cpp $(CXX) $(CXXFLAGS) simpletest.cpp-o simpletest.oclean: $(RM) $(TARGET) $(OBJS) $(LIB)

Automatic variable:

Automatic variables are variables that generate new values based on goals and dependencies each time a rule is executed

Automatic variable meaning $

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report