乐趣区

关于c++:VSCode-C开发环境配置文件WindowsMac

Windows(应用 MSVC 编译)

  • c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Win32",
            "includePath": ["${workspaceFolder}/**"
            ],
            "defines": [
                "_DEBUG",
                "UNICODE",
                "_UNICODE"
            ],
            "windowsSdkVersion": "8.1",
            "compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/cl.exe",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "msvc-x64"
        }
    ],
    "version": 4
}
  • launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "cl.exe - 生成和调试流动文件",
            "type": "cppvsdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "preLaunchTask": "C/C++: cl.exe build active file"
        }
    ]
}
  • tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "msvc build",
            "type": "shell",
            "command": "cl.exe",
            "args": [
                "/EHsc",
                "/Zi",
                "/Fe:",
                "main.exe",
                "main.cpp"
            ],
            "group": "build",
            "presentation": {"reveal": "always"},
            "problemMatcher": "$msCompile"
        },
        {
            "type": "shell",
            "label": "C/C++: cl.exe build active file",
            "command": "cl.exe",
            "args": [
                "/Zi",
                "/EHsc",
                "/Fe:",
                "${fileDirname}\\${fileBasenameNoExtension}.exe",
                "${file}"
            ],
            "options": {"cwd": "${workspaceFolder}"
            },
            "problemMatcher": ["$msCompile"],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}
  • Windows 中应用 MSVC 来编译源文件肯定要通过 VS 提供的 Developer Command Prompt(开发人员命令提醒)工具来关上。
  • 打开方式是:

    • cd 到源文件的工作区。
    • 输出“code.”,之后就会主动关上 VSCode。
  • 想调试源文件就将 launch.json 中的 stopAtEntry 的值设置为 true。之后打断点,F5 就能够调试了。

Mac(应用 clang 编译)

  • c_cpp_properties.json
{
    "configurations": [
        {
            "name": "Mac",
            "includePath": ["${workspaceFolder}/**"
            ],
            "defines": [],
            "macFrameworkPath": ["/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/System/Library/Frameworks"],
            "compilerPath": "/usr/bin/clang",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}
  • launch.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "clang++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "targetArchitecture": "x86_64",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "lldb",  
            "preLaunchTask": "clang++ build active file"
        }
    ]
}
  • tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "clang++ build active file",
            "command": "/usr/bin/clang++",
            "args": [
                "-g",
                "-std=c++11",
                "-lpthread",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {"cwd": "/usr/bin"}
        },
        {
            "type": "shell",
            "label": "g++ build active file",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "-std=c++11",
                "-lpthread",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "options": {"cwd": "/usr/bin"}
        }
    ]
}
退出移动版