当前位置:网站首页>GCC usage, makefile summary
GCC usage, makefile summary
2022-07-01 03:22:00 【Twilight dust remains】
List of articles
Preface
I've been playing a game recently , To use C Programming , I used to use integration before IDE Conduct C Programming for , For the use of gcc,g++ It is true to compile code directly. I don't know , Learn about the relevant use , And learned some basic makefile Grammatical knowledge , Write a work summary
One 、gcc Use
https://gcc.gnu.org/onlinedocs/gcc-12.1.0/gcc/Overall-Options.html#Overall-Options
Official documents
see gcc edition
gcc --version
No compilation options
compile .c file , And link it into an executable file . Default output name a.out, Output the file to the current directory .
gcc main.c
Options -o
Specify the name of the output file
gcc main.c -o main.o
Options -E
Stop after the pretreatment stage ; Do not run the compiler . The output is in the form of preprocessed source code , Send to standard output . It will generate .i Final document
The preprocessing function mainly includes macro definition , File contains , Conditional compilation , To comment, etc .
The preprocessing instruction is based on # The line of code that begins with the .
file.c: Must be pretreated C Source code .
file.i: Should not be pretreated C Source code .
file.ii: Should not be pretreated C++ Source code .
gcc -E main.c -o main.i
Options -S
Compile into assembly language without linking
gcc -S main.c -o main.s
gcc -S main.i -o main.s
Options -c
Compile the file to the target file , But don't link
gcc -c main.c -o main.o
Link to generate executable
Will all .o File links , Generate executable files , The default dynamic link
gcc main.o test.o -c main # Link the target file
gcc main.c test.c -o main # Generate executable file in one step
-static This option takes a static link to the generated file
gcc main.c test.c -o main_Static --static
There are two kinds of links :
Dynamic links :GCC Default options at compile time . Dynamic means that the external code base is loaded only when the application is running , Different programs can share a code base . So the program generated by dynamic link is relatively small , Use less memory .
Static links : Use options when linking “–static”, It will package all the libraries used in the compilation phase into its own executable program . So the advantage of static link is better compatibility , Independent of the external environment , But the generated program is relatively large .
Code optimization -O
-O0 -O1 -O2 -O3 Compiler optimization options 4 A level , -O0 No optimization ,-O3 The highest level of optimization .-O3 The optimization of will be more violent , But the optimization effect is very good
gcc main.c -O1 -o main
Directory of files required for compilation -I
When we were writing code , For the beauty of the whole project and the convenience of sorting the code , Generally, they will .c and .h Separate ,-I Directory name It can make gcc Find the file directory you want to link
gcc main.c -I ./inc -o main # contain inc All files in
other
-g Generate debugging information . GNU The debugger can use this information -lpthread If the file contains multiple threads pkg-config opencv --libs contain opencv Of C++ library
Two 、Makefile file
If we input a long list of compiled instructions every time , It will be very troublesome , and makefile It can simplify our needs .
makefile A key grammar rule is as follows , Represents what goals you need to generate , What is the dependent part , Subsequent operations are placed in the command .
The goal is ...: rely on ...
command 1
command 2
...
Some special variables
[email protected]: The name of the target
$^: Construct the required file list, and the names of all files
$<: The name of the first file to construct the required file list
$?: Construct the updated file in the required file list
For specific knowledge, please refer to the following blog
https://blog.csdn.net/afei__/article/details/82696682
https://blog.csdn.net/BobYuan888/article/details/88640923
Let's start with a recent one Makefile file , You can also use it conveniently in the future . It contains c++ and c Part of the mixed compilation of languages
Makefile Example
BIN_DIR = bin
SRC_DIR = src
INC_DIR = inc
OBJ_DIR = obj
CC = gcc
C++ = g++
LD = gcc # Link the required compiler
INC = -I$(INC_DIR)
CFLAGS += $(INC)
OPT = -O1
LDFLAGS = -lm
# C file
OBJ_FILES_C = $(OBJ_DIR)/main.o \
$(OBJ_DIR)/jpeg.o \
$(OBJ_DIR)/show.o \
$(OBJ_DIR)/huff.o
# All the files
OBJ_FILES = $(OBJ_FILES_C)
# Total link executable gcode The location of
TARGET = $(BIN_DIR)/gcode
# all Represents the final generated file
all: $(TARGET)
# c File compilation
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
$(CC) $(CFLAGS) $(OPT) -c $< -o [email protected]
# cpp File compilation
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(C++) $(CFLAGS) $(OPT) -c $< -o [email protected]
# If you want to link to other libraries , Must be at the end of all .o When a file is linked as a file, it is linked opencv library
# If you are building .o When linking files, the library will eventually not be found
$(TARGET) : $(OBJ_FILES_C)
$(C++) $(LDFLAGS) $(OBJ_FILES_C) $(OPT) -o [email protected] -lpthread
# -lpthread Represents that the code contains multithreaded execution
# $(C++) $(LDFLAGS) $(OBJ_FILES) -o [email protected] `pkg-config opencv --libs` # If included opencv Libraries need to be linked
@echo "> build success <"
#make clean Instructions
clean:
rm -f $(TARGET) $(OBJ_FILES)
3、 ... and 、C and C++ Mixed programming
Post a blog
https://blog.csdn.net/this_is_me_anyway/article/details/79397018
Reference resources
gcc part
https://zhuanlan.zhihu.com/p/404682058
https://gcc.gnu.org/onlinedocs/gcc-12.1.0/gcc/Overall-Options.html#Overall-Options
Makefile part
https://blog.csdn.net/BobYuan888/article/details/88640923
https://blog.csdn.net/afei__/article/details/82696682
边栏推荐
- Hal library setting STM32 interrupt
- mybati sql 语句打印
- LeetCode_ Stack_ Difficulties_ 227. basic calculator (excluding multiplication and division)
- [small program project development -- Jingdong Mall] the home page commodity floor of uni app
- Overview of EtherCAT principle
- Redis 教程
- Mysql知识点
- 家居网购项目
- Ctfshow blasting WP
- How to achieve 0 error (s) and 0 warning (s) in keil5
猜你喜欢

Let's just say I can use thousands of expression packs
![[linear DP] longest common subsequence](/img/47/c3172422e997009facbada929adb1a.jpg)
[linear DP] longest common subsequence

第03章_用户与权限管理

Communication protocol -- Classification and characteristics Introduction

The 'mental (tiring) process' of building kubernetes/kubesphere environment with kubekey

彻底解决Lost connection to MySQL server at ‘reading initial communication packet

E15 solution for cx5120 controlling Huichuan is620n servo error

Best used trust automation script (shell)

Hello World generation

伺服第二编码器数值链接到倍福PLC的NC虚拟轴做显示
随机推荐
leetcode 1818 绝对值,排序,二分法,最大值
JS to find duplicate elements in two arrays
Chapter 03_ User and authority management
Finally in promise
A few lines of transaction codes cost me 160000 yuan
leetcode 1482 猜猜看啊,这道题目怎么二分?
Detailed explanation of ES6 deconstruction grammar
The best learning method in the world: Feynman learning method
shell脚本使用两个横杠接收外部参数
Golang多图生成gif
Data exchange JSON
终极套娃 2.0 | 云原生交付的封装
Install vcenter6.7 [vcsa6.7 (vCenter server appliance 6.7)]
一文讲解发布者订阅者模式与观察者模式
STM32 - DS18B20 temperature sampling of first-line protocol
如何校验两个文件内容是否相同
[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)
世界上最好的学习法:费曼学习法
Go tool cli for command line implementation
[linear DP] shortest editing distance