C琐碎知识点
C程序编译过程
echo $? 输出返回值
xxd 用于二进制或16进制显示文件内容
set nowrap 取消自动换行
file a.out 查看文件类型
man gcc gcc --help tldr gcc 社区维护
What is tldr-pages?
The tldr-pages project is a collection of community-maintained help pages for command-line tools, that aims to be a simpler, more approachable complement to traditional man pages.
控制gcc行为的三个选项 -E -S -c
gcc -S a.c 生成 a.s 是汇编器的输入
objdump -d a.o 查看二进制文件的反汇编
vim 在一个文件中编辑另一个文件 :e b.c
gcc -c a.c --verbose a. 啰嗦的
gcc -m32 a.c 32位
gcc a.c -I . --verbose 将当前目录(头文件所在目录)添加进去
#include 赋值粘贴
宏定义、宏展开 (变量、函数)
movl addl 从左边移动到右边 从左边加到右边
gcc a.o b.o -static 静态链接库文件
long 64位 int 32位 64位的程序,地址占8个字节
make 做菜需要什么原材料,原材料可能还需要其它原材料 能实现增量编译
indent 缩进
枚举和宏非常类似,宏在预处理阶段将名字替换成对应的值,枚举在编译阶段将名字替换成对应的值。我们可以将枚举理解为编译阶段的宏
读内存,也就是加载操作(load),从内存读到寄存器;写内存,就是存储操作(store),从寄存器写入到内存。
Makefile 从理解简单项目开始,yemu
CFLAGS 编译选项
gcc -Wall -Werror -Wall [enables all major warnings.] -Werror [make all warnings into errors, causing compilations to fail if any warnings are reported.]
C 库函数 int rand(void) 返回一个范围在 0 到 RAND_MAX 之间的伪随机数。RAND_MAX 是一个常量,它的默认值在不同的实现中会有所不同,但是值至少是 32767。
indent - changes the appearance of a C program by inserting or deleting whitespace.
vim中多行缩进:可使用ctrl+v进入visual模式,然后用光标移动或者上下键方法选中要缩进的多行代码,shift+‘>’ 向左缩进,‘<’向右缩进
如果一个待编译文件所依赖的文件的时间戳都没有自身文件新的话,make就不需要去重新编译。
yemu.h
#include <stdint.h> typedef uint8_t u8; #define NMEM 16 enum { RA, R1, R2, R3, PC, NREG }; extern u8 M[NMEM], R[NREG]; #define pc (R[PC]) void idex();
yemu.c
#include <stdio.h> #include "yemu.h" int main() { while (1) { if (M[pc] == 0) { printf("Hit GOOD trap @ pc = %d.\n", pc); for (int i = 0; i < NMEM; i++) { printf("M[%02d] = 0x%02x (%d)\n", i, M[i], M[i]); } break; } idex(); } return 0; } u8 R[NREG], M[NMEM] = { 0b11100111, 0b00000100, 0b11100110, 0b00010001, 0b11111000, 0b00000000, 0b00010000, 0b00100001, 0b00000000, };
idex.c
#include <assert.h> #include <stdio.h> #include "yemu.h" typedef union inst { struct { u8 rs : 2, rt: 2, op: 4; } rtype; struct { u8 addr: 4, op: 4; } mtype; } inst_t; #define RTYPE(i) u8 rt = (i)->rtype.rt, rs = (i)->rtype.rs; #define MTYPE(i) u8 addr = (i)->mtype.addr; void idex() { inst_t *cur = (inst_t *)&M[pc]; switch (cur->rtype.op) { case 0b0000: { RTYPE(cur); R[rt] = R[rs]; pc++; break; } case 0b0001: { RTYPE(cur); R[rt] += R[rs]; pc++; break; } case 0b1110: { MTYPE(cur); R[RA] = M[addr]; pc++; break; } case 0b1111: { MTYPE(cur); M[addr] = R[RA]; pc++; break; } default: assert(0); } }
test.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "yemu.h" u8 R[NREG], M[NMEM], oldR[NREG], oldM[NMEM], *newM = M, *newR = R; void random_rm() { for (int i = 0; i < NREG; i++) R[i] = rand() & 0xff; for (int i = 0; i < NMEM; i++) M[i] = rand() & 0xff; } #define ASSERT_EQ(a, b) ((u8)(a) == (u8)(b)) #define TESTCASES(_) \ _(1, 0b11100111, random_rm, ASSERT_EQ(newR[0], oldM[7]) ) \ _(2, 0b00000100, random_rm, ASSERT_EQ(newR[1], oldR[0]) ) \ _(3, 0b11100101, random_rm, ASSERT_EQ(newR[0], oldM[5]) ) \ _(4, 0b00010001, random_rm, ASSERT_EQ(newR[0], oldR[0] + oldR[1]) ) \ _(5, 0b11110111, random_rm, ASSERT_EQ(newM[7], oldR[0]) ) #define MAKETEST(id, inst, precond, postcond) { \ precond(); \ memcpy(oldM, M, NMEM); \ memcpy(oldR, R, NREG); \ pc = 0; M[pc] = inst; \ idex(); \ printf("Test #%d (%s): %s\n", \ id, #inst, (postcond) ? "PASS" : "FAIL"); \ } int main() { TESTCASES(MAKETEST) return 0; }
Makefile
.PHONY: run clean test CFLAGS = -Wall -Werror -std=c11 -O2 CC = gcc LD = gcc yemu: yemu.o idex.o $(LD) $(LDFLAGS) -o yemu yemu.o idex.o yemu.o: yemu.c yemu.h $(CC) $(CFLAGS) -c -o yemu.o yemu.c idex.o: idex.c yemu.h $(CC) $(CFLAGS) -c -o idex.o idex.c run: yemu @./yemu clean: rm -f test yemu *.o test: yemu $(CC) $(CFLAGS) -o test idex.o test.c && ./test
参考资料: https://staight.github.io/2018/10/04/gcc%E7%BC%96%E8%AF%91%E8%BF%87%E7%A8%8B/
https://ivanzz1001.github.io/records/post/cplusplus/2018/08/03/cpluscplus-gcc-compile
https://www.bilibili.com/video/BV1qa4y1j7xk?p=2
2022/4/6
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?