How does the compilation/linking process work?

本文详细阐述了C++程序从源代码到可执行文件的编译链接过程,包括预处理、编译和链接三个阶段的关键步骤及注意事项。

The compilation of a C++ program involves three steps:

  1. Preprocessing: the preprocessor takes a C++ source code file and deals with the #includes, #defines and other preprocessor directives. The output of this step is a "pure" C++ file without pre-processor directives.

  2. Compilation: the compiler takes the pre-processor's output and produces an object file from it.

  3. Linking: the linker takes the object files produced by the compiler and produces either a library or an executable file.

Preprocessing

The preprocessor handles the preprocessor directives, like #include and #define. It is agnostic of the syntax of C++, which is why it must be used with care.

It works on one C++ source file at a time by replacing #include directives with the content of the respective files (which is usually just declarations), doing replacement of macros (#define), and selecting different portions of text depending of #if#ifdef and #ifndef directives.

The preprocessor works on a stream of preprocessing tokens. Macro substitution is defined as replacing tokens with other tokens (the operator ## enables merging two tokens when it make sense).

After all this, the preprocessor produces a single output that is a stream of tokens resulting from the transformations described above. It also adds some special markers that tell the compiler where each line came from so that it can use those to produce sensible error messages.

Some errors can be produced at this stage with clever use of the #if and #error directives.

Compilation

The compilation step is performed on each output of the preprocessor. The compiler parses the pure C++ source code (now without any preprocessor directives) and produces an object file. This object file contains the compiled code (in binary form) of the symbols defined in the input. Symbols in object files are referred to by name.

Object files can refer to symbols that are not defined. This is the case when you use a declaration, and don't provide a definition for it. The compiler doesn't mind this, and will happily produce the object file as long as the source code is well-formed.

Compilers usually let you stop compilation at this point. This is very useful because with it you can compile each source code file separately. The advantage this provides is that you don't need to recompile everything if you only change a single file.

The produced object files can be put in special archives called static libraries, for easier reusing later on.

It's at this stage that "regular" compiler errors, like syntax errors or failed overload resolution errors, are reported.

Linking

The linker is what produces the final compilation output from the object files the compiler produced. This output can be either a shared (or dynamic) library (and while the name is similar, they haven't got much in common with static libraries mentioned earlier) or an executable.

It links all the object files by replacing the references to undefined symbols with the correct addresses. Each of these symbols can be defined in other object files or in libraries. If they are defined in libraries other than the standard library, you need to tell the linker about them.

At this stage the most common errors are missing definitions or duplicate definitions. The former means that either the definitions don't exist (i.e. they are not written), or that the object files or libraries where they reside were not given to the linker. The latter is obvious: the same symbol was defined in two different object files or libraries.


http://stackoverflow.com/questions/6264249/how-does-the-compilation-linking-process-work

MATLAB代码实现了一个基于多种智能优化算法优化RBF神经网络的回归预测模型,其核心是通过智能优化算法自动寻找最优的RBF扩展参数(spread),以提升预测精度。 1.主要功能 多算法优化RBF网络:使用多种智能优化算法优化RBF神经网络的核心参数spread。 回归预测:对输入特征进行回归预测,适用于连续值输出问题。 性能对比:对比不同优化算法在训练集和测试集上的预测性能,绘制适应度曲线、预测对比图、误差指标柱状图等。 2.算法步骤 数据准备:导入数据,随机打乱,划分训练集和测试集(默认7:3)。 数据归一化:使用mapminmax将输入和输出归一化到[0,1]区间。 标准RBF建模:使用固定spread=100建立基准RBF模型。 智能优化循环: 调用优化算法(从指定文件夹中读取算法文件)优化spread参数。 使用优化后的spread重新训练RBF网络。 评估预测结果,保存性能指标。 结果可视化: 绘制适应度曲线、训练集/测试集预测对比图。 绘制误差指标(MAE、RMSE、MAPE、MBE)柱状图。 十种智能优化算法分别是: GWO:灰狼算法 HBA:蜜獾算法 IAO:改进天鹰优化算法,改进①:Tent混沌映射种群初始化,改进②:自适应权重 MFO:飞蛾扑火算法 MPA:海洋捕食者算法 NGO:北方苍鹰算法 OOA:鱼鹰优化算法 RTH:红尾鹰算法 WOA:鲸鱼算法 ZOA:斑马算法
#include <REG51.h> // 8051 ????? #include <stdio.h> // ?? sprintf ??? #include <string.h> // ??????? // LCD1602 ????(?? Proteus ????) sbit LCD_RS = P3^4; // RS ?? sbit LCD_EN = P3^5; // EN ?? #define LCD_DATA_PORT P0 // ???? P0 // ??????(?? P1 ????) sbit KEY_UP = P1^0; // ???? sbit KEY_DOWN = P1^1; // ???? sbit KEY_ENTER = P1^2; // ???? sbit KEY_ESC = P1^3; // ESC ?? // ???? typedef struct { char *title; // ????? void (*handler)(); // ???????(???) } MenuItem; // ???? MenuItem menu[] = { {"Option 1", 0}, {"Option 2", 0}, {"Option 3", 0}, {"Exit", 0} }; #define MENU_LEN (sizeof(menu)/sizeof(menu[0])) // ???? unsigned char current_menu = 0; // ??????? // ????(????,???????) void delay_ms(unsigned int ms) { unsigned int i, j; for(i=0; i<ms; i++) for(j=0; j<120; j++); } // LCD ??? void lcd_write_cmd(unsigned char cmd) { LCD_RS = 0; // ?????? LCD_DATA_PORT = cmd; // ???? LCD_EN = 1; // ???? delay_ms(1); LCD_EN = 0; delay_ms(1); } // LCD ??? void lcd_write_data(unsigned char dat) { LCD_RS = 1; // ?????? LCD_DATA_PORT = dat; // ???? LCD_EN = 1; // ???? delay_ms(1); LCD_EN = 0; delay_ms(1); } // LCD ??? void lcd_init() { lcd_write_cmd(0x38); // 8 ???,2 ???,5x7 ?? lcd_write_cmd(0x0C); // ???,??? lcd_write_cmd(0x06); // ????,????? lcd_write_cmd(0x01); // ?? delay_ms(2); } // ?????(?????) void lcd_show_string(unsigned char row, unsigned char col, char *str) { if(row == 0) lcd_write_cmd(0x80 + col); // ??????? 0x80 else lcd_write_cmd(0xC0 + col); // ??????? 0xC0 while(*str) { lcd_write_data(*str); str++; } } // ????(????) unsigned char key_scan() { if(KEY_UP == 0) { delay_ms(20); // ?? if(KEY_UP == 0) { while(KEY_UP == 0); // ???? return 1; // ?? } } if(KEY_DOWN == 0) { delay_ms(20); if(KEY_DOWN == 0) { while(KEY_DOWN == 0); return 2; // ?? } } if(KEY_ENTER == 0) { delay_ms(20); if(KEY_ENTER == 0) { while(KEY_ENTER == 0); return 3; // ??? } } if(KEY_ESC == 0) { delay_ms(20); if(KEY_ESC == 0) { while(KEY_ESC == 0); return 4; // ESC ? } } return 0; // ??? } // ??????(????????) void update_menu_display() { unsigned char i; char line[17]; // LCD ???? 16 ?? lcd_write_cmd(0x01); // ?? for(i=0; i<MENU_LEN; i++) { if(i == current_menu) { // ????:? "=> " ?????? sprintf(line, "=> %s", menu[i].title); } else { sprintf(line, " %s", menu[i].title); } // ?????? 2 ????,?????? 2 ?(???????) if(i < 2) lcd_show_string(0, i*8, line); else lcd_show_string(1, (i-2)*8, line); } } // ??????? void menu_process() { unsigned char key; lcd_init(); // ??? LCD update_menu_display();// ?????? while(1) { key = key_scan(); // ???? if(key == 1) { // ?? if(current_menu > 0) current_menu--; update_menu_display(); } else if(key == 2) { // ?? if(current_menu < MENU_LEN-1) current_menu++; update_menu_display(); } else if(key == 3) { // ??? // ????:?????(????????) lcd_write_cmd(0x01); lcd_show_string(0, 0, "Selected:"); lcd_show_string(1, 0, menu[current_menu].title); delay_ms(1000); update_menu_display(); // ??????,???????? if(current_menu == MENU_LEN-1) { lcd_write_cmd(0x01); lcd_show_string(0, 0, "Exit Menu..."); delay_ms(1000); break; } } else if(key == 4) { // ESC ? lcd_write_cmd(0x01); lcd_show_string(0, 0, "Canceled"); delay_ms(1000); update_menu_display(); } } } void main() { menu_process(); // ?????? while(1); // ???????(???) }编译上述代码时,出现1 39 M:\vscode\zjdsb\彩条式程序菜单\ctscxcd.c [Error] REG51.h: No such file or directory,compilation terminated.
06-10
### 编译8051单片机C代码时解决 'REG51.h: No such file or directory' 错误的方法 在编译8051单片机C代码时,如果出现 `'REG51.h: No such file or directory'` 错误,通常是因为编译器无法找到 `REG51.h` 头文件。以下是解决问题的详细方法: #### 1. 确认头文件路径 确保 `REG51.h` 文件已存在于项目的目录中或指定的包含路径中。如果没有该文件,可以从以下来源获取: - 安装 SDCC 编译器时,通常会附带 `REG51.h` 文件。 - 如果使用的是 Keil C51 工具链,则需要手动将 Keil 的头文件转换为 SDCC 兼容格式[^3]。 #### 2. 设置正确的编译选项 在使用 SDCC 编译时,可以通过 `-I` 参数指定额外的头文件搜索路径。例如,假设 `REG51.h` 存在于 `/path/to/headers` 目录下,可以这样调用编译器: ```bash sdcc -I/path/to/headers your_file.c ``` #### 3. 检查头文件内容兼容性 由于 SDCC 和 Keil 的语法存在差异,可能需要对 `REG51.h` 进行修改以适应 SDCC 的要求。例如: - 替换 `sbit` 和 `sfr` 定义为 SDCC 支持的格式。 - 示例修改如下: ```c // Keil 格式 sbit P00 = P0^0; sfr P0 = 0x80; // 修改为 SDCC 格式 __sbit __at(0x80) P00; __sfr __at(0x80) P0; ``` #### 4. 使用 Makefile 自动化编译 如果项目较大,建议创建一个 Makefile 来管理编译过程。参考以下示例: ```makefile CC = sdcc CFLAGS = -I/path/to/headers all: $(CC) $(CFLAGS) your_file.c -o your_output.ihx packihx your_output.ihx > your_output.hex makebin -p your_output.hex > your_output.bin clean: rm -rf *.asm *.lst *.mem *.rst *.lnk *.rel *.sym *.ihx *.hex *.map *.lk ``` 此 Makefile 中使用了 `-I` 参数指定头文件路径,并通过 `packihx` 和 `makebin` 转换输出格式[^4]。 #### 5. 验证工具链安装完整性 确保 SDCC 工具链已正确安装并配置环境变量。可以通过以下命令验证: ```bash sdcc --version ``` 如果未正确安装,可以从官方站点下载最新版本并重新安装。 --- ### 示例代码 以下是一个简单的 8051 单片机程序,展示了如何包含 `REG51.h` 并正确编译: ```c #include <REG51.h> void delay(unsigned int ms) { unsigned int i, j; for (i = 0; i < ms; i++) { for (j = 0; j < 123; j++); } } void main() { P1 = 0xFF; // 将 P1 口设置为高电平 while (1) { P1_0 = ~P1_0; // 切换 P1.0 引脚状态 delay(500); // 延时 500ms } } ``` 编译命令: ```bash sdcc -I/path/to/headers your_file.c ``` ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值