VS Code 对C++ 单文件、多文件(CMake工程) 配置文件
针对于单文件编译运行,需要在代码文件夹下建立子文件夹 .vscode ,并放置三个文件
1:c_cpp_properties.json,注意更改7、8、11行的路径
1 { 2 "configurations": [ 3 { 4 "name": "Win32", 5 "includePath": [ 6 "${workspaceFolder}", 7 "C:\\MinGw\\x86_64-w64-mingw32\\include", 8 "C:\\MinGw\\include" 9 ], 10 "defines": ["_DEBUG", "UNICODE", "_UNICODE"], 11 "compilerPath": "C:\\MinGw\\bin\\g++.exe", 12 "cStandard": "c11", 13 "cppStandard": "c++11", 14 "intelliSenseMode": "gcc-x64" 15 } 16 ], 17 "version": 4 18 }
2:launch.json,注意更改19行gdb路径
1 { 2 "version": "0.2.0", 3 "configurations": [ 4 5 { 6 "name": "Build", 7 "type": "cppdbg", 8 "request": "launch", 9 "program": "${workspaceFolder}/${fileBasenameNoExtension}.exe", 10 "args": [], 11 "stopAtEntry": false, 12 "cwd": "${workspaceFolder}", 13 "environment": [], 14 "externalConsole": false, 15 "MIMode": "gdb", 16 "logging": { 17 "engineLogging": false 18 }, 19 "miDebuggerPath": "C:/MinGw/bin/gdb.exe", 20 "setupCommands": [ 21 { 22 "description": "为 gdb 启用整齐打印", 23 "text": "-enable-pretty-printing", 24 "ignoreFailures": true 25 } 26 ], 27 "internalConsoleOptions": 28 } 29 ] 30 }
3:tasks.json
1 { 2 "version": "2.0.0", 3 "tasks": [ 4 { 5 "label": "Compile", 6 "type": "shell", 7 "command": "g++", 8 "args": ["-g", "${file}", "-o", "${fileBasenameNoExtension}.exe"], 9 "group": { 10 "kind": "build", 11 "isDefault": true 12 } 13 } 14 ] 15 }
针对于工程构建,需要在代码文件夹下建立CMakeLists.txt,并建立子文件夹 .vscode ,放置两个文件
1:CMakeLists.txt
1 # 最低CMake版本要求 2 cmake_minimum_required(VERSION 3.10.0) 3 4 # 项目名称 5 project(cmake-test) 6 7 # 设置C/C++标准 8 set(CMAKE_C_STANDARD 11) 9 set(CMAKE_CXX_STANDARD 17) 10 set(CMAKE_CXX_STANDARD_REQUIRED ON) 11 12 # 头文件路径 13 include_directories("Inc") 14 15 # 枚举头文件 16 file(GLOB_RECURSE INCLUDES "Inc/*.h" "Inc/*.hpp") 17 18 # 枚举源文件 19 aux_source_directory("Src" SOURCES) 20 21 # 输出路径 22 set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) 23 24 # 生成可执行的文件 25 add_executable(${PROJECT_NAME} ${SOURCES} ${INCLUDES})
2:c_cpp_properties.json
1 { 2 "configurations": [ 3 { 4 "name": "Win32", 5 "intelliSenseMode": "gcc-x64", 6 "includePath": ["${workspaceFolder}/Inc/"], 7 "cStandard": "c11", 8 "cppStandard": "c++11", 9 "configurationProvider": "vector-of-bool.cmake-tools" 10 } 11 ], 12 "version": 4 13 }
3:launch.json,注意更改15行gdb路径
1 { 2 "version": "0.2.0", 3 "configurations": [ 4 { 5 "name": "CMake-Build", 6 "type": "cppdbg", 7 "request": "launch", 8 "program": "${command:cmake.launchTargetPath}", 9 "args": [], 10 "stopAtEntry": false, 11 "cwd": "${workspaceFolder}", 12 "environment": [], 13 "externalConsole": false, 14 "MIMode": "gdb", 15 "miDebuggerPath": "C:/MinGw/bin/gdb.exe", 16 "setupCommands": [ 17 { 18 "description": "Enable pretty-printing for gdb", 19 "text": "-enable-pretty-printing", 20 "ignoreFailures": false 21 } 22 ] 23 } 24 ] 25 }