当前位置:网站首页>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
边栏推荐
- 2020-09-21 referer字符串切分 boost gateway代码组织层次
- Surveiller l'utilisation du pool de connexion des sources de données
- Fully Automated Delineation of Gross Tumor Volume for Head and Neck Cancer on PET-CT Using Deep Lear
- 装饰器模式的应用,包装ServletRequest,增加addParameter方法
- Introduction to Chang'an chain data storage and construction of MySQL storage environment
- Fully Automated Gross Tumor Volume Delineation From PET in Head and Neck Cancer Using Deep Learning
- Is it safe to open an account for stock speculation? Is it reliable?
- Slider validation code
- Data visualization: the significance of data visualization
- 证券账号开户安全吗?是靠谱的吗?
猜你喜欢

基于stm32标准库独立按键的多按键状态机的实现

CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION

Student增删gaih

CROSSFORMER: A VERSATILE VISION TRANSFORMER BASED ON CROSS-SCALE ATTENTION

FreeRTOS(九)——队列

通用分页框架

装饰器模式的应用,包装ServletRequest,增加addParameter方法

MySQL configuring master-slave databases

A comparison of methods for fully automatic segmentation of tumors and involved nodes in PET/CT of h

Visual assist plug-in settings for UE4 vs
随机推荐
阿里云服务器安装配置redis,无法远程访问
Closed door cultivation (24) shallow understanding of cross domain problems
A 3D Dual Path U-Net of Cancer Segmentation Based on MRI
2020-09-23左右值 右值引用 std::move()
[Huawei certification] the most complete and selected question bank in hcia-datacom history (with answer analysis)
语言特性
力扣94二叉树的中序遍历
Slider validation code
leetcode MYSQL数据库题目178
Monitoring data source connection pool usage
Deep Learning-based Automated Delineation of Head and Neck Malignant Lesions from PET Images
Fabrication d'une calculatrice d'addition simple basée sur pyqt5 et Qt Designer
请用已学过的知识编写程序,找出小甲鱼藏在下边这个长字符串中的密码,密码的埋藏点符合以下规律:
详细分析PBot挖矿病毒家族行为和所利用漏洞原理,提供蓝军详细防护建议
Student addition / deletion gaih
1424. 对角线遍历 II
leetcode MYSQL数据库题目176
云管理平台:OpenStack架构设计及详细解读
Official STM32 chip package download address stm32f10x stm32f40x Download
General part: cognition, design and best practice of prototype design