4058 views|4 replies

654

Posts

26

Resources
The OP
 

18. "Ten Thousand Miles" Raspberry Pi Car——Makefile Learning [Copy link]

 This post was last edited by lb8820265 on 2021-12-13 23:43

I have introduced the motor driver C++ code before , and used Makefile for multi-file compilation. I didn’t want to learn it in depth, but later I found that it was really difficult to make any progress without knowing the Makefile language. It’s time to learn it.

For systematic learning, I still recommend the "C Language Chinese Network" http://c.biancheng.net/makefile/. Makefile is a plain text. The content of the motor driver Makefile is as follows.

CROSS =
CC = $(CROSS)gcc
CXX = $(CROSS)g++
DEBUG = -g -O2
CFLAGS = $(DEBUG) -Wall -c
SRCS = $(wildcard ./*.cpp)
OBJS = $(patsubst %.cpp, %.o, $(SRCS))
TARGET = out
$(TARGET) : $(OBJS)
$(CXX) -o $@ $^ -lwiringPi 
rm -rf $(OBJS)
$(OBJS):%.o : %.cpp
$(CXX) $(CFLAGS) $< -o $@

This may seem like a lot of gibberish, but here are the basics.

Structure of a Makefile

targets : prerequisites
   Command

The last five lines of code in the motor driver Makefile reflect this basic structure. The following example shows how to use the Makefile rules:

test:test.c 
gcc -o test test.c
  • targets: The target of the rule, which can be an Object File (generally called an intermediate file), an executable file, or a label;
  • prerequisites: These are our dependency files, files or targets needed to generate targets. There can be multiple or none.
  • command: The command that make needs to execute (any shell command). There can be multiple commands, each command occupies one line.

The function implemented by the above code is to compile the test.c file. This example can explain the specific use of Makefile in detail. Among them, test is the target file, which is also the executable file we finally generate. The dependent file is the test.c source file. The operation to be executed to rebuild the target file is gcc -o test test.c.

Makefile working process

The motor driver Makefile needs to compile multiple files, so you need to know the workflow. For example, when a project with two files, main.c and test1.c, needs to be compiled, the Makefile is as follows.

main:main.o test1.o
 gcc main.o test1.o -o main
main.o:main.c test.h
 gcc -c main.c -o main.o
test1.o:test1.c test.h
 gcc -c test1.c -o test1.o

The first line is the file that needs to be generated in the end. main is the executable file name, and main.o and test1.o are dependent files, that is, intermediate files.

The second line is what needs to be done to generate main.

The third line is the dependency file needed to generate the middleware main.o.

The fourth line is what needs to be done to generate main.o.

The fifth and sixth lines have similar meanings to the third and fourth lines.

Basic syntax of gcc

Here is a summary of the most basic gcc compilation and linking commands. For details, see " C Language Chinese Website ".

Compile:

gcc -c test.s

Compile the assembly output file test.s into the output file test.o. Adding " -Wall " means generating as many warning messages as possible.

Link:

gcc test.c -o test

Preprocess, assemble, compile and link test.c to form an executable file test. The -o option is used to specify the file name of the output file, which can be left blank. -lwiringPi means adding the wiringPi link library

Definition of variables

In Makefile, the "=" sign is called a variable definition, which is a bit like a macro definition. The right side of the equal sign can be replaced by the left side of the equal sign. For example.

VALUE_LIST = one two three

When calling a variable, use

$(VALUE_LIST)

The first 8 lines in the motor driver are the definition and call of the variables used.

Wildcards

In the code, " *.cpp " means all files with the suffix " .cpp ". "*": matches 0 or any number of characters. But please note that you cannot use wildcards by referencing variables, for example

OBJ=*.c
test:$(OBJ)

It is directly recognized as a file. The file name is "*.c". The correct way is to use the extended function "wildcard", as shown below .

OBJ=$(wildcard *.c)

By the way, let me introduce the path. Assuming that there is a subfolder "sub" in the project, the command to select all .c files in the folder is.

OBJ=$(wildcard ./sub/*.c)

The current path can be replaced with "./" or by default.

In the code, “ %.o : %.cpp ” means extracting a list of all file names ending with “.o” and then replacing those with the “.cpp” suffix.

There is also a wildcard replacement function patsubst, which requires three parameters. The first is the pattern to be matched, the second indicates what to replace it with, and the third is the string to be processed.

Automation variables

When describing rules in Makefile, the dependent files and target files are variable, and obviously the specific file names cannot appear in the command, otherwise the pattern rules will lose their meaning. This requires the use of "automatic variables".

$@: Indicates the target file name of the rule.

$<: The file name of the first dependency of the rule.

$^: represents a list of all dependent files, separated by spaces.

Delete command

The famous code for deleting the database and running away.

rm -rf

Delete all files and directories in the current directory directly without confirming them one by one.

Operation Results

Knowing the above rules, replacing all variables, automatic variables, and wildcards, the meaning of this Makefile is clear. Enter make and the following command will be output.

g++ -g -O2 -Wall -c Emakefun_MotorDriver.cpp -o  Emakefun_MotorDriver.o 
g++ -g -O2 -Wall -c DC_MotoTest.cpp -o  DC_MotoTest.o 
g++ -g -O2 -Wall -c Emakefun_MotorShield.cpp -o  Emakefun_MotorShield.o 
g++ -g -O2 -Wall -c Raspi_i2c.cpp -o  Raspi_i2c.o 
g++ -o out Emakefun_MotorDriver.o DC_MotoTest.o Emakefun_MotorShield.o Raspi_i2c.o -lwiringPi  
rm -rf  Emakefun_MotorDriver.o  DC_MotoTest.o  Emakefun_MotorShield.o  Raspi_i2c.o

question

The Makefile file looks quite complicated. Is there a simpler way to let some software generate it automatically instead of writing the Makefile myself?

This post is from Innovation Lab

Latest reply

Summary of "Wanli" Raspberry Pi car: lb8820265's "Wanli" Raspberry Pi car open source sharing - DIY/Open Source Hardware Zone - Electronic Engineering World - Forum (eeworld.com.cn) Table of contents: "Wanli" Raspberry Pi car launched 1. “Wanli” Raspberry Pi car - Establishing a project warehouse 2. "Wanli" Raspberry Pi car - Python learning (using Thonny) 3. "Wanli" Raspberry Pi car - Python learning (timing task) 4. "Wanli" Raspberry Pi car - C++ learning (compile and run, use geany) 5. "Ten Thousand Miles" Raspberry Pi Car - WiringPi Learning (Delay and Thread Simulation Timer) 6. "Wanli" Raspberry Pi car - wiringPi learning (PWM and external interrupt simulation timer) 7. "Ten Thousand Miles" Raspberry Pi Car——RPi.GPIO Learning (PWM and External Interrupt Simulation Timer) 8. "Wanli" Raspberry Pi car - socket learning (local communication) 9. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (TCP Two-Machine Communication) 10. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (UDP Two-Machine Communication) 11. "Wanli" Raspberry Pi car - socket learning (sent from Android) 12 "Wanli" Raspberry Pi car - socket learning (Android sending and receiving) 13. "Wanli" Raspberry Pi Car - Accessories Preparation 14 "Wanli" Raspberry Pi car - motor drive learning 15 "Wanli" Raspberry Pi car - photoelectric encoder learning (forward and reverse judgment) 16. "Wanli" Raspberry Pi car - photoelectric encoder learning (obtaining speed) 17 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Compiling and Debugging) 18. "Ten Thousand Miles" Raspberry Pi Car——Makefile Learning 19 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Multiple C File Link Debugging) 20 "Million Miles" Raspberry Pi Car - Motor Control Learning (Control Speed) 21. "Wanli" Raspberry Pi car - motor control learning (4-wheel speed control) 22. "Wanli" Raspberry Pi car - mobile phone remote control motor rotation 23 "Wanli" Raspberry Pi car - connected to Raspberry Pi without screen 24 "Millions" Raspberry Pi Car - Bullseye Benchmark Test of Raspberry Pi 64-bit System 25 "Million Miles" Raspberry Pi Car - Nam Wheel Control 26 "Wanli" Raspberry Pi car - program startup 27 "Ten Thousand Miles" Raspberry Pi Car - Fix and Get the Raspberry Pi IP Address 28 "Wanli" Raspberry Pi car - car assembly 29 "Wanli" Raspberry Pi car - straight-driving deviation problem and new control mode 30. "Wanli" Raspberry Pi car - Phase 1 completed demonstration (introduction from scratch)   Details Published on 2022-3-21 13:39
Personal signatureQQ:252669569
 
 

1w

Posts

204

Resources
From 5
 

Summary of "Wanli" Raspberry Pi car:

lb8820265's "Wanli" Raspberry Pi car open source sharing - DIY/Open Source Hardware Zone - Electronic Engineering World - Forum (eeworld.com.cn)

Table of contents:

"Wanli" Raspberry Pi car launched

1. “Wanli” Raspberry Pi car - Establishing a project warehouse

2. "Wanli" Raspberry Pi car - Python learning (using Thonny)

3. "Wanli" Raspberry Pi car - Python learning (timing task)

4. "Wanli" Raspberry Pi car - C++ learning (compile and run, use geany)

5. "Ten Thousand Miles" Raspberry Pi Car - WiringPi Learning (Delay and Thread Simulation Timer)

6. "Wanli" Raspberry Pi car - wiringPi learning (PWM and external interrupt simulation timer)

7. "Ten Thousand Miles" Raspberry Pi Car——RPi.GPIO Learning (PWM and External Interrupt Simulation Timer)

8. "Wanli" Raspberry Pi car - socket learning (local communication)

9. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (TCP Two-Machine Communication)

10. "Ten Thousand Miles" Raspberry Pi Car - Socket Learning (UDP Two-Machine Communication)

11. "Wanli" Raspberry Pi car - socket learning (sent from Android)

12 "Wanli" Raspberry Pi car - socket learning (Android sending and receiving)

13. "Wanli" Raspberry Pi Car - Accessories Preparation

14 "Wanli" Raspberry Pi car - motor drive learning

15 "Wanli" Raspberry Pi car - photoelectric encoder learning (forward and reverse judgment)

16. "Wanli" Raspberry Pi car - photoelectric encoder learning (obtaining speed)

17 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Compiling and Debugging)

18. "Ten Thousand Miles" Raspberry Pi Car——Makefile Learning

19 "Ten Thousand Miles" Raspberry Pi Car——VSCode Learning (Multiple C File Link Debugging)

20 "Million Miles" Raspberry Pi Car - Motor Control Learning (Control Speed)

21. "Wanli" Raspberry Pi car - motor control learning (4-wheel speed control)
22. "Wanli" Raspberry Pi car - mobile phone remote control motor rotation

23 "Wanli" Raspberry Pi car - connected to Raspberry Pi without screen

24 "Millions" Raspberry Pi Car - Bullseye Benchmark Test of Raspberry Pi 64-bit System

25 "Million Miles" Raspberry Pi Car - Nam Wheel Control

26 "Wanli" Raspberry Pi car - program startup

27 "Ten Thousand Miles" Raspberry Pi Car - Fix and Get the Raspberry Pi IP Address

28 "Wanli" Raspberry Pi car - car assembly

29 "Wanli" Raspberry Pi car - straight-driving deviation problem and new control mode

30. "Wanli" Raspberry Pi car - Phase 1 completed demonstration (introduction from scratch)

This post is from Innovation Lab
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle
Personal signature

玩板看这里:

http://en.eeworld.com/bbs/elecplay.html

EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!

 
 
 

7422

Posts

2

Resources
2
 

Just learn about Makefile. Nowadays, there are too few people who write makefiles by hand. There are also many pitfalls and troubles in using cmake. Microsoft's VS is still better, haha.

This post is from Innovation Lab
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 
 

1236

Posts

66

Resources
3
 

Convenient with cmake

This post is from Innovation Lab
 
 
 

6062

Posts

4

Resources
4
 

Well, you have to understand.

This post is from Innovation Lab
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list