prerequisites
- install
Visual Studio Code
. - install
C/C++ extension for VS Code
.
3. Configure the C/C++
environment and make sure it is installed GCC
.
Create project
1. Create a testCPP project under VScode, and then create a test.cpp file in the project root directory, which stores the code for connecting to the database.
#include <mysql.h>
#include <iostream>
using namespace std;
int main() {
MYSQL *conn_mysql;
// 初始化连接
conn_mysql = mysql_init(NULL);
if (!conn_mysql) {
fprintf(stderr, "Mysql初始化连接失败\n");
return(EXIT_FAILURE);
}
// 参数分别为:初始化的连接句柄指针,主机名(或者IP),用户名,密码,数据库名,端口号,NULL,0)后面两个参数在默认安装mysql>的情况下不用改
conn_mysql = mysql_real_connect(conn_mysql, "127.0.0.1", "root", "123456", "test_c", 3306, NULL, 0);
if (conn_mysql){
printf( "连接成功!\n" );
}
else{
printf( "连接失败!\n" );
}
// 关闭数据库连接
mysql_close(conn_mysql);
return(EXIT_SUCCESS);
}
复制代码
- To configure tasks, select Terminal -> Configuration Tasks in the top menu bar, a folder will be generated in the project directory
.vscode
, and.vscode
files will also be generated under the foldertask.json
.
- Tasks.json file configuration under liunx
{
// 请参阅 https://go.microsoft.com/fwlink/?LinkId=733558
//查看有关 tasks.json 格式的文档
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ 生成活动文件",
"command": "/usr/bin/g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
// 配置新增开始
"-I",
"/usr/include/mysql", // 包含头文件目录
"-L",
"/usr/lib64/mysql", // 动态库目录
"-lmysqlclient" // 导入的哪个库
// 配置新增结束
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "编译器: /usr/bin/g++"
}
]
}
复制代码
Note: /usr/include/mysql
and /usr/lib64/mysql
these two directories need to be installed mysql-devel
before they exist, the specific installation stepshttps://centos.pkgs.org/7/mysql-5.7-x86_64/mysql-community-devel-5.7.24-1.el7.x86_64.rpm.html
- Tasks.json file configuration under window10
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cpp.exe 生成活动文件",
"command": "L:\\mingw64\\bin\\cpp.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "编译器: L:\\mingw64\\bin\\cpp.exe"
}
]
}
复制代码
- To configure the c++ build file, view the configuration by running the command: Edit Configuration ( )
C / C ++
from the Command Palette ( ) . This will open the C/C++ Configuration page. When you make a change here, VS Code writes it to a .vscode file called c_cpp_properties.json in the folder.Ctrl + Shift + P
UI
C / C ++
UI
- c_cpp_properties.json file under linux
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/mysql/**" // 只修改这个地方,其他地方默认
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
复制代码
- c_cpp_properties.json file under window10
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"F:/MySql/mysql-5.7.24-winx64/include/**"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "L:\\mingw64\\bin\\gcc.exe",
"cStandard": "gnu17",
"cppStandard": "gnu++14",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
复制代码
The above is all the configuration. Ctrl+Shift+B
After compiling, you can see the effect by executing the compiled file under the command line.