当前位置:网站首页>Lesson 3: gcc compiler
Lesson 3: gcc compiler
2022-07-26 08:23:00 【Eight Liang x】
I typed the content by hand according to the video , There are lessons, bullet screen content and my own thinking
Special thanks “xiaobing1016”, Links are as follows , I hope you can support the originality up be based on VSCode and CMake Realization C/C++ Development | Linux piece _ Bili, Bili _bilibili
Preface :
1.GCC The compiler supports compiling Go、ObectiveC,Objective-C++,Fortran,Ada,D and BRIG(HSAIL) Applications such as ;
2.Linux Development C/C++ Be familiar with GCC
3.VSCode By calling GCC Compiler to implement C/C++ The compilation of ;
In practice :
Use gcc Instruction compilation C Code
Use g++ Instruction compilation C++ Code
3.1 The build process
1. Preprocessing -Pre-Processing //.i file
#-E Option instructs the compiler to preprocess only the input file
g++ -E test.cpp -o test i //.i file 2. compile -Compiling //.s file
#-S Compilation options tell q++ For C++ Stop compiling after the code generates the assembly language file
# g++ The default extension of the generated assembly language file is .s
g++ -S test.i -o test.s3. assembly -Assembling //.o file
#-c Options tell g++ Only compile the source code into the object code of machine language
# By default g++ The created object code file has a .o Extension .
g++ -c test.s -0 test.o4. link -Linking //bin file
# -o Compile option to use the specified file name for the executable to be generated
g++ test.o -o testTips: The sum of the above four steps equals g++ test.cpp -o test, Act as like as two peas. , The above mainly explains the following principles , Those who feel troublesome can use it directly g++ test.cpp -o test
3.2g++ Important compilation parameters
1.-g Compile the executable with debugging information
#-g Options tell GCC Produce something that can be GNU The debugger GDB Debug information used , To debug the program .
# Generate an executable file with debugging information test
g++ -g test.cpp -o test2.-O[n] Optimize source code
## So called optimization , For example, omit variables that have never been used in the code 、 Directly replace the constant expression with the result value, etc , These operations will reduce the target text
The amount of code contained in the piece , Improve the running efficiency of the final generated executable file .
#-O! Options tell q++ Basic optimization of source code . In most cases, these optimizations will make the program execute faster . -o2 Options tell g++ Generate code as small and as fast as possible . Such as -O2,-O3,-On(n Often 0-3)
#-O! At the same time, reduce the length of code and execution time , Its effect is equivalent to -O1
#-O10 It means no optimization
# -O!1 Is the default optimization
#-O!2 Besides finishing -O1 Beyond the optimization of , There are also some additional adjustments , Such as command adjustment, etc .
#-O!3 It includes loop expansion and other optimization work related to processing characteristics .
# Option will make compilation faster than using -O! Time slow , But usually the resulting code executes faster .
# Use -O2 Optimize source code , And output the executable
g++ -O2 test.cppBefore and after optimization :
3.-l and -L Specify the library file | Specify the path to the library file
#-l Parameters ( A lowercase letter ) It is used to specify the library to which the program will link ,-l The parameter is followed by the library name
# stay /1ib and /usr/lib and /usr/1ocal/1ib The library in the library is used directly -l Parameters can link
# link glog library
g++ -lglog test.cpp
# If the library file is not in the above three directories , Need to use -L Parameters ( Capitalization ) Specify the directory where the library files are located
#-L The parameter is followed by the name of the directory where the library file is located
# link mytest library ,libmytest.so stay /home/bing/mytestlibfolder Under the table of contents
g++ -L/home/bing/mytestlibfolder -lmytest test.cpp
4.-I Specify the header file search directory
# -I (include)
#/usr/include There is no need to specify the directory at the moment .gcc Know where to find , But if the header file is not in /usr/icnclude We're going to use -I The parameter specifies , For example, the header file is placed in /myinclude Directory , Then compile the command line to add -I/myinclude Parameters , If you don't, you'll get a "xxxx.h:No such file ordirectory” Error of .-I Parameters can be relative paths . For example, the header file is in the current directory . It can be used -I. To specify the . As we mentioned above -cflags Parameters are used to generate -I Parametric .
G++ -I/myinclude test.cpp
5.-Wall Print warning messages
# Print out gcc Warning information provided
g++ -wall test.cpp
6-.w Turn off warning messages
# Turn off all warning messages
g++-wtest.cpp
7. -std=c++11 Set compilation criteria ( Very important )
# Use c++11 Standard compilation testcpp
g++ -std=c++11 testcpp8.-o Specify output file name
# Specify the file name to be generated
# Specifies that the output executable is named test
g++ test.cpp -o test
Tips: No addition - o Will generate a x.out The executable of 9.-D Defining macro
# In the use of gcc/q++ Define macros when compiling
# Common scenarios :
# DDEBUG Definition DEBUG macro , There may be... In the file DEBUG Information about the macro section , Use one DDEBUG To select on or off DEBUG
Sample code :
//-Dname Defining macro name The default definition content is string “1”
#include<stdio.h
int main()
{
#ifdef DEBUG
printf("DEBUG LOG\n");
#endif
printf(“in/n”);
}
//1. At compile time , Use gcc -DDEBUG main.cpp
//2. The seventh line of code can be executed notes : Use man gcc Commands can be viewed gcc English user manual , Pictured :

