重要参考链接
https://www.cnblogs.com/TAMING/p/8560253.html ***** 主要参考
其他:
https://www.cnblogs.com/lkpp/p/vscode-cpp.html
https://www.zhihu.com/question/30315894
3个重要配置文件:
1、c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"D:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++",
"D:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/x86_64-w64-mingw32",
"D:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/backward",
"D:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include",
"D:/Program Files/mingw64/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/tr1",
"D:/Program Files/mingw64/x86_64-w64-mingw32/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.17763.0",
"compilerPath": "D:/Program Files/mingw64/bin/gcc.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
2、launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "C/C++",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}/${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"miDebuggerPath": "D:/Program Files/mingw64/in/gdb.exe",
"preLaunchTask": "g++",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
},
]
}
3、tasks.json
{
"version": "2.0.0",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}.exe"
],
"problemMatcher": {
"owner": "cpp",
"fileLocation": [
"relative",
"${workspaceRoot}"
],
"pattern": {
"regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
"file": 1,
"line": 2,
"column": 3,
"severity": 4,
"message": 5
}
},
"group": {
"kind": "build",
"isDefault": true
}
}
//VS Code使用问题跟踪
问题1:使用visual studio code 写C++ 怎么样才能正确地引用其他文件的函数?
解决思路:关键在于编译时,要同时编译两个cpp文件,而按VS Code的官方配置,只会编译一个文件。
方法① 在终端输入 g++ main.cpp function.cpp -o main.out 生成main.out可执行文件 然后 输入main.out 回车执行
方法②修改code runner的默认用户配置;code runner的原理就是自动帮你完成在控制台中输入切换路径和编译源代码以及运行编译好的程序的指令
{
"vetur.validation.template": false,
"liveServer.settings.donotShowInfoMsg": true,
"files.autoSave": "onFocusChange",
"explorer.confirmDelete": false,
"cmake-tools-helper.cmake_download_path": "c:\\Users\\ZZY\\.vscode\\extensions\\maddouri.cmake-tools-helper-0.2.1\\cmake_download",
"cmake.configureOnOpen": true,
"code-runner.runInTerminal": false, //使用内置终端
// "code-runner.ignoreSelection": true, //忽略用户选择
"code-runner.executorMap": {
// "cpp": "g++ *.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt" //cpp源文件都在一个项目根目录下 使用ok
// "cpp": "g++ main.cpp testModule/test_map.cpp -o $fileNameWithoutExt && $dir$fileNameWithoutExt"//运行ok
"cpp": "g++ main.cpp testModule/test_map.cpp -o main && $dir$fileNameWithoutExt"//运行ok
}
}
不同语言对应一条指令,运行code runner根据根据指令在控制台执行根据模板生成的命令行指令
如:
cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt
实际执行
其中
$dir代表文件路径
$fileName代表文件名
$fileNameWithoutExt代表文件名对应的exe文件名
$dir$fileNameWithoutExt相当于执行对应的编译好的文件
翻译过来就是切换(cd指令)到源文件所在路径,编译(g++)源文件,输入(-o参数)成对应文件名的可执行文件,然后执行