Calling libmysqlclient C functions from C++
Posted by:
Alan Shank
Date: August 06, 2011 04:01PM
I have a qt4 C++ mainwindow.cpp source file, on Cygwin. I am trying to use the C API. I have:
extern "C"
{
#include "mysql.h"
}
... a bunch of C++ code
...a function
bool MainWindow::init(void)
{
MYSQL *conn;
char db[16];
char server[16];
char user[5];
char pass[8];
strcpy (db, "track");
strcpy (server, "localhost");
strcpy (user, "alan");
strcpy (pass, "ythons");
mysql_init(& conn);
if (!mysql_real_connect(& conn, server, user, pass, db, 0, NULL, 0) )
{
QMessageBox::information(0, "Next", "Failed to connect to DB", QMessageBox::Ok, QMessageBox::Ok);
}
}
It all compiles OK, but the linker gets these errors:
mainwindow.o:mainwindow.cpp:(.text+0xcd): undefined reference to `_mysql_init'
mainwindow.o:mainwindow.cpp:(.text+0x104): undefined reference to `_mysql_real_c
onnect'
collect2: ld returned 1 exit status
make: *** [exper.exe] Error 1
The C++ compiler is "mangling" the C function names, as I see using "nm mainwindow.o". I thought including mysql.h with extern "C" would prevent that.
Thanks,
Alan Shank
Woodland, CA, USA