远程Linux服务器 VSCode 调试cpp文件&多cpp文件,解决库文件路径异常
首先我们应该用VSCode打开项目所在文件夹
用VSCode项目专属的文件夹,否则会和其他cpp文件配置冲突
比如我的项目文件夹是TinyWebServer-master,那么VSCode就用新窗口打开这个文件夹
点击VSCode中的调试按钮!
选择gdb/g++ ,第一次打开项目调试会自动生成 .vscode 文件夹下 tasks.json 和 launch.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++编译有很多要求的话,比如说我这个项目,
- 需要一次编译多文件,
- 我的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"
}