当前位置:网站首页>Embedded UC (UNIX System Advanced Programming) -1
Embedded UC (UNIX System Advanced Programming) -1
2022-07-05 17:03:00 【Orange peel does not stop school】
One 、 But first, let me say GCC
We all know , One .c When the document is finished , It needs a series of processing to be executed by the computer , The events that happened are as follows :
Source code (.c)- precompile -> Header files and macro extensions - compile -> Sink code (.s)- assembly -> Object code (.o)- link -> Executable code (a.out)
I believe everyone can basically write the code , Now let's take a look at the code processing
Use the command to simply write a .c file :vi hello.c
You can directly gcc helloworld.c then ./a.out Run directly and get results , But this is too detailed , We are completely unaware of this .c What happened to the file , And when we want to merge multiple tasks later , I don't know where to start .
You can use the following command , To explore the processing of code step by step , And improve it in time .
gcc -E hello.c -o hello.i precompile ( Compile preprocessing )
-o: Output , Followed by a new file generated by output .
-E: Preprocessing options .
Meaning of using this command : Yes hello.c Preprocess the source file , take stdio.h Copy all contents of the file to hello.c in , Or for some defined macros After doing things like macro replacement A new file generated hello.i file .
gcc -S hello.i Get assembly code (hello.s)
-S: Compile high-level language files into assembly language files , namely Will preprocess the output file hello.i Assemble into hello.s file , Generate assembly code , Assembly language can be regarded as a low-level language , Very close to the implementation of machine code , Conversion to machine instructions by assembly process , Assembly process is actually translating assembly language into machine code , The computer can read .
gcc -c hello.s Get the object code (hello.o)
-c: assembly , Translate your own source files into executable programs that the computer can recognize , Assemble the assembly language file of the previous step into a machine language file .
gcc hello.o -o hello Get executable code (hello)
./hello Run executable code
./ Represents... In the current directory
thus , You wrote it. C The language code , Finally, it is transformed into a form that the computer can understand , Then execute the effect of your command .
Here are some commands for development , To avoid some mistakes , And make some assignments
gcc -Wall xxx.c
-Wall: Generate all warnings .
Sometimes you may forget to assign data types to functions , After such a function is written , The compiler may not generate warnings , Because he will add int type .
But in actual development , Such code is extremely unstable , Because the type returned by this function may not be int type , It will lead to big mistakes later , At this point, you need to feed back this warning to you , So that it can be revised in time , So we can use -Wall.
gcc -Werror xxx.c
-Werror: Treat warnings as errors .
Basically the same as , such as , You apply for a local variable in a function, and the function is to return the address of this local variable , But at this time, this local variable has disappeared at the time of function return call , Then a wild pointer will be returned .
Later, it will be very wrong because of this wild pointer , And for this operation , The compiler will only warn , Or will the program be generated , It can also be executed , But the execution result at this time is likely to deviate from the correct result .
use -Werror after , Such warnings will become errors , As a result, the executable file cannot be generated at all , This error must be corrected before it can be generated normally .
gcc -x c++ cpp.c -lstdc++ -o cpp ( Even if the suffix is .c, But because of being designated c++, Will be in accordance with the c++ The rules of )
-x: Specify the language of the source code ( The extension of the file to be executed is ignored , Execute in the specified language )
-O0/O1/O2/O3: Specify the optimization level
O0 No optimization , default O1 Optimize ,O2 Emphasize speed , Sacrifice space ( Big ) Change time ( short ),O3 Emphasize volume , Sacrifice time ( Long ) Change space ( Small )
Two 、 Say the header file
1. Head file guard
for example :
#ifndef __XXX_
#define __XXX_
...
#endif
The effect is to prevent redefinition .
A header file may be contained by multiple source files , The function definitions written in the header file will therefore be extended by the preprocessor to multiple source files containing the header file , And in the compilation stage, it is compiled into many different object files , This will cause link errors :multiple definition, Multiple definitions .
gcc -I< Additional search paths for header files >
This command is used to find header files , such as , You wrote some header files yourself , Such as :#include"my.h", At this time, you can quote , But if you write it carelessly #include<my.h>, You need to specify the path of your header file to the compiler , Otherwise, the compiler will only look for the default path .
Header file , The difference between angle brackets and quotation marks :
#include <my.h>
Look for the -I Specified directory , Then find the system directory .
#include "my.h"
Look for the -I Specified directory , Find the current directory , Finally, find the system directory .
2. Preprocessing instruction
#include - Insert the specified file contents into this directive
#define - Defining macro
#undef - remove macros
#if - If
#ifdef - If the macro is defined
#ifndef - If the macro is undefined
#else - otherwise , And #if/#ifdef/#ifndef In combination with
#elif - Otherwise, if , And #if/#ifdef/#ifndef In combination with
#endif - End judgment , And #if/#ifdef/#ifndef In combination with
#error - Make a mistake , End preprocessing
#warning - Generate a warning , Continue preprocessing
#line - Specify line number
#pragma - Set the state of the compiler or indicate the operation of the compiler
#pragma GCC dependency Dependent files
#pragma GCC poison Grammatical taboos
#pragma pack( Align by a few bytes :1/2/4/8)
#pragma pack() - Align by default bytes The default four byte alignment
Realize self experiment , Need more code experience
3. Predefined macros
There is no need to define , The preprocessor will expand these macros to their corresponding values according to the preset rules .
__BASE_FILE__: The name of the source file being processed
__FILE__: The file name
__LINE__: The line number of the line
__FUNCTION__: The function name of the function
__func__: Same as __FUNCTION__
__DATE__: Processing date
__TIME__: The processing time
__INCLUDE_LEVEL__: Including layers , from 0 Start
8. environment variable
Some data saved in the process to the following : key ( function , What is it? )= value ( Content ).
env
C_INCLUDE_PATH
C Additional search paths for language header files , amount to -I Options .
CPATH
Same as C_INCLUDE_PATH
CPLUS_INCLUDE_PATH
C++ Additional search paths for language header files , amount to -I Options .
LIBRARY_PATH
Link library path
LD_LIBRARY_PATH
Load library path
The next article is about Library , You can understand these first and then continue .
边栏推荐
- 【组队 PK 赛】本周任务已开启 | 答题挑战,夯实商品详情知识
- Hiengine: comparable to the local cloud native memory database engine
- Do sqlserver have any requirements for database performance when doing CDC
- [729. My schedule I]
- American chips are no longer proud, and Chinese chips have successfully won the first place in emerging fields
- Jarvis OJ shell traffic analysis
- How can C TCP set heartbeat packets to be elegant?
- Google Earth engine (GEE) -- a brief introduction to kernel kernel functions and gray level co-occurrence matrix
- 国内首家 EMQ 加入亚马逊云科技「初创加速-全球合作伙伴网络计划」
- 项目引入jar从私服Nexus 拉去遇到的一个问题
猜你喜欢
Etcd build a highly available etcd cluster
How to set the WiFi password of the router on the computer
[729. My schedule I]
Enter a command with the keyboard
How was the middle table destroyed?
If you can't afford a real cat, you can use code to suck cats -unity particles to draw cats
Data access - entityframework integration
深潜Kotlin协程(二十一):Flow 生命周期函数
composer安装报错:No composer.lock file present.
Deep learning plus
随机推荐
What else do you not know about new map()
It is forbidden to copy content JS code on the website page
The two ways of domestic chip industry chain go hand in hand. ASML really panicked and increased cooperation on a large scale
【jmeter】jmeter脚本高级写法:接口自动化脚本内全部为变量,参数(参数可jenkins配置),函数等实现完整业务流测试
Data access - entityframework integration
Sentinel flow guard
Google Earth Engine(GEE)——Kernel核函数简单介绍以及灰度共生矩阵
Keras crash Guide
[729. My schedule I]
tf. sequence_ Mask function explanation case
关于new Map( )还有哪些是你不知道的
网站页面禁止复制内容 JS代码
[brush questions] effective Sudoku
Iphone14 with pill screen may trigger a rush for Chinese consumers
Wechat official account web page authorization login is so simple
What is ROM
挖财股票开户安全吗?怎么开股票账户是安全?
File operation --i/o
BS-XX-042 基于SSM实现人事管理系统
[brush title] goose factory shirt problem