开发者

Header Files in C/C++, Standard?

开发者 https://www.devze.com 2023-03-03 04:35 出处:网络
Are开发者_开发百科 header files standard or different in gcc vs other compilers?Its not really clear what you are asking, but the \"standard\" header files are only standard in the sense that they (sh

Are开发者_开发百科 header files standard or different in gcc vs other compilers?


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.


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.


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.


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.


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:

#ifdef __GNUC__
#include <pthread.h>
#else
#include <windows.h>
#endif

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消