为什么在多个cpp文件中包含相同的头文件,然后他们的编译工作? [英] Why including same headers in multiple cpp files and then their compilation works?
问题描述
例如,我有2个cpp文件:f1.cpp和f2.cpp,以及一个头文件:xxx.h。
For example I have 2 cpp files: f1.cpp and f2.cpp, and also a header file: xxx.h.
f1.cpp有以下源代码:
f1.cpp has the following source code:
#include <iostream>
#include "xxx.h"
using namespace std;
int main ()
{
rect rplace;
polar pplace;
cout<<"Enter the x and y values: ";
while (cin>>rplace.x>>rplace.y)
{
pplace=rect_to_polar(rplace);
show_polar(pplace);
cout<<"Next two numbers (q to quit): ";
}
cout<<"Done.\n";
return 0;
}
f2.cpp源代码:
f2.cpp source code:
#include <iostream>
#include <cmath>
#include "xxx.h"
polar rect_to_polar (rect xypos)
{
using namespace std;
polar answer;
answer.distance=sqrt(xypos.x*xypos.x+xypos.y*xypos.y);
answer.angle=atan2(xypos.y, xypos.x);
return answer;
}
void show_polar (polar dapos)
{
using namespace std;
const double Rad_to_deg=57.29577951;
cout<<"distance="<<dapos.distance;
cout<<", angle= "<<dapos.angle*Rad_to_deg;
cout<<"degrees\n";
}
和xxx.h:
struct polar
{
double distance;
double angle;
};
struct rect
{
double x;
double y;
};
polar rect_to_polar (rect xypos);
void show_polar(polar dapos);
我认为应该有一个编译器错误,因为头 xxx.h
和 iostream
包含两次:一次在f1.cpp中,一次在f2.cpp中。但是一切都编译好,所以我不明白它是如何工作的。
I thought that there should be a compiler error because the headers xxx.h
and iostream
are included two times: once in f1.cpp and once in f2.cpp. But everything was compiled, so I don't understand how it can work.
推荐答案
预处理器只读头文件和放置它们转换为 #include
指令所在的翻译单元。如果一个源文件中包含一个头文件,则只有该文件知道该头文件中的声明。
The preprocessor simply reads the header files and puts them into the translation units where the #include
directive is. If one header file is included in one source file, only that file knows about the declarations in that header file.
您还应该知道声明和定义。当你声明某些东西时,你只是告诉编译器这东西存在,是类型这个,那个。当你定义时,你告诉编译器这是我之前声明的。
You also should know the difference between declarations and definitions. When you declare something, you just tells the compiler that "this thing exists and is of type this-and-that". When you define something, you tell the compiler "this is the thing I declared before".
你可以有多个声明。因为它们仅作为编译器的元数据,并且不在其转换单元之外使用。然而,你可以只有一个定义的东西。这就是为什么你不能在包含在多个源文件的头文件中定义全局变量/函数。
You can have multiple declarations of the same thing. because they only act as meta-data for the compiler, and are not used outside its translation unit. You can however only have one definition of something. That's why you can't define global variables/functions in header files included in multiple source files.
另外,在这个答案中,我谈到源文件文件和翻译单位。头文件是您 #include
的文件。源文件是包含(可以说)的文件。翻译单元是完整的预处理源,包含源和所有包含的头文件,并传递给实际的编译器。
Also, In this answer I talk about "source files", "header files" and "translation units". Header files are the files you #include
. Source files is the files that does the including (so to speak). And a translation unit is the complete preprocessed source, complete with the source and all included header files, and which is passed on to the actual compiler.
这篇关于为什么在多个cpp文件中包含相同的头文件,然后他们的编译工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!