我完全按照它在官方文档here中的说法,我在Google上找不到任何关于预编译头不工作的结果。我用gcc main.c -lSDL2 -lSDL2_image
编译,我用gcc <headername>
编译了头文件。我使用-H来检查PCH是否被使用,我还使用了大约45个头,当不使用头时,编译时间有明显的差异。
范例:
// main.c
#include <stdio.h>
#include "header.h"
int main() {
printf("%s\n", string);
return 0;
}
// header.h
char* string = "Hello World!";
使用gcc -H main.c
编译后,它给出了与使用gcc header.h
输出编译头相同的输出:
. /usr/include/stdio.h
.. /usr/include/bits/libc-header-start.h
... /usr/include/features.h
.... /usr/include/features-time64.h
..... /usr/include/bits/wordsize.h
..... /usr/include/bits/timesize.h
...... /usr/include/bits/wordsize.h
.... /usr/include/sys/cdefs.h
..... /usr/include/bits/wordsize.h
..... /usr/include/bits/long-double.h
.... /usr/include/gnu/stubs.h
..... /usr/include/gnu/stubs-64.h
.. /usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/include/stddef.h
.. /usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/include/stdarg.h
.. /usr/include/bits/types.h
... /usr/include/bits/wordsize.h
... /usr/include/bits/timesize.h
.... /usr/include/bits/wordsize.h
... /usr/include/bits/typesizes.h
... /usr/include/bits/time64.h
.. /usr/include/bits/types/__fpos_t.h
... /usr/include/bits/types/__mbstate_t.h
.. /usr/include/bits/types/__fpos64_t.h
.. /usr/include/bits/types/__FILE.h
.. /usr/include/bits/types/FILE.h
.. /usr/include/bits/types/struct_FILE.h
.. /usr/include/bits/types/cookie_io_functions_t.h
.. /usr/include/bits/stdio_lim.h
.. /usr/include/bits/floatn.h
... /usr/include/bits/floatn-common.h
.... /usr/include/bits/long-double.h
. header.h
Multiple include guards may be useful for:
/usr/include/bits/libc-header-start.h
/usr/include/bits/time64.h
/usr/include/bits/typesizes.h
/usr/include/features-time64.h
/usr/include/gnu/stubs-64.h
/usr/include/gnu/stubs.h
/usr/lib/gcc/x86_64-pc-linux-gnu/13.2.1/include/stddef.h
header.h
1条答案
按热度按时间tpxzln5u1#
正如医生所说
一旦看到第一个C标记,就不能使用预编译的头。
但是,通过在
"header.h"
之前包含<stdio.h>
,<stdio.h>
包含了大量的C标记,因此不会使用预编译的头文件。尝试在
<stdio.h
之前包含"header.h"
。或者更好的做法是,让"header.h"
包含<stdio.h>
,这样就可以获得预编译stdio.h
的好处。还要注意你的“header.h”包含定义,而不仅仅是声明,这可能会导致你在更大的项目中出现问题。