利用VS-code编写C/C++

2019-12-07  本文已影响0人  nowherespyfly

一、准备环境

二、配置vs-code

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch", 
            "type": "cppdbg", 
            "request": "launch", 
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe", 
            "args": [], 
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}", 
            "environment": [], 
            "externalConsole": true, 
            "internalConsoleOptions": "neverOpen", 
            "MIMode": "gdb", 
            "miDebuggerPath": "gdb.exe", 
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": false
                }
            ],
            "preLaunchTask": "Compile"
        }
    ]
}
  1. tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Compile", 
            "command": "clang",
            "args": [
                "${file}",
                "-o", 
                "${fileDirname}/${fileBasenameNoExtension}.exe",
                "-g",
                "-Wall", 
                "-static-libgcc", 
                "--target=x86_64-w64-mingw", 
            ], 
            "type": "process", 
            "group": {
                "kind": "build",
                "isDefault": true 
            },
            "presentation": {
                "echo": true,
                "reveal": "always", 
                "focus": false,
                "panel": "shared" 
            },
            // "problemMatcher":"$gcc" 
        }
    ]
}

3)settings.json

{
    "files.defaultLanguage": "c",
    "editor.formatOnType": true, 
    "editor.suggest.snippetsPreventQuickSuggestions": false, 
    "editor.acceptSuggestionOnEnter": "off",

    "code-runner.runInTerminal": true, 
    "code-runner.executorMap": {
        "c": "cd $dir && clang '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc --target=x86_64-w64-mingw -std=c11 && &'$dir$fileNameWithoutExt'",
        "cpp": "cd $dir && clang++ '$fileName' -o '$fileNameWithoutExt.exe' -Wall -g -O2 -static-libgcc --target=x86_64-w64-mingw -std=c++17 && &'$dir$fileNameWithoutExt'"
    },
    "code-runner.saveFileBeforeRun": true,
    "code-runner.preserveFocus": true, 
    "code-runner.clearPreviousOutput": false,
    "code-runner.ignoreSelection": true,
    "C_Cpp.clang_format_sortIncludes": true, 
    "C_Cpp.errorSquiggles": "Disabled", 
    "C_Cpp.autocomplete": "Disabled", 
    "C_Cpp.suggestSnippets": false, 
}
  1. 在工作目录下创建compile_flags.txt
-Wall
--target=x86_64-w64-mingw
-std=c++17

三、编译和调试

在工作目录下创建helloworld.cpp:

#include <iostream>
using namespace std;
int main(){
    cout << "hello world" << endl;
    return 0;
}

保存文件后Ctrl+Shift+B编译,.\a.exe运行;或者F5编译加调试加运行。更多关于调试的信息可以参见VSCode官网。如果以上配置都没问题的话,就可以得到输出“hello world”了。

[以上内容参考谭九鼎的知乎回答:Visual Studio Code 如何编写运行 C、C++ 程序?]

上一篇下一篇

猜你喜欢

热点阅读