C++两个cpp文件间怎么进行各自函数的调用
两个cpp文件间如何进行各自函数的调用
c++文件调用c++文件
1.前提
文件1:main.cpp
文件2:called.cpp
2.问题描述
想要在 main.cpp 代码中调用 called.cpp 文件中的函数,要怎么做?
3.解决方法
在网上搜寻了许多方法后,笔者才发现了一个行之有效且简单的解决方法。
只需要在main.cpp文件中添加两行代码即可。如下:
//main.cpp #include#include "call.cpp" //只需在头文件中添加该行代码,即可调用 using namespace std; int main() { called(); //进行调用 return 0; }
//call.cpp #includeusing namespace std; void called(){ cout<<"Hello world!"< C++分文件调用自己写的函数
1.创建.h的头文件
在头文件内编写:
#includeusing namespace std; int F_max(int a, int b); 2.创建.cpp的源文件
在源文件被编写:先要与头文件关联起来加入 ”xxx.h“
#include "FindMax.h" int F_max(int a, int b) { return a > b ? a : b; }3.在头文件写函数的声明
4.在源文件中写函数的定义(内容)
5.在任意文件调用
调用的时候只需要加上头文件就可以了




