A prerequisite for

  1. The installationVisual Studio Code.
  2. The installationC/C++ extension for VS Code.

3. Good configurationC/C++Environment, ensure that it has been installedGCC.

Create a project

1. Create the testCPP project under VScode, and then create the test. CPP file in the root directory of the project.

#include <mysql.h> #include <iostream> using namespace std; int main() { MYSQL *conn_mysql; // initialize the connection conn_mysql = mysql_init(NULL); if (! Conn_mysql) {fprintf(stderr, "Mysql failed to initialize connection \n"); return(EXIT_FAILURE); } // The parameters are: Initialized connection handle pointer, host name (or IP), username, password, database name, port number, NULL, Conn_mysql = mysql_real_connect(conn_mysql, "127.0.0.1", "root", "123456", "test_c", 3306, NULL, 0); If (conn_mysql){printf(" Connect successfully! \n" ); } else{printf(" Connection failed! \n" ); } // close the database connection mysql_close(conn_mysql); return(EXIT_SUCCESS); }Copy the code
  1. Configuration tasks, from the top menu bar select Terminal -> Configuration Tasks, will be generated in the project directory.vscodeFolder, at the same time.vscodeIt will also be generated under the foldertask.jsonFile.
  • Tasks. json file configuration in liunx
{please refer to https://go.microsoft.com/fwlink/?LinkId=733558 / / / / see the tasks. The json format of the document "version" : "2.0.0", "tasks" : [{" type ":" cppbuild ", "label" : "C/C + + : g + + activity generated file", "command" : "/ usr/bin/g + +", "args" : [" g ", "${file}", "- o", "${fileDirname} / ${fileBasenameNoExtension}", / / configure new start "-i", "/ usr/include/mysql", // include header directory "-l ", "/usr/lib64/mysql", "-lmysqlClient ", "options": {" CWD ": "${workspaceFolder}}", "problemMatcher" : "$GCC"], "group" : {" kind ":" build ", "isDefault" : true}, "detail" : "compiler: /usr/bin/g++" } ] }Copy the code

Note: /usr/include/mysql and /usr/lib64/mysql will not exist until mysql-devel is installed. The specific installation steps at https://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 files generated activity", "command" : "L:\\mingw64\\bin\\cpp.exe", "args": [ "-g", "${file}", "-o", "${fileDirname}\\${fileBasenameNoExtension}.exe" ], "options": { "cwd": "${workspaceFolder}}", "problemMatcher" : "$GCC"], "group" : "build", "detail" : "compiler: L: \ \ mingw64 \ \ bin \ \ CPP exe"}}]Copy the code
  1. Configure the c++ compilation file by running the commandC / C ++: From the command panel (Ctrl + Shift + P) Edit configuration (UI) to seeC / C ++configurationUI . The C/C ++ Configuration page is displayed. When you make changes here, VS Code writes them to a.vs Code file in the folder called c_cpp_properties.json.

  • In Linux, c_cpp_properties.json file

{
  "configurations": [{"name": "Linux"."includePath": [
        "${workspaceFolder}/**"."/usr/include/mysql/**" // Change only this place, other places default]."defines": []."compilerPath": "/usr/bin/gcc"."cStandard": "gnu17"."cppStandard": "gnu++14"."intelliSenseMode": "linux-gcc-x64"}]."version": 4
}

Copy the code
  • Window10: c_cpp_properties.json file
{
  "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
}
Copy the code

That’s all, Ctrl+Shift+B compendies. execute the compiled file on the command line to see the effect.