当前位置:网站首页>GCC and makefile
GCC and makefile
2022-06-29 09:58:00 【Water like ice】
Preface
Self contact C Since the language , Small demo direct gcc, Of large projects Makefile They are all built-in in the framework , It's usually repaired as needed , The specific rules are always confused ! When you build a small project , Rub one of your hands Makefile It's really hard. , I can't write it at all , So book this blog , Later ctrl+c.
This article will not expand on how to write a Makefile. For details , Please refer to the following very detailed tutorial , Contains almost GNU make Of Makefile All the details of :
This article contains the following :
- makefile Small template
- gcc Instructions
Makefile Small template
For pure C Language
# Instruction compiler and options
CC=gcc
CFLAGS=-Wall -std=gnu99
# Target file
TARGET=main
SRCS = main1.c \
main2.c \
main3.c
INC = -I./
OBJS = $(SRCS:.c=.o)
$(TARGET):$(OBJS)
$(CC) -o [email protected] $^
clean:
rm -rf $(TARGET) $(OBJS)
%.o:%.c
$(CC) $(CFLAGS) $(INC) -o [email protected] -c $<
Be careful :Makefile There is a rule that the command line is based on tab Key start ,4 Spaces or others will report errors :Makefile:2: *** missing separator.stop
- Compared to a single file and multiple files makefile, Through the variable
INCThe header file path is established . Header file paths are separated by spaces . - Compilation Rules
%.o:%.cHeader file parameters are added to$(CC) $(CFLAGS) $(INC) -o [email protected] -c $<, - Single file and multiple files makefile The header file path parameter is added .
SRCSvariable , If there are many documents, you can use“\”Symbol continuation .[email protected]-- Represents the target document$^-- Represents all dependent files$<-- Represents the first dependent file ( The one on the far left ).
Apply to C/C++ Mixed compilation
The directory structure is as follows :
httpserver
│ main.cpp
│ Makefile
└─────inc
│ │ mongoose.h
│ │ http_server.h
│
──────src
│ │ http_server.cpp
│ │ mongoose.c
│ │ ...
Makefile as follows :
CC=gcc
CXX=g++
# Compiler parameter settings at compile time , Include header file path settings
CFLAGS:=-Wall -O2 -g
CFLAGS+=-I $(shell pwd)/inc
CXXFLAGS:=-Wall -O2 -g -std=c++11
CXXFLAGS+=-I $(shell pwd)/inc
# Library file add
LDFLAGS:=
LDFLAGS+=
# Specify the storage location of the source program
SRCDIRS:=.
SRCDIRS+=src
# Set the file type used in the program
SRCEXTS:=.c .cpp
# Set the name of the running program
PROGRAM:=httpserver
SOURCES=$(foreach d,$(SRCDIRS),$(wildcard $(addprefix $(d)/*,$(SRCEXTS))))
OBJS=$(foreach x,$(SRCEXTS),$(patsubst %$(x),%.o,$(filter %$(x),$(SOURCES))))
.PHONY: all clean distclean install
%.o: %.c
$(CC) -c $(CFLAGS) -o [email protected] $<
%.o: %.cxx
$(CXX) -c $(CXXFLAGS) -o [email protected] $<
$(PROGRAM): $(OBJS)
ifeq ($(strip $(SRCEXTS)),.c)
$(CC) -o $(PROGRAM) $(OBJS) $(LDFLAGS)
else
$(CXX) -o $(PROGRAM) $(OBJS) $(LDFLAGS)
endif
install:
install -m 755 -D -p $(PROGRAM) ./bin
clean:
rm -f $(shell find -name "*.o")
rm -f $(PROGRAM)
distclean:
rm -f $(shell find -name "*.o")
rm -f $(shell find -name "*.d")
rm -f $(PROGRAM)
all:
@echo $(OBJS)
gcc Instructions
One step in place
gcc main.c -o main
Compilation of multiple program files
gcc main1.c main2.c -o main
Preprocessing
gcc -E main.c -o main.i
or gcc -E main.c
gcc Of -E Options , You can make the compiler stop after preprocessing , And output the preprocessing results .
Compile into assembly code
After pretreatment , Can be generated directly on test.i File compilation , Generate assembly code :gcc -S main.i -o main.s
gcc Of -S Options , Indicates that during program compilation , After generating assembly code , stop it ,-o Output assembly code file .
assembly
For the assembly code file generated above test.s,gas The assembler is responsible for compiling it into the target file , as follows :gcc -c main.s -o main.o
Connect
gcc The connector is gas Provided , Be responsible for connecting the object file of the program with all additional object files required , Final generation of executable . Additional target files include static connection library and dynamic connection Library .
For the... Generated in the previous section main.o, With the C Connect to standard I / O libraries , Finally generate executable program main.
Error detection
Parameters -Wall, Use it to make GCC Generate as many warning messages as possible .gcc -Wall main.c -o main
Bring... When compiling the program -Werror Options , that GCC Will stop compiling wherever warnings are generated , Force programmers to modify their code , as follows :gcc -Werrormain.c -o main
Create a dynamic link library
To generate o file gcc -c -fPIC add.c // We have to add -fPIC Options , The purpose is to make the library not care about the function location in the file
To compile gcc -shared -fPIC -o libadd.so add.o
Library file connection
When developing software , It's rare not to use a third-party library at all , Generally speaking, we need the support of many function libraries to complete the corresponding functions . From a programmer's point of view , The function library is actually some header files (.h) And libraries (so、 or lib、dll) Set . although Linux Most of the functions under will put the header file in by default /usr/include/ Under the table of contents , The library file is placed in /usr/lib/ Under the table of contents ; But sometimes , The library we want to use is not in these directories , therefore GCC When compiling, you must use your own method to find the required header file and library file .
Add extra :Linux Connection required so The library files ( With soft connection ), It can be completely copied to /usr/include/ or /usr/lib/ Under the table of contents , Use cp -d * /usr/lib/ command , Then don't forget to run again ldconfig.
among inclulde The path to the folder is /home/test/include,lib The folder is /home/test/lib,lib The folder contains binary so file libtest.so
First, compile main.c For the target file , This is the time to execute :gcc –c –I /home/test/include main.c –o main.o
Finally, link all the target files into executable files :gcc –L /home/test/lib -ltest main.o –o main
By default , GCC Dynamic link library is preferred when linking , Consider using static link libraries only if they don't exist , Add... At compile time if necessary -static Options , Force the use of static link libraries .gcc –L /home/test/lib -static -ltest main.o –o main
Search path order when static library Links :
ldWill go to GCC Parameters in the command -L- Look again gcc Environment variables of
LIBRARY_PATH - Find the internal directory again
/lib、/usr/lib、/usr/local/libThis is when compile gcc It's written in the program
Dynamic links 、 Search path order at execution time :
- The dynamic library search path specified when compiling the object code
- environment variable
LD_LIBRARY_PATHSpecified dynamic library search path - The configuration file
/etc/ld.so.confThe dynamic library search path specified in - Default dynamic library search path
/lib - Default dynamic library search path
/usr/lib
Related environment variables :LIBRARY_PATH environment variable : Specify the program static link library file search path LD_LIBRARY_PATH environment variable : Specify the program DLL file search path
边栏推荐
- 数据仓库:金融/银行业的分层架构篇
- Making of simple addition calculator based on pyqt5 and QT Designer
- kdevelop新建工程
- KDevelop new project
- Matlab tips (21) matrix analysis -- partial least squares regression
- Do you know what BFD is? This article explains the principle and usage scenarios of BFD protocol in detail
- Invalidconnectionattributeexception exception exception handling
- 367. effective complete square dichotomy
- How to set Google Chrome as the default browser
- 2020-9-14 广告系统入门
猜你喜欢

容器

kdevelop新建工程

KDevelop new project

Custom MVC framework implementation

A 3D Dual Path U-Net of Cancer Segmentation Based on MRI

Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning

Gmail:如何快速将邮件全部已读

Cisco ASA、FTD和HyperFlex HX的漏洞分析复现

【华为认证】HCIA-DATACOM史上最全精选题库(附答案解析)

基于stm32标准库独立按键的多按键状态机的实现
随机推荐
MySQL modify auto increment initial value
ImageView图片填充问题
Data visualization: the four quadrants of data visualization teach you to correctly apply icons
CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION
Making of simple addition calculator based on pyqt5 and QT Designer
FreeRTOS(八)——时间管理
云管理平台:OpenStack架构设计及详细解读
Fully Automated Delineation of Gross Tumor Volume for Head and Neck Cancer on PET-CT Using Deep Lear
leetcode MYSQL数据库题目176
linux环境下安装配置redis,并设置开机自启动
安装Anaconda后启动JupyterLab需要输入密码
微信小程序实现数据侦听器watch,包含销毁watch和子属性的watch
Idea auto completion
在Activity外使用startActivity()方法报错原因与解决办法
Wechat applet implements the data listener watch, including the watch that destroys the watch and sub attributes
Application of decorator mode, packaging ServletRequest and adding addparameter method
数据可视化:数据可视化四象限,教你正确应用图标
Slider validation code
mysql修改自动递增初始值
长安链GO语言智能合约环境搭建及使用