C++ 什么是 .h.gch 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1241399/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:17:37  来源:igfitidea点击:

What is a .h.gch file?

c++g++

提问by mnuzzo

I recently had a class project where I had to make a program with G++.

我最近有一个课堂项目,我必须用 G++ 制作一个程序。

I used a makefile and for some reason it occasionally left a .h.gch file behind.

我使用了一个 makefile,出于某种原因,它偶尔会留下一个 .h.gch 文件。

Sometimes, this didn't affect the compilation, but every so often it would result in the compiler issuing an error for an issue which had been fixed or which did not make sense.

有时,这不会影响编译,但时常会导致编译器针对已修复或没有意义的问题发出错误。

I have two questions:

我有两个问题:

1) What is a .h.gch file and what is one used for? and

1) 什么是 .h.gch 文件,它的用途是什么?和

2) Why would it cause such problems when it wasn't cleaned up?

2)为什么不清理会导致这样的问题?

采纳答案by Dunewalker

A .gchfile is a precompiled header.

.gch文件是一个预编译的头。

If a .gchis not found then the normal header files will be used.

如果.gch未找到,则将使用正常的头文件。

However, if your project is set to generate pre-compiled headers it will make them if they don't exist and use them in the next build.

但是,如果您的项目设置为生成预编译的头文件,它会在不存在的情况下生成它们并在下一次构建中使用它们。

Sometimes the *.h.gchwill get corrupted or contain outdated information, so deleting that file and compiling it again should fix it.

有时*.h.gch会损坏或包含过时的信息,因此删除该文件并再次编译它应该可以修复它。

回答by riteshkasat

If you want to know about a file, simply type on terminal

如果你想了解一个文件,只需在终端上输入

file filename

file a.h.gchgives:

file a.h.gch给出:

GCC precompiled header (version 013) for C

回答by Goz

Its a GCC precompiled header.

它是一个 GCC 预编译头文件。

Wikipedia has a half decent explanation, http://en.wikipedia.org/wiki/Precompiled_header

维基百科有一个不错的解释,http://en.wikipedia.org/wiki/Precompiled_header

回答by Oleksandr Tymoshenko

a) They're precompiled headers: http://gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html

a)它们是预编译的头文件:http: //gcc.gnu.org/onlinedocs/gcc/Precompiled-Headers.html

b) They contain "cached" information from .h files and should be updated every time you change respective .h file. If it doesn't happen - you have wrong dependencies set in your project

b) 它们包含来自 .h 文件的“缓存”信息,每次更改相应的 .h 文件时都应更新。如果没有发生 - 您在项目中设置了错误的依赖项

回答by tgibson

Other answers are completely accurate with regard to what a gch file is. However, context (in this case, a beginner using g++) is everything. In this context, there are two rules:

关于 gch 文件是什么,其他答案是完全准确的。然而,上下文(在这种情况下,使用 g++ 的初学者)就是一切。在这种情况下,有两个规则:

  1. Never, ever, ever put a .h file on a g++ compile line. Only .cpp files. If a .h file is ever compiled accidentally, remove any *.gch files

  2. Never, ever, ever put a .cpp file in an #include statement.

  1. 永远,永远,永远不要将 .h 文件放在 g++ 编译行上。只有 .cpp 文件。如果 .h 文件被意外编译,请删除所有 *.gch 文件

  2. 永远不要将 .cpp 文件放在 #include 语句中。

If rule one is broken, at some point the problem described in the question will occur. If rule two is broken, at some point the linker will complain about multiply-defined symbols.

如果规则一被打破,在某个时刻就会出现问题中描述的问题。如果规则二被破坏,在某些时候链接器会抱怨多重定义的符号。