什么时候.h不需要包含头文件? [英] When is .h not needed to include a header file?

查看:114
本文介绍了什么时候.h不需要包含头文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这样做:

  #include< iostream> 
using namespace std;

但此操作失败:

  #include< stdio> 

不需要 .h / p>

关于命名空间问题,我没有在 cstdio 中找到这样的逻辑:

  #pragma once 
#ifndef _CSTDIO_
#define _CSTDIO_
#include< yvals.h>

#ifdef _STD_USING
#undef _STD_USING
#include< stdio.h>
#define _STD_USING

#else / * _STD_USING * /
#include< stdio.h>
#endif / * _STD_USING * /

// undef常用宏覆盖
#undef clearerr
#undef feof
#undef ferror
#undef getc
#undef getchar
#undef putc
#undef putchar

#define _HAS_CONVENTIONAL_CLIB 1
#define _IOBASE _base
#define _IOPTR _ptr
#define _IOCNT _cnt

#ifndef _FPOSOFF
#define _FPOSOFF(fp)((long)(fp))
#endif / * _FPOSOFF *

typedef FILE _Filet;

#ifndef RC_INVOKED
#if _GLOBAL_USING
_STD_BEGIN
使用:: _ Filet;

使用:: size_t; using :: fpos_t; using :: FILE;
using :: clearerr; using :: fclose;使用:: feof;
using :: ferror;使用:: fflush; using :: fgetc;
using :: fgetpos; using :: fgets; using :: fopen;
using :: fprintf;使用:: fputc; using :: fputs;
using :: fread; using :: freopen;使用:: fscanf;
using :: fseek; using :: fsetpos;使用:: ftell;
using :: fwrite; using :: getc; using :: getchar;
using :: gets; using :: perror;
using :: putc;使用:: putchar;
using :: printf;使用:: puts; using :: remove;
using :: rename; using :: rewind; using :: scanf;
using :: setbuf; using :: setvbuf;使用:: sprintf;
using :: sscanf;使用:: tmpfile;使用:: tmpnam;
using :: ungetc; using :: vfprintf; using :: vprintf;
using :: vsprintf;


_STD_END
#endif / * _GLOBAL_USING * /
#endif / * RC_INVOKED * /

#endif / * _CSTDIO_ *


解决方案

标准,其中没有一个具有.h扩展名。 stdio.h 的C ++版本是:

  #include< cstdio>包含 stdio.h 

在C ++ std 命名空间中的名称,
,但是如果你愿意,你仍然可以使用C ++代码中的所有C标准头文件。



编辑:将名称放在cstdio的GCC版本中的std命名空间中的宏是:

  _GLIBCXX_BEGIN_NAMESPACE(std)

头文件通过尝试使用类似的方法做它应该做的:

  std :: printf(hello); 


This works:

#include <iostream>
using namespace std;

but this fails:

#include <stdio>

When is .h not needed?

About the namespace issue,I didn't find such logic in cstdio:

#pragma once
#ifndef _CSTDIO_
#define _CSTDIO_
#include <yvals.h>

#ifdef _STD_USING
 #undef _STD_USING
  #include <stdio.h>
 #define _STD_USING

#else /* _STD_USING */
 #include <stdio.h>
#endif /* _STD_USING */

// undef common macro overrides
 #undef clearerr
 #undef feof
 #undef ferror
 #undef getc
 #undef getchar
 #undef putc
 #undef putchar

 #define _HAS_CONVENTIONAL_CLIB 1
 #define _IOBASE    _base
 #define _IOPTR _ptr
 #define _IOCNT _cnt

#ifndef _FPOSOFF
  #define _FPOSOFF(fp)  ((long)(fp))
#endif /* _FPOSOFF */

typedef FILE _Filet;

#ifndef RC_INVOKED
 #if _GLOBAL_USING
_STD_BEGIN
using ::_Filet;

using ::size_t; using ::fpos_t; using ::FILE;
using ::clearerr; using ::fclose; using ::feof;
using ::ferror; using ::fflush; using ::fgetc;
using ::fgetpos; using ::fgets; using ::fopen;
using ::fprintf; using ::fputc; using ::fputs;
using ::fread; using ::freopen; using ::fscanf;
using ::fseek; using ::fsetpos; using ::ftell;
using ::fwrite; using ::getc; using ::getchar;
using ::gets; using ::perror;
using ::putc; using ::putchar;
using ::printf; using ::puts; using ::remove;
using ::rename; using ::rewind; using ::scanf;
using ::setbuf; using ::setvbuf; using ::sprintf;
using ::sscanf; using ::tmpfile; using ::tmpnam;
using ::ungetc; using ::vfprintf; using ::vprintf;
using ::vsprintf;


_STD_END
 #endif /* _GLOBAL_USING */
#endif /* RC_INVOKED */

#endif /* _CSTDIO_ */

解决方案

It's not needed for the header files defined by the C++ Standard, none of which have a .h extension. The C++ version of stdio.h is:

#include <cstdio>

which wraps stdio.h, placing the names in it in the C++ std namespace, but you can still use all the C Standard header files in C++ code, if you wish.

Edit: The macro that places the names in the std namespace in the GCC version of cstdio is:

_GLIBCXX_BEGIN_NAMESPACE(std)

You can check that your own header does what it should do by trying to use something like:

std::printf( "hello" );

in your code.

这篇关于什么时候.h不需要包含头文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