GCC警告当相对导入文件多次在Include Path列表中

发布于 2025-01-31 03:51:19 字数 609 浏览 2 评论 0原文

如果标题文件名在包括路径列表中多次存在,那么我该如何使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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

软糯酥胸 2025-02-07 03:51:19
// File: alternative_1/my_include.h
int function() {
  return 1;
}

这是错误的。 YO不应将任何功能体和数据定义放入标题文件中。它们应仅包含类型的声明,定义,外部对象声明和静态内联函数。

并回答您的问题 - 仅编译器搜索包括路径,直到找到包含文件为止。然后他们停下来搜索其余目录。无法更改它。

// File: alternative_1/my_include.h
int function() {
  return 1;
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文