使用#include 无限编译循环
Infinite compilation loop with #include
如果我有如下两个文件
//file1.h
#include "file2.h"
//file2.h
#include "file1.h"
这种循环依赖可以发生在两个以上的文件中,为了简单起见,我只列出了两个。在这种情况下会发生什么?
我也很想知道 C++ 标准是否限制这种情况的发生。
标准不限制这种递归。避免它的常见做法是使用
#ifndef FILE_H
#define FILE_H
// content of the header file
#endif
#pragma once
// content of the header file
请注意 #pragma one
,尽管许多编译器都支持它,但它不是标准的一部分:
#pragma once
is a non-standard but widely supported preprocessor directive
如果我有如下两个文件
//file1.h
#include "file2.h"
//file2.h
#include "file1.h"
这种循环依赖可以发生在两个以上的文件中,为了简单起见,我只列出了两个。在这种情况下会发生什么? 我也很想知道 C++ 标准是否限制这种情况的发生。
标准不限制这种递归。避免它的常见做法是使用
#ifndef FILE_H
#define FILE_H
// content of the header file
#endif
#pragma once
// content of the header file
请注意 #pragma one
,尽管许多编译器都支持它,但它不是标准的一部分:
#pragma once
is a non-standard but widely supported preprocessor directive