包含链在C++中是如何起作用的
How does chain of includes function in C++?
- 在我的
first.cpp
中,我放置了#include second.h
因此,CCD_ 3看到了CCD_ 4的内容 - 在
second.cpp
中,我放置了#include third.h
我的问题是:first.cpp
会看到third.cpp
的内容吗?
添加
我认为,如果我包含second.h,我将能够使用second.h中声明的函数,并在second.cpp中描述(定义、编写)。从这个意义上讲,second.cpp的内容对first.cpp 来说是可用的
使用#include
时,该文件的实际文件内容显示为编译器输入的一部分。这意味着first.cpp
在编译器看来就像它里面有second.h
和third.h
。second.cpp
和third.cpp
中的源代码是单独的文件[1],需要单独编译,它们都在编译结束时的链接阶段组合在一起。
[1] 除非second.h
包含#include "second.cpp"
或类似的内容,否则这不是通常的方法,所以我将忽略该选项。
您可以将#include
看作一个简单的文本插入。但如果包含second.h
,则"看到"的是second.h
,而不是second.cpp
。
以为例
//second.h
#ifndef SECOND_INCLUDED
#define SECOND_INCLUDED
void foo();
#endif
//first.cpp
#include second.h
void bar()
{
foo();//ok
}
void no_good()
{
wibble();//not ok - can't see declaration from here
}
//third.h
#ifndef THIRD_INCLUDED
#define THIRD_INCLUDED
void wibble();
#endif
//second.cpp
#include second.h
#include third.h
void foo()
{
wibble();//ok in second.cpp since this includes third.h
}
包含头文件的cpp文件查看头文件中的内容,而不是包含头的其他源文件中的信息。
first.cpp将看到third.h的常量(您将能够在third.h中声明并在third.cpp中定义的first.cpp函数中使用)
#include<iostream>
using namespace std;
在你的测试中。cpp:
#include "test.h"
int main()
{
cout << "Hello world!" << endl;
}
如您所见,在<iostream>
中声明的cout
在test.cpp中可见。
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 带有指定长度字符* 参数的 std::regex_search 在 VS2017 中不起作用?
- Bjarne Stroustrup Book - std_lib_facilities.h - 不起作用(未知类型名称)
- 为什么简单的算术减法在"if"条件下不起作用?
- 为什么Stroustup书中的has_f不起作用
- 你能检查一下为什么在这个代码中从链接列表中删除项目不起作用吗
- 嵌套While循环不起作用(C++问题)
- C++Matching Brackets 2解决方案不起作用
- 为什么这段代码不起作用,我该如何解决?
- 我正在开发服务器,ip作为参数传递不起作用