- 编写C++样例程序
该文件命名为main.cpp
#include
using namespace std;
int main(int argc , char ** argv)
{
int value1 = 10 ;
int value2 = 20;
cout << "Before Swap : " << endl;
cout << "value1 = " << value1 << "\t value2 = " << value2 << endl;
swap( value1 , value2 );
cout << "After Swap : " << endl;
cout << "value1 = " << value1 << "\t value2 = " << value2 << endl;
return 0;
}
- 使用linux命令初步运行
g++ .\main.cpp
注意
(1) 只输入g++ 文件名,会默认生成a.exe可执行文件
(2) 若想指定生成exe文件名,须加参数-o,如:
g++ .\main.cpp -o main.exe
# 或者
g++ .\main.cpp -o [指定文件名(不加文件扩展名亦可)]
(3) 若想生成带有调试信息的exe文件,须在g++后指定参数-g,如:
g++ -g .\main.cpp
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name" : "g++.exe - 生成和调试活动文件" ,
"type": "cppdbg" ,
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Server\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": []
}
此处文件夹命名为TestProject
void swap(int &a , int &b);
#include"swap.h"
void swap(int &a , int &b)
{
int temp = a ;
a = b;
b = temp;
}
#include
#include "swap.h"
using namespace std;
int main(int argc , char ** argv)
{
int value1 = 10 ;
int value2 = 20 ;
cout << "Before Swap : " << endl;
cout << "value1 = " << value1 << "\t value2 = " << value2 << endl;
swap( value1 , value2 );
cout << "After Swap : " << endl;
cout << "value1 = " << value1 << "\t value2 = " << value2 << endl;
return 0;
}
g++ -g .\main.cpp .\swap.cpp -o multi_swap
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name" : "g++.exe - 生成和调试活动文件" ,
"type": "cppdbg" ,
"request": "launch",
// "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"program": "${workspaceFolder}\\multi_swap.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "D:\\Server\\MinGW\\bin\\gdb.exe",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
// "preLaunchTask": "C/C++: g++.exe build active file"
}
]
}
注意
(1) 此处由于是多文件编译,故须修改program字段的值 :
① 首先查看cwd的值,[ 是 当前工作目录空间 (即当前项目根目录TestProject下)]
② 复制cwd的值至program,并在其后指明3.6命令生成exe文件的名称(全称,扩展名不可省略)
(2) 由于已在3.6命令编译链接生成exe文件,且无Tasks.json文件,故将PreLaunchTask注释
(3) 按F5执行程序前,注意打上断点