Linker error when connecting C to mysql - 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.

Related

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

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.

How Do I Specify Additional Dependencies running Visual Studio from Command Line?

On Windows 10 I am trying to compile the following program from the book MySql by Paul DuBois using Visual Studio 2022:
#include <stdio.h>
#include <mysql.h>
#define def_host_name "localhost" /* host to connect to */
#define def_user_name "xxx" /* user name */
#define def_password "yyyyyy" /* user password */
#define def_db_name NULL /* database to connect to */
MYSQL *conn; /* pointer to connect handler */
int main (int argc, char *argv[])
{
conn = mysql_init(NULL);
mysql_real_connect( conn, /* pointer to connection handler */
def_host_name, /* host to connect to */
def_user_name, /* user name */
def_password, /* user password */
def_db_name, /* database to connect to */
0, /* port (using default) */
NULL, /* socket (use default) */
0 /* flags (none) */
);
mysql_close(conn);
exit(0);
}
It compiles and runs using the Visual Studio 2022 IDE with the following setting:
Additional Include Directories: C:\Program Files\MySQL\MySQL Server 8.0\include
Linker Additional Aibrary Directories: C:\Program Files\MySQL\MySQL Server 8.0\lib
Linker Input Additional Dependences: mysqlclient.lib
I am unable to compile from the command line. For example:
CL client1.c /I "C:\Program Files\MySQL\MySQL Server 8.0\include" /link "C:\Program Files\MySQL\MySQL Server 8.0\lib\mysqlclient.lib"
client1.obj
client1.obj : error LNK2019: unresolved external symbol _mysql_init#4 referenced in function _main
client1.obj : error LNK2019: unresolved external symbol _mysql_real_connect#32 referenced in function _main
client1.obj : error LNK2019: unresolved external symbol _mysql_close#4 referenced in function _main
C:\Program Files\MySQL\MySQL Server 8.0\lib\mysqlclient.lib : warning LNK4272: library machine type 'x64' conflicts with target machine type 'x86'
client1.exe : fatal error LNK1120: 3 unresolved externals
What command line should I be using?

"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;
}

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

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.