vscode调用*.cpp子文件和*.h头文件
时间: 2023-11-14 22:24:35 浏览: 159
在 VS Code 中调用 C++ 的 *.cpp 子文件和 *.h 头文件,需要遵循以下步骤:
1. 创建一个文件夹来存储您的 C++ 代码。
2. 在该文件夹中创建一个名为 main.cpp 的文件,作为您的主文件。
3. 在该文件夹中创建一个名为 "include" 的子文件夹,用于存储您的头文件。
4. 在 "include" 子文件夹中创建一个名为 "functions.h" 的文件,其中包含您的函数原型。
5. 在该文件夹中创建一个名为 "functions.cpp" 的文件,其中包含您的函数定义。
6. 在主文件 main.cpp 中包含 "functions.h" 头文件,以便可以调用其中定义的函数。
7. 在 VS Code 中打开该文件夹,并使用编译器编译和运行 main.cpp。
以下是一个示例代码:
```c++
// functions.h
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
int add(int a, int b);
#endif
// functions.cpp
#include "functions.h"
int add(int a, int b) {
return a + b;
}
// main.cpp
#include <iostream>
#include "include/functions.h"
int main() {
std::cout << add(5, 3) << std::endl;
return 0;
}
```
在 VS Code 中,您可以使用 C++ 插件来编译和运行您的代码。您可以在 VS Code 中按下 Ctrl + Shift + B,然后选择您的编译器来编译代码。运行时,您可以在终端中输入 ./main 来运行生成的可执行文件。
阅读全文
相关推荐


















