C语言的一些不常用的编译知识

发布时间 2023-07-23 17:17:29作者: 松雅湖~阿辉

1.__stringify

Linux内核中有如下两个宏:
#define __stringify_1(x...) #x
#define __stringify(x...) __stringify_1(x)

作用:会将x替换成()里面的内容。

2.GCC __attribute__ 使用

__attribute__((used)):

表示对于这个函数可能不会调用它、可能用不到它,编译器不用进行 warning 提示。
而在嵌入式中 中断函数都是由内部的中断处理机制通过中断向量做跳转调用的,不是开发人员 “显式” 去调用的,因此在一些规则检查比较严格的编译器上编译时,就会出现类似于上面的警告,为了视野干净我们就添加这个属性。向编译器说明这段代码有用,即使在没有用到的情况下编译器也不会警告!告诉编译器避免被链接器因为未用过而被优化掉。

3.##的用法

#define CONCAT(s1, s2) s1##s2。int n = CONCAT(123,456);   结果就是n=123456。

4.#define XXX (...)宏定义

 

#include <stdio.h>
using namespace std;
#define LOG(...) printf(__VA_ARGS__)
#define TADD(...) add(__VA_ARGS__)
int add (int a, int b) {
return a + b;
}
int add (int a, int b, int c) {
return a + b + c;
}
int main() {
// test1
LOG("This is a test %d, %d, %d \n", 1, 2, 3);
printf("result1 is %d\n", TADD(1,2));
printf("result1 is %d\n", TADD(1,2,3));
return 0;
}

This is a test 1, 2, 3
result1 is 3
result1 is 6