1

I'm trying to do a simple mysql connection with C++, I am using VSCode and g++ for compiling.

The error is

/tmp/cc79SKd5.o: In function `main':
/home/cameron/Documents/CodingTutorials/DBtest/main.cpp:5: undefined reference to `mysql_init'
/home/cameron/Documents/CodingTutorials/DBtest/main.cpp:6: undefined reference to `mysql_real_connect'

Searching around previous posts it seems like I haven't included the correct linking arguments, I found this post Mysql with C++ error: undefined reference to mysql_init but I don't understand how to do this in VScode.

I think I'm missing something basic here.

My code is

#include<iostream>
#include<mysql/mysql.h>
int main(){
    MYSQL* conn;
    conn = mysql_init(0);
    conn = mysql_real_connect(conn,"192.168.0.112","root","xxx","testDB",0,NULL,0);
    if (conn){
        std::cout<<"DB Connected";
    }
};

My Launch.json is

"version": "0.2.0",
    "configurations": [
        {
            "name": "g++ - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/main",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++ build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

My c_cpp_properties is

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/usr/include/mysql/**",
                "/usr/include/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/gcc",
            "compilerArgs":["mysql_config --cflags --libs"],
            "cStandard": "gnu11",
            "cppStandard": "gnu++14",
            "intelliSenseMode": "clang-x64"
        }
    ],
    "version": 4
}
Camh
  • 25
  • 6
  • What command line are you using to building your program? The error is telling you that you haven't added the MySQL libs to your link-line. – Mansoor Aug 24 '20 at 08:10
  • I am trying to build it in debug mode in VScode – Camh Aug 24 '20 at 08:14
  • If I had to guess I would say `"compilerArgs":["$(mysql_config --cflags --libs)"]` but really I'm guessing. The point is if you were running this from the command line then what needs to be passed to the compiler is `$(mysql_config --cflags --libs)` not `mysql_config --cflags --libs`. – john Aug 24 '20 at 08:41
  • It would help solve this problem if you can find out what command line VSCode is generating. – john Aug 24 '20 at 08:43
  • Tried adding $(mysql_config --cflags --libs) but no luck – Camh Aug 24 '20 at 12:13
  • The Terminal command it is running is > Executing task: /usr/bin/g++ -g /home/cameron/Documents/CodingTutorials/DBtest/main.cpp -o /home/cameron/Documents/CodingTutorials/DBtest/main – Camh Aug 24 '20 at 12:14
  • g++ -g main.cpp -o main $(mysql_config --cflags --libs) Works if I run it in a terminal window manually, just not through VSCode – Camh Aug 24 '20 at 12:19

0 Answers0