3.3【 actual combat 】g++ Command line compilation
Case study : The original directory structure :2 directories,3 files
# The original directory structure
.
├── include
│ └── swap.h
├── main.cpp
└── src
└── swap.cpp
2 directories, 3 files3.3.1 Compile directly
The simplest compilation , And run
# take main.cpp src/Swap.cpp Compile to executable
g++ main.cpp src/Swap.cpp -Iinclude
# function a.out
./a.outTips: Don't write directly g++ main.cpp src/swap.cpp, This will give you an error

Because the header file cannot be found , So add -Iinclude command
Add parameter compilation , And run
# take main.cpp src/Swap.cpp Compile to executable With a bunch of parameters
g++ main.cpp src/Swap.cpp -Iinclude -std=c++11 -O2 -Wall -o b.out
# function b.out
./b.out3.3.2 Generate library files and compile
Link static libraries to generate executables ①:
## Get into src Under the table of contents
$cd src
# assembly , Generate Swap.o file
g++ Swap.cpp -c -I../include
# Generate static libraries libSwap.a, The static library file is in
ar rs libSwap.a Swap.o
## Go back to the parent directory
$cd ..
# link , Generate executable files :staticmain -lswap Connect to a swap Static library ,-Lsrc Is the address of the static library file src/, And the header file directory -Iinclude, Be short of one cannot ,L It starts with relative paths
g++ main.cpp -Iinclude -Lsrc -lswap -o staticmainLink dynamic library to generate executable file ②:
## Get into src Under the table of contents
$cd src
# Generate dynamic library libSwap.so
g++ Swap.cpp -I../include -fPIC -shared -o libSwap.so
## The above command is equivalent to the following two commands
# gcc Swap.cpp -I../include -c -fPIC
# gcc -shared -o libSwap.so Swap.o
## Go back to the parent directory
$cd ..
# link , Generate executable files :sharemain
g++ main.cpp -Iinclude -Lsrc -lSwap -o sharemainStatic library Dynamic library :
stay Linux in , Library files are divided into static library and shared library . Static library to .a Suffixed name , Shared libraries to .so ending . All libraries are packaged collections of functions , The difference is that the static library generates a copy every time it is called , Shared libraries have only one copy , More save a space .
Static libraries will eventually be integrated into executable files , Dynamic libraries will not
The static library has integrated all the files , Dynamic libraries are just links , Using the original file
Static library , After compiling the program , Direct link , Generate final file , If you change part of the source code of the main file , Need to relink . Dynamic library , The program runs behind memory , Then link the library file , If the source code is modified , The recompile link section does not involve dynamic library files , So it's more convenient .
Relatively speaking , Static library linked files are also larger , After all, it was linked before running , It is equivalent to copying the source code to the main program in advance .
3.3.3 Run the executable
Run the executable ①
# Run the executable
./staticmainRun the executable ②
# Run the executable ,sharemain Is the name of the dynamic library file
LD_LIBRARY_PATH=src ./sharemain
边栏推荐
- SPSS用KMeans、两阶段聚类、RFM模型在P2P网络金融研究借款人、出款人行为规律数据
- sed作业
- A summary of practical websites that won't brighten people's eyes
- Condition judgment function of MySQL function summary
- C# WinForm中PreviewKeyDown、KeyDown、KeyPress、KeyUp区别与联系
- Data validation typeerror: qiao Validate is not a function
- [endnote] detailed explanation of document template layout syntax
- JSP action -- usebean action
- C # get the information of the selected file
- 2022/7/1
猜你喜欢

Ten thousand words long article | deeply understand the architecture principle of openfeign

Basic configuration of BGP

Shell programming

苹果强硬新规:用第三方支付也要抽成,开发者亏大了!

2022-7-9 personal qualifying 6 competition experience

BGP的基本配置

If the thread crashes, why doesn't it cause the JVM to crash? What about the main thread?

JSP action -- usebean action

我,35岁了。

CentOS install mysql5.7
随机推荐
CentOS install mysql5.7
Nodejs2day(nodejs的模块化,npm下载包,模块加载机制)
Two ways to monitor the change of user points
Burp Suite-第八章 如何使用Burp Intruder
The first ide overlord in the universe, replaced...
通用 DAO 接口设计
How WPS sets page headers page by page
Error handling response: Error: Syntax error, unrecognized expression: .c-container /deep/ .c-contai
Dev gridcontrol captures key events
随机分布学习笔记
Burp Suite - Chapter 2 burp suite proxy and browser settings
[endnote] detailed explanation of document template layout syntax
Day 4 homework
Random distribution learning notes
B title: razlika priority queue approach
Code cloud change remote warehouse command
Software engineering -- dental clinic -- demand analysis
OSPF总结
99 multiplication table and inverted triangle 99 multiplication table
An empirical study on urban unemployment in Guangxi (Macroeconomics)