于初学Linux系统的小伙伴来说,由于对Linux系统及其在windows下的环境配置比较生疏,所以当使用windows下vscode连接Linux中的MySQL,并进行相关研发时,颇为头疼;下面对其环境配置做出具体介绍!!!
1. 安装Ubuntu及Linux系统
Ubuntu安装具体流程见:https://blog.csdn.net/weixin_47156401/article/details/120941578
2. 在Linux下安装MySQL
Linux下MySQL的安装教程网上较多,这里就不一一赘述了!
3.安装vscode及相关插件
除了需要安装C++后端开发所需要的一些插件外,还需要安装MySql相关的插件,主要有:MySQL(Jun Han)、MySQL(Weijian Chen)、MySQL Syntax,当然也可以安装一些美化的插件,如:SQL Beautify;
4. 在Linux下建立工程文件--->mySQL_test
5. 使用vscode连接Ubuntu,并打开mySQL_test
在mySQL_test工程下简历test01.cpp文件,以进行测试,如下图所示:
测试代码如下:
#include<iostream> #include<mysql.h> using namespace std; int main(){ cout<<"hell0 mysql"<<endl; return 0; }
6.在mySQL_test工程中生成环境配置文件
环境配置文件主要有:c_cpp_properties.json、tasks.json、launch.json;其相关介绍及其生成具体流程见:https://blog.csdn.net/weixin_47156401/article/details/129467510?spm=1001.2014.3001.5501
生成结果如下图所示:
(1)c_cpp_properties.json文件
{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", ], "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "c17", "cppStandard": "gnu++14", "intelliSenseMode": "linux-gcc-x64", "configurationProvider": "ms-vscode.makefile-tools" } ], "version": 4 }
(2)tasks.json文件
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++ 生成活动文件", "command": "/usr/bin/g++", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "调试器生成的任务。" } ], "version": "2.0.0" }
(3)launch.json文件
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "name": "(gdb) 启动", "type": "cppdbg", "request": "launch", "program": "输入程序名称,例如 ${workspaceFolder}/a.out", "args": [], "stopAtEntry": false, "cwd": "${fileDirname}", "environment": [], "externalConsole": false, "MIMode": "gdb", "setupCommands": [ { "description": "为 gdb 启用整齐打印", "text": "-enable-pretty-printing", "ignoreFailures": true }, { "description": "将反汇编风格设置为 Intel", "text": "-gdb-set disassembly-flavor intel", "ignoreFailures": true } ] } ] }
7.查询Linux下MySQL的相关路径
在Linux终端输入:
mysql_config
会出现如下界面,其中红色框选部分为Linux下MySQL的相关路径;
8.将MySQL的相关路径插入到vscode的配置文件中
(1)MySQL相关路径插入到c_cpp_properties.json文件中
(2)c_cpp_properties.json文件环境配置后代码
{ "configurations": [ { "name": "Linux", "includePath": [ "${workspaceFolder}/**", "/usr/include/mysql" ], "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "c17", "cppStandard": "gnu++14", "intelliSenseMode": "linux-gcc-x64", "configurationProvider": "ms-vscode.makefile-tools" } ], "version": 4 }
(3)MySQL相关路径插入到tasks.json文件中
(4)tasks.json文件环境配置后代码
{ "tasks": [ { "type": "cppbuild", "label": "C/C++: g++ 生成活动文件", "command": "/usr/bin/g++", "args": [ "-fdiagnostics-color=always", "-g", "${file}", "-I", "/usr/include/mysql", "-L", "/usr/lib/x86_64-linux-gnu", "-lmysqlclient", "-o", "${fileDirname}/${fileBasenameNoExtension}" ], "options": { "cwd": "${fileDirname}" }, "problemMatcher": [ "$gcc" ], "group": { "kind": "build", "isDefault": true }, "detail": "调试器生成的任务。" } ], "version": "2.0.0" }
说明:“-I”表示“Include”对应路径;“-L”表示“Lib”对应路径;和VS中配置Opencv道理相通;
9.配置结果
(1)配置前
(2)配置后
(3)代码运行结果
至此windows下vscode连接Linux中的MySQL环境配置完毕,如果工程中的源文件较多,可使用Makefile对其进行编译,具体操作流程见:https://blog.csdn.net/weixin_47156401/article/details/122033897
若有帮到你,点个赞,留下你的脚印哦!