当前位置:网站首页>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
边栏推荐
- 手把手带你了解一块电路板,从设计到制作(干货)
- leetcode 1818 绝对值,排序,二分法,最大值
- 过滤器 Filter
- [applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)
- VMware vSphere 6.7虚拟化云管理之12、VCSA6.7更新vCenter Server许可
- Cookie&Session
- How to use hybrid format to output ISO files? isohybrid:command not found
- JUC学习
- Cloud native annual technology inventory is released! Ride the wind and waves at the right time
- 一文讲解发布者订阅者模式与观察者模式
猜你喜欢
随机推荐
手把手带你了解一块电路板,从设计到制作(干货)
[applet project development -- Jingdong Mall] user defined search component of uni app (Part 1)
几行事务代码,让我赔了16万
Design of serial port receiving data scheme
The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
Keil5中如何做到 0 Error(s), 0 Warning(s).
md5sum操作
倍福TwinCAT3 Ads相关错误详细列表
Kmeans
Finally in promise
gcc使用、Makefile总结
Huawei operator level router configuration example | configuration optionA mode cross domain LDP VPLS example
Pytest -- plug-in writing
Druid monitoring statistics source
最好用的信任关系自动化脚本(shell)
彻底解决Lost connection to MySQL server at ‘reading initial communication packet
Detailed explanation of ES6 deconstruction grammar
MySQL knowledge points
【日常训练】1175. 质数排列
A few lines of transaction codes cost me 160000 yuan









