'Error: Undefined reference to mysql_init and mysql_real_connect
good I am developing a C ++ program using CodeBlocks that connects to a MySQL database with XAMPP using the libmysql.a linker but I can't get it to work, this is the code:
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <mysql.h>
#include <string>
#include <vector>
using namespace std;
const char* hostname = "localhost";
const char* username = "root";
const char* password = "";
const char* database = "productos";
unsigned int port = 3306;
const char* unixsocket = NULL;
unsigned long clientflag = 0;
connectDatabase(){
MYSQL *conn;
conn = mysql_init(0);
conn = mysql_real_connect(conn, hostname, username, password, database, port, unixsocket, clientflag);
if (conn){
cout << "Conexión realizada con éxito!!!" << endl;
}else{
cout << "Ha habido un error..." << endl;
}
int main (){
connectDatabase();
}
As I have configured everything :
I attach a capture of the errors in the output:
||=== Build: Debug in OrganizadorProductosPAP (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `connectDatabase()':|
C:\Users\Pablo\Documents\C++\OrganizadorProductosPAP\main.cpp|29|undefined reference to `mysql_init'|
C:\Users\Pablo\Documents\C++\OrganizadorProductosPAP\main.cpp|30|undefined reference to `mysql_real_connect'|
||error: ld returned 1 exit status|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Build log:
-------------- Build: Debug in OrganizadorProductosPAP (compiler: GNU GCC Compiler)---------------
g++.exe -o bin\Debug\OrganizadorProductosPAP.exe obj\Debug\main.o obj\Debug\src\Producto.o C:\Users\Pablo\Documents\C++\OrganizadorProductosPAP\libmysql.a
obj\Debug\main.o: In function `connectDatabase()':
C:/Users/Pablo/Documents/C++/OrganizadorProductosPAP/main.cpp:29: undefined reference to `mysql_init'
C:/Users/Pablo/Documents/C++/OrganizadorProductosPAP/main.cpp:30: undefined reference to `mysql_real_connect'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
3 error(s), 0 warning(s) (0 minute(s), 0 second(s))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|