当前位置:网站首页>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
边栏推荐
- How to traverse objects in the vector container
- 你知道BFD是什么吗?一文详解BFD协议原理及使用场景
- Es error nonodeavailableexception[none of the configured nodes are available:[.127.0.0.1}{127.0.0.1:9300]
- 详细分析PBot挖矿病毒家族行为和所利用漏洞原理,提供蓝军详细防护建议
- 【技术开发】酒精测试仪解决方案开发设计
- Basic operations of MAC MySQL database
- 数据可视化:数据可视化的意义
- The 23 most useful elasticsearch search techniques you must know
- 力扣94二叉树的中序遍历
- 367. 有效的完全平方数-二分法
猜你喜欢

力扣94二叉树的中序遍历

Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images

UE4 material UV texture does not stretch with model scale

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

C语言中通过sprintf()函数构造sql语句
![[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)](/img/d4/f5ea847573433f7ca7bd429f57e40a.png)
[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)

Zabbix4.4 configure the indicators of the monitoring server and solve the garbled graphics pages

1424. diagonal traversal II

Segmentation of Head and Neck Tumours Using Modified U-net

Data visualization: the four quadrants of data visualization teach you to correctly apply icons
随机推荐
Cloud management platform: 9 open source cloud management platforms (CMP)
The 23 most useful elasticsearch search techniques you must know
Idea debugging fails, reporting jdwp no transports initialized, jvmtierror=agent_ ERROR_ TRANSPORT_ LOAD(196)
微信小程序实现数据侦听器watch,包含销毁watch和子属性的watch
Fabrication d'une calculatrice d'addition simple basée sur pyqt5 et Qt Designer
遍历vector容器中的对象的方式
你必须知道的23个最有用的Elasticseaerch检索技巧
数据可视化:数据可视化的意义
官方stm32芯片包下载地址 stm32f10x stm32f40x下载
A 2.5D Cancer Segmentation for MRI Images Based on U-Net
[noi Simulation Competition] add points for noi (heavy chain dissection, line segment tree)
Wechat applet implements the data listener watch, including the watch that destroys the watch and sub attributes
Zabbix4.4 configure the indicators of the monitoring server and solve the garbled graphics pages
float 与 int 相乘产生的令人崩溃的“ 2.3 * 10 = 22 ”
[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)
C # judge whether the array contains any items of another array
mysql修改自动递增初始值
Segmentation of Head and Neck Tumours Using Modified U-net
JS获取手机型号和系统版本
Fully Automated Delineation of Gross Tumor Volume for Head and Neck Cancer on PET-CT Using Deep Lear