GCC警告当相对导入文件多次在Include Path列表中
如果标题文件名在包括路径列表中多次存在,那么我该如何使GCC警告我或出错,而不是选择默默进行操作?
考虑此示例:
// File: alternative_1/my_include.h
#define VAL 1
// File: alternative_2/my_include.h
#define VAL 2
// File: main.c
#include <stdio.h>
#include "my_include.h"
int main(void) {
printf("Using alternative_%d\n", VAL);
}
我不希望此示例默默编译并运行:
❯ gcc -Ialternative_2 -Ialternative_1 main.c -o main
❯ ./main
Using alternative_2
If a header filename exists multiple times in include path list, how can I make GCC warn me or error out, instead of choosing silently proceeding?
Consider this example:
// File: alternative_1/my_include.h
#define VAL 1
// File: alternative_2/my_include.h
#define VAL 2
// File: main.c
#include <stdio.h>
#include "my_include.h"
int main(void) {
printf("Using alternative_%d\n", VAL);
}
I do not want this to silently compile and run:
❯ gcc -Ialternative_2 -Ialternative_1 main.c -o main
❯ ./main
Using alternative_2
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是错误的。 YO不应将任何功能体和数据定义放入标题文件中。它们应仅包含类型的声明,定义,外部对象声明和静态内联函数。
并回答您的问题 - 仅编译器搜索包括路径,直到找到包含文件为止。然后他们停下来搜索其余目录。无法更改它。
It is wrong. Yuo should not put any function bodies and data definitions into header files. They should only contain types declarations, definitions, extern objects declarations and static inline functions.
And answering your question - compilers only search include paths until they found the include file. Then they stop to search the rest of the directories. There is no way of changing it.