C/C++ 中的头文件,标准?
gcc 与其他编译器的头文件是标准的还是不同的?
Are header files standard or different in gcc vs other compilers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
gcc 与其他编译器的头文件是标准的还是不同的?
Are header files standard or different in gcc vs other compilers?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(5)
它不太清楚你在问什么,但“标准”头文件只是标准,因为它们(应该)满足C/C++标准(由管理机构指定,例如ANSI等)。
不同的编译器经常通过不同的实现来满足这些标准,至少在标准允许的情况下。
换句话说,您应该只依赖标准中指定的行为,因为具体实现可能会略有不同。
Its not really clear what you are asking, but the "standard" header files are only standard in the sense that they (should) meet the C/C++ standard (as specified by the governing body, e.g. ANSI, etc.)
Different compilers often meet these standards through different implementations, at least when the standard allows them to do so.
In other words, you should only rely on the behavior that is specified in the standard, as specific implementations may vary slightly.
标准头文件被称为 so,因为它们被定义为 ANSI C/C++ 标准的一部分,因此,它们对于所有符合 ANSI 的编译器都是相同的。
Standard header files are called so, because they are defined as a part of ANSI C/C++ standard, an so, they will be the same for all compilers, that are ANSI-compliant.
我希望我能理解你的问题,但这就是我的想法。
与 .cpp 文件一起创建类的头文件 (.h) 是您在 C++ 中执行操作的方式。
对于大多数情况,SomeClass.h 将生成类的原型,而 SomeClass.cpp 将包含该类工作所需的代码。
如果由于某种原因 GCC 对编译做了一些非常不同的事情,那么我不知道。我认为对于任何编译器来说都是同样的想法。
I hope I understand your question but here's my go.
Header files (.h) that go along with a .cpp file to create a class are how you do things in C++.
For most cases, a SomeClass.h will prototype the class, and the SomeClass.cpp will contain the code necessary for the class to work.
If for some reason GCC does something very different for compilation, then I have no idea. I assume it's the same idea for any compiler.
标准未指定标准库所需的标准头文件之外的头文件概念。但使用#include 来指定要导入的文件是。这是标准的,也是编译器搜索这些文件的一般顺序。只要预处理器的行为被明确定义,用于避免多重包含的
#ifndef BLAH
方法也是由标准强制执行的(尽管正如我所说,标准对于您是否使用他们根本)。不过,#pragma Once
不是标准的,因此使用它需要您自担风险。The concept of header files, outside of the standard ones required for the standard library, is not specified by the standard. But using #include to specify files to import is. So that is standard, as well as the general order the compiler searches for those files. And the
#ifndef BLAH
method for avoiding multiple inclusion is also mandated by the standard, insofar as the behavior of the preprocessor is well defined (although as I said, the standard is silent on whether or not you use them at all).#pragma once
is not standard, though, so use it at your own risk.您可能会发现不同编译器套件之间存在细微差别。但更重要的是,您会发现跨不同平台的各种库和头文件。例如,GCC 经常出现在 POSIX 系统上,因此只要预定义了
__GNUC__
,就很容易找到
。这会导致如下的假设代码:如果有疑问,请选择 C++ 标准库 使用 C++ 时以及使用 C 时的 C 标准库 时。(但仍会遇到一些小问题不一致主要是由不同的编译器版本引起的。)
此外,测试您的代码是否在不同的系统上构建和运行。如果它可以在 Windows 下使用 Visual Studio 并在 Linux 下使用 GCC 运行,那么您可以放心,将代码移植到其他系统将是直接的。
You may find minor variations between different compiler suites. But more significantly, you will find a variety of libraries and header files across different platforms. For instance, GCC is often found on POSIX systems, so it's quite common to find, say,
<pthread.h>
, whenever__GNUC__
is pre-defined. This leads to assumptive code like the following:If in doubt, favor the C++ Standard Library when using C++ and the C Standard Library when using C. (But continue to expect a few niggling inconsistencies caused mostly by different compiler versions.)
Also, test that your code builds and runs on different systems. If it works using Visual Studio under Windows and GCC under Linux, you can be somewhat assured that porting your code to other systems will be straight-forward.