cannot link to static mysqlclient library, though shared library works - mysql

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

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.

mysql.h compile, but not its functions

I am trying to see how to embed sql code in a C program, but I have an issue I am not able to understand when I compile this code :
#include <stdio.h>
#include <stdlib.h>
#include <mysql/mysql.h>
MYSQL *mysql;
MYSQL_RES *results;
MYSQL_ROW record;
int main() {
mysql = mysql_init(NULL);
if (mysql == NULL) {
fprintf(stderr, "%s\n", mysql_error(mysql));
return 1;
}
if (mysql_real_connect(mysql, "localhost", "root", "PassWord",
NULL, 0, NULL, 0) == NULL) {
fprintf(stderr, "%s\n", mysql_error(mysql));
mysql_close(mysql);
return 1;
}
mysql_query(mysql, "SHOW DATABASES");
return 0;
}
this is what the compiler tell me when I compile it :
clang++ -g -c testSql.cc
clang++ testSql.o -o testSql
/usr/bin/ld: testSql.o: in function `main':
/home/antoine/Documenti/L3/Information Management II/code/testSql.cc:12: undefined reference to `mysql_init'
/usr/bin/ld: /home/antoine/Documenti/L3/Information Management II/code/testSql.cc:15: undefined reference to `mysql_error'
/usr/bin/ld: /home/antoine/Documenti/L3/Information Management II/code/testSql.cc:19: undefined reference to `mysql_real_connect'
/usr/bin/ld: /home/antoine/Documenti/L3/Information Management II/code/testSql.cc:21: undefined reference to `mysql_error'
/usr/bin/ld: /home/antoine/Documenti/L3/Information Management II/code/testSql.cc:22: undefined reference to `mysql_close'
/usr/bin/ld: /home/antoine/Documenti/L3/Information Management II/code/testSql.cc:26: undefined reference to `mysql_query'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:25: testSql] Errore 1
I have check in the mysql.h file, those function are implemented, so I do not understand why I have this "undifined reference" error, do any onehave an idea on the origin of this error ?
Thank you all for the answer. I had to provide a linker in my compilation.
I am now compiling with
clang++ -g -c testSql.cc
clang++ testSql.o -o testSql `mysql_config --cflags --libs`
instead of
clang++ -g -c testSql.cc
clang++ testSql.o -o testSql
and it compile correctly.
Thank you !

Embedded MariaDB C/C++ API

I'm trying to get an embedded MariaDB (i.e. not connecting to running server) setup going but I'm failing to get any of the examples I find to work.
The most recent example I have is from this post https://stackoverflow.com/a/24548826/400048
When the app runs it produces:
Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
The docs https://mariadb.com/kb/en/library/embedded-mariadb-interface/ isn't much help on this.
For convenience the code from that StackOverflow post is:
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv) {
static char *server_options[] = {
"mysql_test", // An unused string
"--datadir=/tmp/mysql_embedded_data", // Your data dir
NULL };
int num_elements = (sizeof(server_options) / sizeof(char *)) - 1;
static char *server_groups[] = { "libmysqld_server",
"libmysqld_client", NULL };
// Init MySQL lib and connection
mysql_library_init(num_elements, server_options, server_groups);
MYSQL *con = mysql_init(NULL);
if (con == NULL) {
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
mysql_options(con, MYSQL_READ_DEFAULT_GROUP, "libmysqld_client");
mysql_options(con, MYSQL_OPT_USE_EMBEDDED_CONNECTION, NULL);
// Connect to no host/port -> Embedded mode
if (mysql_real_connect(con, NULL, NULL, NULL, NULL, 0, NULL, 0) == NULL) {
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
// Create a sample empty DB, named "aNewDatabase"
if (mysql_query(con, "CREATE DATABASE aNewDatabase")) {
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
// Close connection
mysql_close(con);
exit(0);
}
I had a cursory look at https://github.com/MariaDB/server but didn't know where to really look...or in fact what I was looking for.
How does one go about getting an embedded mariadb going?
I'm running on Mac OS High Sierra, MariaDB was installed with brew install mariadb --with-embedded.
UPDATE:
I'm fairly certain I'm linking to the correct lib.
ls /usr/local/lib | grep maria
FIND_LIBRARY(mariadb mariadb)
MESSAGE(FATAL_ERROR "BOOM ${mariadb}")
Output of which is:
BOOM /usr/local/lib/libmariadb.dylib
UPDATE 2
I'm now linking to the following. Note that I started with just libmysqld and added libraries until all link errors went away. The trouble here is I may not have all the correct libs or versions.
TARGET_LINK_LIBRARIES(sql_fn /usr/local/lib/libmysqld.a)
TARGET_LINK_LIBRARIES(sql_fn /usr/local/opt/openssl/lib/libcrypto.a)
TARGET_LINK_LIBRARIES(sql_fn /usr/local/opt/openssl/lib/libssl.a)
TARGET_LINK_LIBRARIES(sql_fn /usr/local/opt/bzip2/lib/libbz2.a)
TARGET_LINK_LIBRARIES(sql_fn /usr/local/lib/liblz4.a)
TARGET_LINK_LIBRARIES(sql_fn /usr/local/opt/zlib/lib/libz.a)
TARGET_LINK_LIBRARIES(sql_fn /usr/local/opt/xz/lib/liblzma.a)
TARGET_LINK_LIBRARIES(sql_fn /usr/local/lib/libsnappy.a)
It now compiles but exits with code 6
Process finished with exit code 6
Looking at https://stackoverflow.com/a/7495907/400048 if it's points to the same thing/is still true then exit code 6 means EX_ILLEGAL_TABLE 6, unfortunately I don't know what table that would be. The mysql_test and datadir strings passed in are valid identifiers/path.
Ok first a little explanation about your example. The user is using gcc to compile and I can see you are using cmake.
First what does -lz and mysql_config --include --libmysqld-libs means. The first is link zlib to link zlib in cmake you can refer to this answer, but long story short:
find_package( ZLIB REQUIRED )
if ( ZLIB_FOUND )
include_directories( ${ZLIB_INCLUDE_DIRS} )
target_link_libraries( sql_fn ${ZLIB_LIBRARIES} )
endif( ZLIB_FOUND )
Then you need the mariaDB library and that is the second part. mysql_config --include --libmysqld-libs this means execute the command mysql_config --include --libmysqld-libs which will return a string with the link options so execute the command:
$mysql_config --include --libmysqld-libs
-I/usr/local/mysql/include
-L/usr/local/mysql/lib -lmysqld
And you should get an output like the one above. The -I is to look for headers in a given directory and the -L is to search a library in a directory, the -l is to link a given library it serves the same purpose as -lz only you are adding -lmysqld.
Well now that all is explained you need only include the -I -L and -l options with mysql however this is not such a standard library so you need to include directories and libraries through a script as explained in this anwer. So again long story short there is no bullet proof for this for example my library is in /usr/local/mysql/lib and yours is in /usr/local/lib. Since that is the case it will be easier to use the second method.
execute_process(COMMAND mysql_config --include
OUTPUT_VARIABLE MYSQL_INCLUDE)
execute_process(COMMAND mysql_config --libmysqld-libs
OUTPUT_VARIABLE MYSQL_LIBS)
target_compile_options(sql_fn PUBLIC ${MYSQL_INCLUDE})
target_link_libraries(sql_fn ${MYSQL_LIBS})
And that is all the required information you need. Now we are glad we have Cmake to make things easier for us don't we. ;)
Here is my CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(embedded_mysql)
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp)
add_executable(embedded_mysql ${SOURCE_FILES})
find_package( ZLIB REQUIRED )
if ( ZLIB_FOUND )
include_directories( ${ZLIB_INCLUDE_DIRS} )
target_link_libraries( embedded_mysql ${ZLIB_LIBRARIES} )
endif( ZLIB_FOUND )
execute_process(COMMAND mysql_config --include
OUTPUT_VARIABLE MYSQL_INCLUDE)
execute_process(COMMAND mysql_config --libmysqld-libs
OUTPUT_VARIABLE MYSQL_LIBS)
string(STRIP ${MYSQL_LIBS} MYSQL_LIBS)
target_compile_options(embedded_mysql PUBLIC ${MYSQL_INCLUDE})
target_link_libraries(embedded_mysql ${MYSQL_LIBS})
You can see the code in github here

Using mysql in c programming

I installed ubuntu on a virtual machine. There, I installed mysql server sudo apt-get install mysql-server .This worked, because I could acces mysql-u root -p password
After that, I did : sudo apt-get install libmysqlclient-dev
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv)
{
printf("MySQL client version: %s\n", mysql_get_client_info());
exit(0);
}
When I compile this with
gcc version.c -o version `mysql_config --cflags --libs`
it works.
But when I compile this one from below
gcc createdb.c -o createdb -std=c99 `mysql_config --cflags --libs`
I get some errors.
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv)
{
MYSQL *con = mysql_init(NULL);
if (con == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
exit(1);
}
if (mysql_real_connect(con, "localhost", "root", "root_pswd",
NULL, 0, NULL, 0) == NULL)
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
if (mysql_query(con, "CREATE DATABASE testdb"))
{
fprintf(stderr, "%s\n", mysql_error(con));
mysql_close(con);
exit(1);
}
mysql_close(con);
exit(0);
}
Errors:
"Usage:: No such file or directory
[OPTIONS]: No such file or directory
Options:: No such file or directory
[-I/usr/include/mysql: No such file or directory
[-L/user/lib/x86_64-linux-gnu: No such file or directory
.
.
.
unrecognized command line option '--cflags'
unrecognized command line option '--libs'
.
.
unrecognized command line option '--socket'
unrecognized command line option '--port' "
Can someone explain me what I did wrong,and how to fix it?
I just want to get some data from tables in a C program.
I suspect you actually ran
gcc createdb.c -o createdb -std=c99 `mysql_config` --cflags --libs
rather than
gcc createdb.c -o createdb -std=c99 `mysql_config --cflags --libs`
That’s not going to work; mysql_config, if it doesn’t get any arguments, is going to print out a bunch of usage instructions, which will be passed to gcc, and then you’ll follow that with --cflags --libs, which gcc also doesn’t understand. gcc is severely confused and complains.
If you make sure those arguments get to mysql_config rather than gcc, everyone will be happy.

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