error while connecting mariadb with c : undefined reference to `mysql_init#4' - mysql

I am trying to connect to mariadb database using c program. Initially it was showing error for #include <mysql.h> as no such file or directory.
But after including directory name, that problem is solved now, but it is showing another error.
Following is the code I was trying to run:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// #include "C:/Program Files/MariaDB 10.11/include/mysql/my_global.h"
#include "mysql/mysql.h"
int main (int argc, char* argv[])
{
// Initialize Connection
MYSQL *conn;
if (!(conn = mysql_init(0)))
{
fprintf(stderr, "unable to initialize connection struct\n");
exit(1);
}
// Connect to the database
if (!mysql_real_connect(
conn, // Connection
"mariadb.example.net", // Host
"db_user", // User account
"db_user_password", // User password
"test", // Default database
3306, // Port number
NULL, // Path to socket file
0 // Additional options
));
{
// Report the failed-connection error & close the handle
fprintf(stderr, "Error connecting to Server: %s\n", mysql_error(conn));
mysql_close(conn);
exit(1);
}
// Use the Connection
// ...
// Close the Connection
mysql_close(conn);
return 0;
}
I am getting following error in output:
PS C:\Dev\Win> gcc Db_con.c -o Db_con
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0x1e): undefined reference to `mysql_init#4'
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0xa1): undefined reference to `mysql_real_connect#32'
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0xaf): undefined reference to `mysql_error#4'
C:\Users\hajos\AppData\Local\Temp\ccGZ2Rhz.o:Db_con.c:(.text+0xd9): undefined reference to `mysql_close#4'
collect2.exe: error: ld returned 1 exit status
Can anyone explain what is the problem and how to solve it?

You have to link against the MariaDB Connector/C libraries.
From MariaDB Connector/C documentation:
Linking your application against MariaDB Connector/C
Windows
For static linking the library libmariadb.lib is required, for dynamic linking use libmariadb.dll. Using the MSI installer, these libraries can be found in the lib directory of your MariaDB Connector/C installation.
Unless you use the experimental plugin remote_io (which requires the curl library) there are no dependencies to other libraries than the Windows system libraries.

Related

"Can't connect ... through socket '/tmp/mysql.sock' " using C API mysql_real_connect() but /tmp/mysql.sock' exists

I'm running OS 11.1 Big Sur and updating an objective C program that uses the MySql C API (NOT the C Connector).
I CAN connect to local MySql server from the command line using
/usr/local/mysql/bin/mysql --user=root --password=aPassword
but when my software tries to connect through the C API using mysql_real_connect(mSQLPtr, theHost, theLogin, thePass, theDatabase, 0, NULL, 0); it fails with
Failed to connect to database: Error: Can't connect to local MySQL
server through socket '/tmp/mysql.sock' (1)
I've read heaps of posts to try and fix it e.g. stopping and starting the server, checking that /tmp/mysql.sock file does exist after starting the server (AND IT DOES EXIST), and adding a my.cnf to the /etc folder. It contains:
[mysqld]
socket=/tmp/mysql.sock
[client]
socket=/tmp/mysql.sock
Nothing has worked so far.
Can anyone offer any suggestions please.
Cheers
Jeff
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
static char *host = "localhost";
static char *user = "user";
static char *pass = "password";
static char *db = "mysql";
static char *socket = NULL;
unsigned int port = 3306;
unsigned int flags = 0;
/*
* brew install mysql-client
* export PATH=/usr/local/opt/mysql-client/bin:$PATH
* brew install mysql-connector-c++ (optional)
* gcc -o main $(mysql_config --cflags) main.c $(mysql_config --libs) -L/usr/local/opt/openssl/lib
*
* -L/usr/local/opt/openssl/lib is required if you get ld: library not found for -lssl error
*/
int main(int argc, char *argv[]) {
MYSQL *con = mysql_init(NULL);
if (!mysql_real_connect(con, host, user, pass, db, port, socket, flags)) {
fprintf(stderr, "Error %s (%d)", mysql_error(con), mysql_errno(con));
exit(1);
}
printf("Connected\n");
return EXIT_SUCCESS;
}

cannot link to static mysqlclient library, though shared library works

I have a sample program to familiarize myself with mysqlclient APIs. However when I compile and link it to the mysqlclient library statically (.a file), the linker complains it cannot find the file, although it exists in my path. Linking to the shared library (.dylib file on my Mac) works.
Please help me get my head around this behaviour. Much appreciated!
Here's my driver program client.c that calls mysqlclient library.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <mysql.h>
int main(int argc, char **argv)
{
MYSQL *mysql = NULL;
if (mysql_library_init(argc, argv, NULL)) {
fprintf(stderr, "could not initialize MySQL client library\n");
exit(1);
}
mysql = mysql_init(mysql);
if (!mysql) {
puts("Init faild, out of memory?");
return EXIT_FAILURE;
}
if (!mysql_real_connect(mysql, /* MYSQL structure to use */
NULL, /* server hostname or IP address */
NULL, /* mysql user */
NULL, /* password */
NULL, /* default database to use, NULL for none */
0, /* port number, 0 for default */
NULL, /* socket file or named pipe name */
CLIENT_FOUND_ROWS /* connection flags */ )) {
puts("Connect failed\n");
} else {
const char *query = "SELECT VERSION()";
if (mysql_real_query(mysql, query, strlen(query))) {
printf("Query failed: %s\n", mysql_error(mysql));
} else {
puts("Query OK");
}
}
mysql_close(mysql);
mysql_library_end();
return EXIT_SUCCESS;
}
Here's how I compile it
gcc -I /usr/local/Cellar/mysql/8.0.16/include/mysql client.c -L /usr/local/Cellar/mysql/8.0.16/lib/ -l mysqlclient.a
ld: library not found for -lmysqlclient.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Compiling without the .a succeeds, as it links to the shared library, not static one.
Lastly, here's my library files:
ls /usr/local/Cellar/mysql/8.0.16/lib/libmysqlclient*
/usr/local/Cellar/mysql/8.0.16/lib/libmysqlclient.21.dylib /usr/local/Cellar/mysql/8.0.16/lib/libmysqlclient.a /usr/local/Cellar/mysql/8.0.16/lib/libmysqlclient.dylib
This argument:
-l mysqlclient.a
causes the linker to look for a file named libmysqlclient.a.a. Instead, you want something like:
gcc -I /usr/local/Cellar/mysql/8.0.16/include/mysql client.c /usr/local/Cellar/mysql/8.0.16/lib/mysqlclient.a
-lmysqlclient should work. The extension .a does not need.
When you want to use static link, --static should be used.
You may also need to link other libraries
gcc client.c -o client --static -lmysqlclient -lssl -lcrypto -ldl -lpthread

Possible to use thrift in a mysql plugin?

I'm using Mysql 5.5 and the plugin need to query a thrift interfaced server for some information. I created the thrift client which basically opens a connection to the server, gets a status, and then closes the connection:
#include "../../xxxx/gen-cpp/Xxxx.h"
#include <transport/TSocket.h>
#include <transport/TBufferTransports.h>
#include <protocol/TBinaryProtocol.h>
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace ::za::co::xxxx;
int main(int argc, char **argv) {
boost::shared_ptr<TSocket> socket(new TSocket("localhost", 9090));
boost::shared_ptr<TTransport> transport(new TBufferedTransport(socket));
boost::shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
XxxxServiceClient client(protocol);
transport->open();
client.getStatus();
transport->close();
return 0;
}
I then changed main() to a function name and added it into the plugin code file and called it from the main function.
The plugin code builds fine but the map now contains a whole lot of thrift references and on trying to load the plugin, I get this error:
ERROR 1126 (HY000): Can't open shared library '/usr/lib/mysql/plugin/libxxxx.so' (errno: 13 undefined symbol: _ZTVN6apache6thrift9transport18TBufferedTransportE)
Is there any way to get these new thrift references resolved on installing the plugin? It installs and runs fine without the above code.
Using the Thrift cpp tutorial code I was able to create a simple hello world MySQL daemon plugin which made a client call to CppServer process:
#include <mysql/plugin.h>
#include <mysql_version.h>
#include <protocol/TBinaryProtocol.h>
#include <transport/TSocket.h>
#include <transport/TTransportUtils.h>
#include "gen-cpp/Calculator.h"
using namespace apache::thrift;
using namespace apache::thrift::protocol;
using namespace apache::thrift::transport;
using namespace tutorial;
using namespace shared;
using namespace boost;
static int hello_world_plugin_init(void *p) {
shared_ptr<TTransport> socket(new TSocket("localhost", 9090));
shared_ptr<TTransport> transport(new TBufferedTransport(socket));
shared_ptr<TProtocol> protocol(new TBinaryProtocol(transport));
CalculatorClient client(protocol);
transport->open();
client.ping();
transport->close();
return 0;
}
And this Makefile:
BOOST_DIR = /usr/include/boost
MYSQL_DIR = /usr/include/mysql
THRIFT_DIR = /usr/local/include/thrift
LIB_DIR = /usr/local/lib
GEN_SRC = gen-cpp/SharedService.cpp gen-cpp/shared_types.cpp gen-cpp/tutorial_types.cpp gen-cpp/Calculator.cpp
DEFS = -DMYSQL_DYNAMIC_PLUGIN -DHAVE_NETINET_IN_H
default: hello_thrift.cc
g++ ${DEFS} -fPIC -shared -o libhellothrift.so -I${MYSQL_DIR} -I${THRIFT_DIR} -I${BOOST_DIR} -Igen-cpp -L${LIB_DIR} hello_thrift.cc ${GEN_SRC} -lthrift
This is just example code and will crash your MySQL server if Thrift CppServer isn't running.
I tested this on Ubuntu 12.04 LTS using gcc 4.6.3, MySQL 5.5.24, Thrift 0.8.0, Boost 1.46

Linker error when connecting C to mysql

i want to connect C to mysql using the following code:
#include <C:\Program Files\MySQL\MySQL Connector C 6.0.2\include\my_global.h>
#include <C:\Program Files\MySQL\MySQL Connector C 6.0.2\include\mysql.h>
#include <stdio.h>
MYSQL *conn; /* pointer to connection handler */
int main ( int argc, char *argv[] )
{
conn = mysql_init ( NULL );
mysql_real_connect (
conn,"localhost","root","","operator", 3306, NULL,0 );
mysql_close ( conn );
return 0;
}
When i try to compile ,i get:
Error 3 error LNK2019: unresolved external symbol _mysql_real_connect#32 referenced in function _main C:\Users\kristel\Documents\Visual Studio 2010\Projects\database\database\database.obj database
Error 4 error LNK2019: unresolved external symbol _mysql_init#4 referenced in function _main C:\Users\kristel\Documents\Visual Studio 2010\Projects\database\database\database.obj database
It seems like something is missing but i dont know when.
I have added the mysqllib.lib like in the picture but still the problem is not resolved.
Just another question: Doing my searches,i have found something concerning cmake.Is there any relation between my error and cmake?
Thank you.
While you have included the mySQL header files in your code, you haven't included the mySQL library in your link options.

Build Error in developing C and MYSQL application

I am writing some code in C which connects to MYSQL server. I am using Netbeans and new to this. I configured it as directed and installed MYSQL Connector C. I also installed CYGWIN GCC, G++, GDB, MAKE from cygwin site. I created a c project and in the properties-> build-> c compiler->Include directories, set the path of mysql connector (C:\Program Files\MySQL\Connector C 6.0.2\include). Now i write some code to interect with MYSQL server, on build some error occurs.
#include <stdio.h>
#include <stdlib.h>
#include <mysql.h>
int main(int argc, char** argv) {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
char *server = "localhost";
char *user = "root";
char *password = "aaaa"; /* set me first */
char *database = "mysql";
conn = mysql_init(NULL);
/* Connect to database */
if (!mysql_real_connect(conn, server,
user, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
/* send SQL query */
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
/* output table name */
printf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
printf("%s \n", row[0]);
/* close connection */
mysql_free_result(res);
mysql_close(conn);
return (EXIT_SUCCESS);
}
After build following error occurs:
build/Debug/Cygwin-Windows/main.o: In function `main':
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:26: undefined reference to `_mysql_init'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:28: undefined reference to `_mysql_real_connect'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:30: undefined reference to `_mysql_error'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:34: undefined reference to `_mysql_query'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:35: undefined reference to `_mysql_error'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:38: undefined reference to `_mysql_use_result'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:41: undefined reference to `_mysql_fetch_row'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:44: undefined reference to `_mysql_free_result'
/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2/main.c:45: undefined reference to `_mysql_close'
make[2]: Leaving directory `/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2'
make[1]: Leaving directory `/cygdrive/c/Documents and Settings/AEM/My Documents/NetBeansProjects/CppApplication_2'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/Cygwin-Windows/cppapplication_2.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2
BUILD FAILED (exit value 2, total time: 1s)
I googled a lot but could not find anything to solve this issue. Need Help.
add -lmysql to link with the mysql library (valid for gcc). Or, if you use another Compiler, tell the Compiler
where the Libs are (add the Path)
to link the Library to the executable. This is something different, then adding the Path.