当前位置:网站首页>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
边栏推荐
猜你喜欢

POI exports excel and displays hierarchically according to parent-child nodes

第03章_用戶與權限管理

Multithreaded printing
![[applet project development -- JD mall] uni app commodity classification page (Part 2)](/img/f3/752f41f5b5cc16c8a71498ea9cabb5.png)
[applet project development -- JD mall] uni app commodity classification page (Part 2)

Chapter 03_ User and authority management

Cookie&Session

Stop saying that you can't solve the "cross domain" problem

Redis高效点赞与取消功能

Let's just say I can use thousands of expression packs

Redis tutorial
随机推荐
Feign远程调用和Getaway网关
手把手带你了解一块电路板,从设计到制作(干货)
[applet project development -- JD mall] uni app commodity classification page (Part 2)
How to achieve 0 error (s) and 0 warning (s) in keil5
About the application of MySQL
世界上最好的学习法:费曼学习法
实战 ELK 优雅管理服务器日志
split(),splice(),slice()傻傻分不清楚?
Data exchange JSON
C language EXECL function
Cloud native annual technology inventory is released! Ride the wind and waves at the right time
C # realize solving the shortest path of unauthorized graph based on breadth first BFS -- complete program display
Stop saying that you can't solve the "cross domain" problem
go实现命令行的工具cli
EDLines: A real-time line segment detector with a false detection control翻译
最好用的信任关系自动化脚本(shell)
Introduction to EtherCAT
VMware vSphere 6.7虚拟化云管理之12、VCSA6.7更新vCenter Server许可
服务器渲染技术jsp
Md5sum operation