我可以制作一个名为 "stdio.h" 的自定义头文件吗?

Can I make a custom header file named "stdio.h"?

我可以制作一个名为 stdio.h 的自定义头文件并可以在我的程序中与 <stdio.h> 一起使用吗?

我创建了一个名为 stdio.h 的文件,然后 main.c 将此内容添加到 main.c 以测试它是否正常工作。

#include "stdio.h"
#include <stdio.h>

int main()
{
    testArea(); // this will use "stdio.h"
    int test = 2;
    printf("%d", test); // this will use <stdio.h>
    return 0;
}

程序输出:

1002 (100 from "stdio.h" and 2 from <stdio.h>)

我的stdio.h的内容:

#include <stdio.h>

void testArea()
{
    int abc = 100;
    printf("%d", abc);
}

惊喜,确实如此!现在,您应该知道使用“”意味着通过文件路径搜索头文件。同时<>主要看编译器的includes文件夹。

所以,你可以做到。

PS:如果你要投反对票,请做出解释,以便我也能了解问题所在。它对我来说很好。

编辑:现在,您可以看到程序知道调用什么和做什么。

奖励:如果您尝试添加文件中存在的同名函数,编译器会报错。

#include <stdio.h>

void testArea()
{
    int abc = 100;
    printf("%d", abc);
}

void printf()
{

}

将以returnstdio.h:9:7: error: conflicting types for ‘printf’为例。你只是应该避免使用现有的函数,但它不限于同名头文件。你不能用任何文件名来做,因为名称会发生​​冲突。

同样,这个用法很好。应该完全没有问题。

编辑 2:我正在使用 Linux Mint 18.2

这是我的gcc --version

gcc (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

而且,这是我编译代码的方式:

duman@duman-pc ~/Desktop/Nuclear Test Zone $ gcc main.c -o test
duman@duman-pc ~/Desktop/Nuclear Test Zone $ ./test

没什么特别的,而且我真的不知道什么会导致未定义的行为,因为我的编译器不允许使用同名函数。

编辑 3:只是为了看看是否与名称使用相关的任何内容都会引发警告,我使用所有这些标志编译了相同的代码: 并且关于名称有 none。

我已经尽力了。希望对你有帮助。

C11 7.1.2p3:

If a file with the same name as one of the above < and > delimited sequences, not provided as part of the implementation, is placed in any of the standard places that are searched for included source files, the behavior is undefined.

标准位则指6.10.2p2:

searches a sequence of implementation-defined places for a header identified uniquely by the specified sequence between the < and > delimiters, and causes the replacement of that directive by the entire contents of the header. How the places are specified or the header identified is implementation-defined.

所以这里给出的唯一保证是 如果 你的 stdio.h 不在 implementation-defined 的地方搜索 header,则行为不会未定义。然后您可以将其包含在

#include "stdio.h"

但是,如果您真的希望该文件包含在

#include <stdio.h>

然后为了让编译器找到它,您需要将它放在任何标准位置,并且所有赌注都关闭。


但是,在独立的(即非托管的)执行环境中。 stdio.h 可能不是标准的 header 名称,所以它也可能完全没问题。并不是说独立执行环境有任何可移植性。

因此,除非您更具体地指定您要做什么,否则我们只能参考 C 标准并耸耸肩。有一个名为 stdio.h 的源文件并不严格符合,但它很可能符合,所以 YMMV.

如果您在文件目录中指定自定义 "stdio.h" 的来源(即执行

#include "C:/ProgrammingC/stdio.h"

可能没问题,但是

#include "stdio.h" //This only selects the standard include
                   //if there's no other stdio.h in the build directory

有风险,

#include <stdio.h>

绝对不是你想要的

Antti Haapala 所述,命名文件 stdio.h 并将其放在编译器查找的任何目录中被明确描述为未定义行为包含文件。

然而,默认情况下,编译器不会在源文件目录中搜索标准 headers,但是 -I. 的命令行参数可以轻松改变这种行为。

如果没有这个选项并且假设您没有将源文件放在编译器的系统目录中,您可以使用名称 stdio.h 作为包含文件并将其包含在 #include "stdio.h" 中而不会干扰#include <stdio.h> 中提到的标准 header(甚至可能根本不是文件)。

您可以通过将源文件本身命名为 stdio.h...

进一步 confusion-land

我真的想混淆reader,将源文件命名为a.out并用gcc -o stdio.h -x c a.out编译。