Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何正确使用#include指令?_C++_C - Fatal编程技术网

C++ 如何正确使用#include指令?

C++ 如何正确使用#include指令?,c++,c,C++,C,是否有关于如何正确使用#include的材料? 我没有找到任何C/C++教科书详细解释这个用法。 在正式项目中,我总是在处理它时感到困惑。总是让我绊倒的大问题是: 这将在标题路径中搜索: #include <stdio.h> 对于每个标题,您应该做的第二件事是: myfilename.h: #ifndef MYFILENAME_H #define MYFILENAME_H //put code here #endif 这种模式意味着您不能在编译中重新定义标题(为orsogufo向

是否有关于如何正确使用
#include
的材料? 我没有找到任何C/C++教科书详细解释这个用法。
在正式项目中,我总是在处理它时感到困惑。

总是让我绊倒的大问题是:

这将在标题路径中搜索:

#include <stdio.h>
对于每个标题,您应该做的第二件事是:

myfilename.h:

#ifndef MYFILENAME_H
#define MYFILENAME_H
//put code here
#endif
这种模式意味着您不能在编译中重新定义标题(为orsogufo向我指出这被称为“include-guard”而干杯)。阅读一下C编译器是如何编译文件的(在链接之前),因为这会让“定义和包含”的世界对你来说非常有意义,C编译器在解析文本时不是很聪明。(然而,C编译器本身是另一回事)

  • 如果你有钱的话,请查看John Lakos的大型C++软件设计。
  • 谷歌C++编码指南也有一些不错的东西。
  • 查看萨特草药材料在线(博客)以及

基本上,您需要了解哪里不需要include头,例如转发声明。还要确保include文件可以逐个编译,并且只有在必须时才将#include放入h文件中(例如模板)。

头文件是C分离接口和实现的方式。它们分为两种类型:标准头文件和用户定义头文件。 标准头文件(如string.h)允许我们访问底层C库的功能。您应该将其包含在每个使用相关功能的.c文件中。通常使用括号,如#include 用户定义的头文件向其他程序员或C代码的其他部分公开了函数的实现。如果您已经实现了一个名为rational.c的模块,用于使用有理数进行计算,那么它的公共接口应该有一个相应的rational.h文件。每个使用该功能的文件都应该#包括rational.h,rational.c也应该#包括它。通常使用#include“rational.h”完成此操作 编译中包含的部分称为。它主要用于文本替换和粘贴文本。 Spence在防止重复的#includes的模式中是正确的,因为重复的#includes会弄乱名称空间。这是包含的基础,为您提供了更多的功能,同时也带来了更多的麻烦。

如果
yourfile.h
在当前工作目录中,您可以使用
\include“yourfile.h”
和<代码> >包含如果路径> >文件> h >代码>文件包含在C++包含目录中(配置中的某个地方,例如:代码> c:\MyLI\\YouFr.H./Cuff>,路径<代码> c:\MyLIB \/COD>必须指定为包含目录) 还可以包括.cpp和.hpp(h plus)。 有一组特定的文件可以像这样编写:
#include
。对于这个特定的示例工作,您需要使用namespace std指定


<> P>有一个非常好的软件,它与微软的Visual C++集成在一起,并显示了包含路径。p> 除了其他注释外,请记住,如果只有指针或引用,则不需要在另一个标题中包含标题。例如:

所需标题:

#include "Y.h"
class X
{
   Y y; // need header for Y
};
class Y; 
class X
{
   Y* y; // don't need header for Y
};
//#include "Y.h" in .cpp file
不需要标题:

#include "Y.h"
class X
{
   Y y; // need header for Y
};
class Y; 
class X
{
   Y* y; // don't need header for Y
};
//#include "Y.h" in .cpp file
第二个示例编译速度更快,依赖性更少。这在大型代码库中可能很重要。

请查看使用
\include
和<>代码>包含C++的C语言包含的。

< P>编辑:Andy Brice也以一种简明的方式提出了这一点。 在null的回答中,最重要的是要考虑将“包含”放在哪里

编写#include时,预处理器会将您在当前文件中列出的文件的内容包括在内,包括这些文件中的任何#include。这显然会导致编译时非常大的文件(COAD BULATE),因此需要仔细考虑是否需要包含一个γ。 在标准的代码文件布局中,您有一个包含类和函数声明的类的.h文件,然后是一个.cpp实现文件,您应该注意头文件中包含的#include的数量。这是因为,每次更改头文件时,也包括它的任何文件(即使用类的文件)都必须重新编译;如果标头本身有很多包含,那么使用该类的每个文件在编译时都会明显膨胀

最好尽可能使用前向声明,这样您就可以编写方法签名等,然后#在.cpp文件中包含相关文件,这样您就可以实际使用代码所依赖的类和结构

//In myclass.h
class UtilClass; //Forward declaration of UtilClass - avoids having to #include untilclass.h here

class MyClass
{
    MyClass();
    ~MyClass();

    void DoSomethingWithUtils(UtilClass *util); //This will compile due to forward declaration above
};

//Then in the .cpp
#include utilclass.h

void MyClass::DoSomethingWithUtils(UtilClass *util)
{
    util->DoSomething(); //This will compile, because the class definition is included locally in this .cpp file.
}

因此,编译器可能支持包含文件的两个唯一搜索路径:
我们可以非正式地称系统包含路径和用户包含路径。
#包含搜索系统包含路径。
#include“XX”搜索用户包含路径,然后搜索系统包含路径

检查标准草案n2521:
第16.2节:

2 A preprocessing directive of the form 

  # include < h-char-sequence> new-line 

  searches a sequence of implementation-defined places for a header identified
  uniquely by the specified sequence between the < and > delimiters, and
  causes the replacement of that directive by the entire contents of the
  header. How the places are specified or the header identified is
  implementation-defined. 

3 A preprocessing directive of the form 

  # include " q-char-sequence" new-line 

  causes the replacement of that directive by the entire contents of the
  source file identified by the specified sequence between the " " delimiters.
  The named source file is searched for in an implementation-defined manner.
  If this search is not supported, or if the search fails, the directive is
  reprocessed as if it read

  # include < h-char-sequence> new-line 

  with the identical contained sequence (including > characters, if any)
  from the original directive. 
2表单的预处理指令
#包括新行
在实施定义的位置序列中搜索已识别的标题
通过<和>分隔符之间的指定序列唯一地,以及
使指令的全部内容替换该指令
标题。如何指定位置或识别标题
实施定义。
3表单的预处理指令
#包括“q-char-sequence”新行
使指令的全部内容替换该指令
由“”分隔符之间的指定序列标识的源文件。
以实现定义的方式搜索指定的源文件。
如果不支持此搜索,或者
g++ -v -E -xc++ /dev/null -I LOOK_IN_HERE
#include "..." search starts here:
#include <...> search starts here:
  LOOK_IN_HERE
  /usr/include/c++/4.0.0
  /usr/include/c++/4.0.0/i686-apple-darwin9
  /usr/include/c++/4.0.0/backward
  /usr/local/include
  /usr/lib/gcc/i686-apple-darwin9/4.0.1/include
  /usr/include
  /System/Library/Frameworks (framework directory)
  /Library/Frameworks (framework directory)
End of search list.
#include "plop.h"
#include "plop-used-class.h"

/// C Header Files
#include <stdio.h>    // I know bad example but I drew a blank

/// C++ Header files
#include <vector>
#include <memory>
class Question;
class Answer;

class UniversityChallenge
{
...
    Answer AskQuestion( Question* );
...
};