0

I am trying to connect c++ to a mysql database and get this error

undefined reference to `mysql_init'
undefined reference to `mysql_real_connect'

my code is

#include <iostream>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "mysql.h"

int main(){
    MYSQL* conn;
    MYSQL_ROW row;
    MYSQL_RES * res;
    
    conn = mysql_init(0);
    conn = mysql_real_connect(conn, "localhost", "root", "", "cpp", 0, NULL, 0);
    if(conn){
        std::cout << "Connected to database successfully";
    } else {
        std::cout << "Connection failed...";
        exit(0);
    }

    return 0;
}

I am compiling with

g++ main.cpp

I have included all header files in the same directory

This stack overflow answer did not work for me

Mysql with C++ error: undefined reference to mysql_init

I am on windows 11

  • 3
    You must also link to the mysql library. If you are on Linux, this will probably be a `.a` file. If you are on Windows, this will probably be a `.lib` file. – Andreas Wenzel Jun 27 '22 at 01:34
  • Please read https://dev.mysql.com/doc/c-api/8.0/en/c-api-building-clients.html – Bill Karwin Jun 27 '22 at 01:58

0 Answers0