为什么主函数可以调用并运行其他CPP文件的功能
why can main function call and run the function of other cpp file?
i创建一个C 项目test
,其中包含以下三个文件:你好。hpp:
#ifndef HELLO_H
#define HELLO_H
int cHelloSay();
#endif
hello.cpp:
#include "hello.hpp"
#include<iostream>
int sayHello(){
std::cout << "123";
}
int i=sayHello();
和main.cpp:
#include "hello.hpp"
int main(int argc, char** argv) {
return 0;
}
然后我编译了此项目,然后输出:123
。所以我很困惑为什么int i=sayHello();
行执行,尽管main()
不称呼它?
现在,我知道在函数之外声明的任何变量都是全局变量,尽管它在另一个 *.cpp文件中。因此,在致电main()
。
int i=sayHello();
行在遵守程序后,变量被初始化。作为其中的一部分,它在称为int main()之前调用您的 int i=sayHello();
。
希望这会有所帮助!
相关文章:
- 当我在其中一个线程执行中(在activemq-cpp中)捕获到特定值时,我如何终止/停止所有其他线程
- 我应该将除 .cpp 以外的其他文件添加到 git 中吗?
- 如何制作 cmakelists.txt编译使用在其他地方声明和实现的函数和类的 CPP
- 访问从 CPP 文件到其他头文件的静态变量
- 如何将 cpp 文件中的静态函数公开给其他文件
- 如何在其他 cpp 文件中使用嵌套Q_ENUM,而不是在 qml 中?
- 如何在 CMakeList 中包含 h/hpp 头文件.txt而不是 cpp 或其他 hpp 文件
- CPP如何使主线程等到其他线程在周期中完成
- 仅一个功能的链接错误.在同一类和CPP文件中具有相似签名的其他函数工作正常
- 如何防止在标题中包含标头,以最大程度地减少包含在其他 CPP 中
- 如何使用QT或Linux上的任何其他CPP框架播放WMA音频文件
- 是否有其他快速有效地运行这样的方法?CPP
- 为什么主函数可以调用并运行其他CPP文件的功能
- 不完整的参数错误,包括其他.cpp文件中的postgres.cpp工作文件
- 用Cpp或其他快速语言反转大型哈希表
- 如何通过其他.cpp文件中的类变量编辑公共成员变量
- main.cpp 无法访问头文件和其他.cpp文件中的变量和函数
- CPP,如果其他报表发布
- 如何通过CPP判断其他程序的结果?
- 使动态初始化的变量可用于其他.cpp文件