#包含stdio混淆是每个头文件所需要的
#include stdio confusion is it needed for each header file?
我知道我对#include的理解或它的编译方式是不正确的,否则我的代码就会工作。我很困惑为什么我的代码需要在两个位置包含#include才能正确编译和运行。
我的主cpp文件armperfmon.cpp:
#include "armperfmon.h"
int main(int argc, char* argv[])
{
FILE* pOutFile = NULL;
PrintCounterOptions(pOutFile);
}
主头文件armperfmon.h:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "counters.h"
void PrintCounterOptions(FILE* pFile);
第二个包含函数counter.cpp:的cpp
void PrintCounterOptions(FILE* pFile)
{
fprintf("print some stuff");
}
函数计数器的第二个头文件.h
void PrintCounterOptions(FILE* pFile);
错误:
counters.cpp: error: 'FILE' was not declared in this scope
counters.cpp: error: 'pFile' was not declared in this scope
如果我在函数cpp文件中输入#include <stdio.h>
,那么错误就会消失,函数会按预期编译/执行。我假设在主.h文件中,当它包含<stdio.h>
时,它将可用于后续的file*定义,特别是因为它包含在counters.h之前。当我输入这个时,我也意识到更正确的include是<cstdio>
。如果有人能澄清我的思维过程中出了什么问题,我将不胜感激。
很难准确回答这个问题,因为你已经去掉了所有特定的细节,比如文件名,但简而言之,C++源文件在结果链接在一起之前是独立编译的,在编译"第二个cpp"时根本看不到"主头":它只是"主cpp文件"的头。实际上,头文件的全部目的是作为声明的公共位置,然后将声明#include
d转换为多个转换单元,这就是您需要在这里通过向"第二个头文件"添加必要的代码来实现的。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 文本文件中的单词链表
- CMake-按正确顺序将项目与C运行时对象文件链接
- 使用新行和不使用新行读取文件
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 挂起和取消挂起一个文件DLL
- 如何确定我已使用非编码文件到达 EOF?
- 命名空间中具有.h和.cpp文件的类
- 如何使用ndk-build.cmd构建Android.so文件
- 从包含m行的文件中提取n行,必要时(惰性地)重复该文件
- 使用 stdio.h 重新定位和重命名文件C++
- 对应于 stdio.h 的库文件是动态链接还是静态链接
- 我想在Windows上使用Clang从C++编译64位代码-找不到stdio.h文件
- #包含stdio混淆是每个头文件所需要的
- #在stdio.h等C标准头文件中定义__CLIBNS::std::
- 使用 stdio 重定向 Unix 和C++上的输入和输出文件
- 从stdio文件构造流
- 如何在C/C++中使用stdio文件操作时检测磁盘空间不足
- c++ (VS2012) stdio:不能读取一个字节的文件,如果字节是09h