一文懂远程Linux服务器 VSCode 调试cpp文件&一次编译多cpp文件&正常库文件路径异常

远程Linux服务器 VSCode 调试cpp文件&多cpp文件,解决库文件路径异常

首先我们应该用VSCode打开项目所在文件夹

用VSCode项目专属的文件夹,否则会和其他cpp文件配置冲突
比如我的项目文件夹是TinyWebServer-master,那么VSCode就用新窗口打开这个文件夹
在这里插入图片描述

点击VSCode中的调试按钮!

在这里插入图片描述

选择gdb/g++ ,第一次打开项目调试会自动生成 .vscode 文件夹下 tasks.jsonlaunch.json
如果没有自动生成launch.json,可以自己手动创建,在你的项目目录/.vscode

为什么要配置json文件

先了解g++ 编译 cpp 文件的过程,
简单来说,就是在命令行中编译,然后运行生成的可执行文件,像这样

bash g++ main.cpp
bash ./a.out

调试是在命令行中用gdb,像这样

bash gdb a.out
gdb> start
...

而vscode两个json配置文件,实际上就是在命令行套了一层壳,task.json 是为了g++ 编译cpp,launch.json是为了gdb调试生成的可执行文件,我们只需要了解这两个配置文件怎么配置的,配好了就能用vscode进行调试和编译(而非命令行)

具体配置

一些配置没有修改的必要,具体看这个博客https://blog.csdn.net/qq_43152052/article/details/124953342

下面只讲关键点
配置就是对号入座,

对于tasks.json
如果你在多文件项目中,g++编译有很多要求的话,比如说我这个项目,

  1. 需要一次编译多文件,
  2. 我的mysql.h文件目录不在正常目录下,要链接其他位置的静态库动态库,

那么你可以在tasks.json中arg如下修改

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ 生成活动文件",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g", // 下面是你要一次编译的所有文件
                "${file}",
                "timer/lst_timer.cpp",
                "http/http_conn.cpp",
                "log/log.cpp",
                "CGImysql/sql_connection_pool.cpp",
                "webserver.cpp",
                "config.cpp",
                //到这截止
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-lpthread",
                "-L", // 选项 链接动态库
                "/www/server/mysql/lib/", // 参数
                "-lmysqlclient",
                "-I", // 选项 链接非正常目录静态库
                "/www/server/mysql/include/" // 参数
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}

对于mysql库路径错误,详细看我上一篇博客

对于launch.json

重要的是program关键字,它的value就是你的可执行文件

"program":"${workspaceRoot}/server",  

还有这个关键字

preLaunchTask

最后附上我的两个json文件仅供参考

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":"myexe : makefile build and debug",    
            "type":"cppdbg",        
            "request":"launch",    
            "program":"${workspaceRoot}/server",   
            "args":["param1","param2","2>&1",">","out"],    
            "stopAtEntry":false,    
            "cwd":"${workspaceRoot}",   
            "environment":[],            
            //"externalConsole":true,    
            "MIMode":"gdb",            
            "miDebuggerPath":"/usr/bin/gdb",    
            "setupCommands":[    
                {
                    "description":"Enable pretty-printing for gdb",   
                    "text":"-enable-pretty-printing",        
                    "ignoreFailures":true       
                },
                {
                    "description":"Let User defined signal 1 not appear",
                    "text":"handle SIGUSR1 nostop noprint",    
                    "ignoreFailures":true       
                }
            ]
    
        }
    ]
}

tasks.json

{
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: g++ 生成活动文件",
            "command": "/usr/bin/g++",
            "args": [
                "-fdiagnostics-color=always",
                "-g", // 下面是你要一次编译的所有文件
                "${file}",
                "timer/lst_timer.cpp",
                "http/http_conn.cpp",
                "log/log.cpp",
                "CGImysql/sql_connection_pool.cpp",
                "webserver.cpp",
                "config.cpp",
                //到这截止
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}",
                "-lpthread",
                "-L", // 选项 链接动态库
                "/www/server/mysql/lib/", // 参数
                "-lmysqlclient",
                "-I", // 选项 链接非正常目录静态库
                "/www/server/mysql/include/" // 参数
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "detail": "调试器生成的任务。"
        }
    ],
    "version": "2.0.0"
}
  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值