vscode源文件和可执行文件分离

原文地址: vscode源文件和可执行文件分离

vscode源文件和可执行文件分离

前言

用vscode写c/c++时, 为了方便, 会把不同的源文件放在一个文件夹里

这里不是做项目, 一个源文件就是一个单独的程序

然后生成的可执行文件和源代码就会放在一个目录里, 还是同名, 就很容易点错, 所以就想着改改

image-20210329094648997

环境和版本

  • Visual Studio Code 1.54.3
  • Liunx Ubuntu 18.04 LTS
  • g++ 7.5.0

code-runner设置

打开vscode, 菜单栏文件->首选项->设置

2021-03-29 09-59-00 的屏幕截图

使用搜索功能, 搜索code-runner并找到Excutor Map, 点击在settings.json编辑

image-20210329100143260

出来这样的页面, 我们要修改的是cpp这一行, 当然, 其它语言也是一样

image-20210329100349464

修改成如下:

扫描二维码关注公众号,回复: 13025521 查看本文章
"cpp": "cd $dir && if [ ! -d \"bin\" ];then mkdir bin;fi  && g++ $fileName -o bin/$fileNameWithoutExt && bin/$fileNameWithoutExt"

其中if [ ! -d \"bin\" ];then mkdir bin;fi为判断bin是否存在, 如果不存在则创建, 当然, bin这个名字也改成自己喜欢的

调试

配置调试的是launch.json这个文件, 而在调试程序执行前会需要先进行编译生成可执行二进制文件, 这时就需要配置task.json文件

launch.json中, 通过改写preLaunchTask参数, 并在task.json中添加参数, 可以达到预先创建bin文件夹的目的

069

task.json中加上如下任务:

{
    
    
            "type":"shell",
            "label":"创建bin文件夹",
            "command":" if [ ! -d \"bin\" ];then mkdir bin;fi"
 }

并在生成可执行文件的参数中, 更改目录

"args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/bin/${fileBasenameNoExtension}"
            ],

launch.json中修改可执行文件的位置

"program": "bin/${fileBasenameNoExtension}"

lauch.json中添加任务:

"preLaunchTask": [
                "创建bin文件夹",
                "C/C++: g++ 生成活动文件"
            ]

完整tasks.json参考代码

{
    
    
    "tasks": [
        {
    
    
            "type":"shell",
            "label":"创建bin文件夹",
            "command":" if [ ! -d \"bin\" ];then mkdir bin;fi"
        },
        {
    
    
            "type": "cppbuild",
            "label": "C/C++: g++ 生成活动文件",
            "command": "/usr/bin/g++",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/bin/${fileBasenameNoExtension}"
            ],
            "options": {
    
    
                "cwd": "${workspaceFolder}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
    
    
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

完整的launch.json

{
    
    
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
    
    
            "name": "g++ - 生成和调试活动文件",
            "type": "cppdbg",
            "request": "launch",
            "program": "bin/${fileBasenameNoExtension}",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
    
    
                    "description": "为 gdb 启用整齐打印",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": [
                "创建bin文件夹",
                "C/C++: g++ 生成活动文件"
            ],
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

没必要直接复制粘贴, 版本可能会更改而不适用, 看懂怎么改的即可

windows系统方法也类似, 要注意的

  • 路径分隔符号不一样, linux是/, windows是\

  • 创建文件夹的命令需要修改, 这里给出参考

    if not exist bin ( md bin);
    

猜你喜欢

转载自blog.csdn.net/qq_42848693/article/details/115295868