已解决
linux下使用vscode对C++项目进行编译
来自网友在路上 156856提问 提问时间:2023-11-06 15:25:22阅读次数: 56
最佳答案 问答题库568位专家为你答疑解惑
项目的目录结构
头文件swap.h
在自定义的头文件中写函数的声明。
// 函数的声明
void swap(int a,int b);
swap.cpp
导入函数的声明,写函数的定义
#include "swap.h" // 双引号表示自定义的头文件
#include <iostream>
using namespace std;// 函数的定义
void swap(int a,int b)
{int temp=a;a = b;b = temp;cout <<"a="<<a<<endl;cout <<"b="<<b<<endl;
}
main.cpp
主函数
#include <iostream>using namespace std;
#include <swap.h>
// 函数的分文件编写// // 函数的声明
// void swap(int a,int b);// // 函数的定义
// void swap(int a,int b)
// {
// int temp=a;
// a = b;
// b = temp;
// cout <<"a="<<a<<endl;
// cout <<"b="<<b<<endl;
// }int main()
{int a=10;int b=20;swap(a,b);return 0;
}
编译
按ctrl+` 打开终端
输入以下命令进行编译
g++ main.cpp src/swap.cpp -Iinclude -o main
-I是指定头文件所在的目录,include此处是头文件swap.h放置的目录。
g++后面是要编译的文件,main和src目录下的文件都要编译。
运行
终端中输入以下命令
./main
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"linux下使用vscode对C++项目进行编译":http://eshow365.cn/6-33739-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: Leetcode48旋转图像
- 下一篇: 什么是OTP认证?OTP认证服务器有哪些应用场景?