vs code 编译c++

1.刚刚知道vs2013和vs code是两个东西。。。尴尬。

2.刚开始用是为了使用LeetCode插件,直接在插件中下载就行,不用下载github上的那个。首先下载vscode,用官网就行。还要求nodejs8+,这个在它那个readme里有没什么难度。

3.完事后想着要不把C++的环境也配置下,于是乎开始了尴尬之旅,我太难了。

需要C++插件和Run Code插件然后安装mingw,这个玩意太难了,主要是我是win7 32位,下载地址不要用这个,这是64位的,用这个,进去后点download后选这个

下载好后才算是安装了一个下载器,打开这个下载器,把这四个安装上,默认路径是c:/MinGW/bin,把这个添加到环境变量中。

现在还缺一个gdb,在这里,下载后解压,把gdb复制到bin下。这里先可以了。然后是vscode中是这样

launch文件为:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "(gdb) Launch",
            "preLaunchTask": "build",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "miDebuggerPath": "c:/MinGW/bin/gdb.exe",    #唯一需要注意的地方
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }]
}

tasks文件为:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "windows": {
                "command": "g++",
                "args": [
                    "-ggdb",
                    "\"${file}\"",
                    "--std=c++11",
                    "-o",
                    "\"${fileDirname}\\${fileBasenameNoExtension}.exe\""
                ]
            }
        }
    ]
}

settings文件为:

{
    "files.defaultLanguage": "c", // ctrl+N新建文件后默认的语言
    "editor.formatOnType": true, // (对于C/C++)输入分号后自动格式化当前这一行的代码
    "editor.suggest.snippetsPreventQuickSuggestions": false, // clangd的snippets有很多的跳转点,不用这个就必须手动触发Intellisense了
    "editor.acceptSuggestionOnEnter": "off", // 我个人的习惯,按回车时一定是真正的换行,只有tab才会接受Intellisense
    // "editor.snippetSuggestions": "top", // (可选)snippets显示在补全列表顶端,默认是inline
 
    "code-runner.runInTerminal": true, // 设置成false会在“输出”中输出,无法输入
    "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'"
        // "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命令;未注释的仅适用于PowerShell(Win10默认),文件名中有空格也可以编译运行;注释掉的适用于cmd(win7默认),也适用于PS,文件名中有空格时无法运行
    "code-runner.saveFileBeforeRun": true, // run code前保存
    "code-runner.preserveFocus": true, // 若为false,run code后光标会聚焦到终端上。如果需要频繁输入数据可设为false
    "code-runner.clearPreviousOutput": false, // 每次run code前清空属于code runner的终端消息,默认false
    "code-runner.ignoreSelection": true, // 默认为false,效果是鼠标选中一块代码后可以单独执行,但C是编译型语言,不适合这样用
 
    "C_Cpp.clang_format_sortIncludes": true, // 格式化时调整include的顺序(按字母排序)
    "C_Cpp.errorSquiggles": "Disabled", // 因为有clang的lint,所以关掉;如果你看的是别的答主用的不是vscode-clangd,就不要加这个了
    "C_Cpp.autocomplete": "Disabled", // 同上;这几条也可以考虑放到全局里,否则很多错误会报两遍,cpptools一遍clangd一遍
    "C_Cpp.suggestSnippets": false,
    "files.associations": {
        "iostream": "cpp",
        "ostream": "cpp"
    }, // 同上
}

然后应该可以了,按F5.

补充:

1.中文乱码的解决办法:

忘了当时是怎么搜到的了,应该是在setting.josn下加入这个。

2.需要编译的时候,把文件复制到C++文件夹中,还不能有中文名字,就可以编译;一般会出现终端即将被重用,不用理他等一会就行。

3.记录下几个插件:

background:背景有个卡通人物

Bracket Pair Colorizer 2:彩色括号

4.我也不知道为啥上面的配置好像不好用了,有搜了新的配置:

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "C++ Launch (GDB)",                 // 配置名称,将会在启动配置的下拉菜单中显示
            "type": "cppdbg",                           // 配置类型,这里只能为cppdbg
            "request": "launch",                        // 请求配置类型,可以为launch(启动)或attach(附加)
            "launchOptionType": "Local",                // 调试器启动类型,这里只能为Local
            "targetArchitecture": "x86",                // 生成目标架构,一般为x86或x64,可以为x86, arm, arm64, mips, x64, amd64, x86_64
            "program": "${file}.exe",                   // 将要进行调试的程序的路径
            "miDebuggerPath":"c:\\MinGW\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
            "args": ["blackkitty",  "1221", "# #"],     // 程序调试时传递给程序的命令行参数,一般设为空即可
            "stopAtEntry": false,                       // 设为true时程序将暂停在程序入口处,一般设置为false
            "cwd": "${workspaceRoot}",                  // 调试程序时的工作目录,一般为${workspaceRoot}即代码所在目录
            "externalConsole": true,                    // 调试时是否显示控制台窗口,一般设置为true显示控制台
            "preLaunchTask": "g++"                    // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
        }
    ]
}

tasks.json

{
    "version": "0.1.0",
    "command": "g++",
    "args": ["-g","${file}","-o","${file}.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
        }
    }
}

这样既直接F5就行了,又能显示中文。

-------------------------------我是分割线-------------------------------

下面用ubuntu16.04安装一下,首先下载deb的安装包,然后双击安装成功,主要是配置。再建立一个文件夹比如叫vscode吧,在下面写一个基本的程序,其中是这个结构:

launch.json文件内容为:

{
    // 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": "(gdb) Launch",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": true,
            "MIMode": "gdb",
            "preLaunchTask": "build",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ]
        }
    ]
}

tasks.json文件内容为:

{
    // See https://go.microsoft.com/fwlink/?LinkId=733558
    // for the documentation about the tasks.json format
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build",
            "type": "shell",
            "command": "g++",
            "args": [
                "-g",
                "${file}",
                "-std=c++11",
                "-o",
                "${fileBasenameNoExtension}.out"
            ],
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
}

设置完后按F5就可以,就是最后那里有点乱码,不太影响吧。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值