[C++] VS Code 中导入MYSQL (包含使用gcc,g++) [2022.12.20更新 最后放上vstudio的配置链接]
首先安装Gcc
1. 下载
链接 : https://sourceforge.net/projects/mingw-w64/files/
备用网盘 : https://pan.baidu.com/s/1trdQtDO6XqmfCbZHc1IDzQ?pwd=nwh5
2. 添加环境变量
3. 创建一个c/c++的文件,随后进入该文件所在文件夹
按住CTRL + ALT + P 进入配置,修改编译器路径

编译器路径:

修改IntelliSense模式:

随后会自动生成.vscode文件,文件下会包含c_cpp_properties.json文件
4. 编译c/c++文件后,会生成tasks.json 文件, 和launch.json 文件(有时需要手动创建)
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:\\Program Files\\MySQL\\MySQL Server 8.0\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "E:/BaiduNetdiskDownload/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe 生成活动文件",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
],
"group": "build",
"detail": "调试器生成的任务。"
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "E:/BaiduNetdiskDownload/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
],
"options": {
"cwd": "E:/BaiduNetdiskDownload/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch", // 配置名称,将会在启动配置的下拉菜单中显示
"type": "cppdbg", // 配置类型,这里只能为cppdbg
"request": "launch", // 请求配置类型,可以为launch(启动)或attach(附加)
"program": "${fileDirname}/${fileBasenameNoExtension}", //将要进行调试的程序的路径
"args": [], // 程序调试时传递给程序的命令行参数,一般设为空即可
"stopAtEntry": false, // 设为true时程序将暂停在程序入口处,一般设置为false
"cwd": "${workspaceFolder}", // 调试程序时的工作目录,一般为${workspaceFolder}即代码所在目录
"environment": [],
"externalConsole": false, // 调试时是否显示控制台窗口,一般设置为true显示控制台====用true的时候需要在return的上面加getchar();
"MIMode": "gdb",
"miDebuggerPath": "E:\\BaiduNetdiskDownload\\x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0\\mingw64\\bin\\gdb.exe", // miDebugger的路径,注意这里要与MinGw的路径对应
"preLaunchTask": "C/C++: gcc.exe 生成活动文件", // 调试会话开始前执行的任务,一般为编译程序,c++为g++, c为gcc
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": false
}
]
}
]
}
注意事项:
在编译cpp文件的时候,使用g++, 在编译c文件的时候,使用gcc
导入mysql
1. 下载mysql的c++版本
https://dev.mysql.com/downloads/installer/
2. 找到mysql对应的文件目录
example : C:\\Program Files\\MySQL\\MySQL Server 8.0\\include
需要在 c_cpp_properties.json以及tasks.json添加mysql的路径
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"C:\\Program Files\\MySQL\\MySQL Server 8.0\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.19041.0",
"compilerPath": "E:/BaiduNetdiskDownload/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/g++.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe 生成活动文件",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/nologo",
"/Fe${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$msCompile"
],
"group": "build",
"detail": "调试器生成的任务。"
},
{
"type": "cppbuild",
"label": "C/C++: g++.exe 生成活动文件",
"command": "E:/BaiduNetdiskDownload/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin/g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
//"-I", //包含头目录
//"C:\\Users\\Administrator.DC_LENG\\Desktop\\cpptest", //头目录路径
//"C:\\Users\\Administrator.DC_LENG\\Desktop\\cpptest\\datamanager.cpp", //不想一个个编译成 .o文件,直接包含 cpp 文件
"-I",
"C:\\Program Files\\MySQL\\MySQL Server 8.0\\include", // mysql头文件,有顺序要求,要求放在被导入文件之后,
"-L",
"C:\\Program Files\\MySQL\\MySQL Server 8.0\\lib", // mysql 库文件 dll
"-llibmysql" // 导入那个库
],
"options": {
"cwd": "E:/BaiduNetdiskDownload/x86_64-8.1.0-release-posix-sjlj-rt_v6-rev0/mingw64/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "调试器生成的任务。"
}
],
"version": "2.0.0"
}
3. 把libmysql.dll复制到主函数所在的文件夹
4. testcode
#include <iostream> #include <mysql.h> using namespace std; int main (){ cout << "Hello, world!" << endl; // q1(); MYSQL conn; int res; mysql_init(&conn); if (mysql_real_connect(&conn, "127.0.0.1", "root", "pswd", "test", 0, NULL, CLIENT_FOUND_ROWS)) //"root":数据库管理员 ,:123密码 "test":数据库的名字 { printf("connect success!\n"); res = mysql_query(&conn, "insert into test values('user','123456')"); if (res) { printf("error\n"); } else { printf("OK\n"); } mysql_close(&conn); } else { printf("connect error!\n"); cout << mysql_error(&conn) << endl; } return 0; }
Vstudio的配置链接:
https://blog.csdn.net/weixin_36466834/article/details/111604663

浙公网安备 33010602011771号