mysql.h compile, but not its functions - mysql

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 !

Related

PTX kernel name mangling

I cannot link my Cuda program when a kernel is compiled from ptx file.
main.cu:
extern
__global__ void kernel(int, float*);
int main()
{
...
kernel<<<...>>>(...);
...
}
kernel.cu
__global__
void kernel(int n, float* p)
{
...
}
If I compile like below, I have no problems and I get an executable:
nvcc -dc main.cu kernel.cu --gpu-architecture=sm_70
nvcc -dlink main.o kernel.o --gpu-architecture=sm_70 -o dlink.o
g++ dlink.o main.o kernel.o -lcudart
If I compile like below (by generating ptx), I get errors:
nvcc -ptx kernel.cu --gpu-architecture=sm_70
nvcc -dc main.cu kernel.ptx --gpu-architecture=sm_70
nvcc -dlink main.o kernel.o --gpu-architecture=sm_70 -o dlink.o
g++ dlink.o main.o kernel.o -lcudart
Error:
main.o: In function `main':
tmpxft_0000b5ce_00000000-5_main.cudafe1.cpp:(.text+0x4789): undefined reference to `kernel(int, float*)'
tmpxft_0000b5ce_00000000-5_main.cudafe1.cpp:(.text+0x497e): undefined reference to `kernel(int, float*)'
collect2: error: ld returned 1 exit status
I am following an example from CUDA_Compiler_Driver_NVCC.pdf.
What do I need to do to fix the error?
(This is CUDA 10.2).
If you want to write your own PTX (or modify PTX), the proper CUDA methodology to use is the CUDA driver API and associated compilation flow.
The CUDA vectorAddDrv sample code has all the plumbing and workflow that you need.

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

Can not link libmysqlclient.a?

I'm using Ubuntu 14.04, and I installed libmysqlclient-dev package already. But I always get link error because mysql_init symbol missing.
My source code, Makefile, run-time result and symbol info are as follows:
igsrd#naivechou/~/project/m01/uuid_sign>cat main.cpp
#include <cstdio>
#include <cstdlib>
#include "mysql/mysql.h"
int main(int argc,char** argv)
{
auto con = mysql_init(nullptr);
//printf("mysql client version : %s\n",mysql_get_client_info());
exit(EXIT_SUCCESS);
}
igsrd#naivechou/~/project/m01/uuid_sign>LANG= && make -B
g++ -o main.o -c main.cpp -std=c++11 -ggdb
g++ -o uuid_sign -L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -ldl main.o
main.o: In function `main':
/home/igsrd/project/m01/uuid_sign/main.cpp:6: undefined reference to `mysql_init'
collect2: error: ld returned 1 exit status
make: *** [uuid_sign] Error 1
igsrd#naivechou/~/project/m01/uuid_sign>nm -C /usr/lib/x86_64-linux-gnu/libmysqlclient.a | grep mysql_init
0000000000002b10 T mysql_init
0000000000002d30 T mysql_init_character_set
U mysql_init_character_set
Source is very simple, just one line to call mysql_init().
Making process shows every options for compiler and linker, I think there are no missing options.
Error message is undefined reference of linking error, so I dump libmysqlclient.a to grep the mysql_init, and it is not on undefined state.
Now I really have no idea. What's wrong with this ?
Change your second command, try this (change the position of main.o):
g++ -o uuid_sign main.o -L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -ldl

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.

Can not compile simple mysql program

cblog.c:
#include "html.h"
#include "config.h"
#include <mysql/mysql.h>
//#include <my_global.h>
#include <stdio.h>
#include <stdlib.h>
int main() {
MYSQL *conn;
MYSQL_RES *res;
MYSQL_ROW row;
conn = mysql_init(NULL);
// Connect to database
if (!msyql_real_connect(conn, server, username, password, database, 0, NULL, 0)) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
// Query the DB
if (mysql_query(conn, "show tables")) {
fprintf(stderr, "%s\n", mysql_error(conn));
exit(1);
}
res = mysql_use_result(conn);
htmlf("MySQL Tables in mysql database:\n");
while ((row = mysql_fetch_row(res)) != NULL)
htmlf("%s \n", row[0]);
mysql_free_result(res);
mysql_close(conn);
html("Hello World!");
return 0;
}
Makefile:
all:
# gcc -Wall cblog.c html.c -o cblog `mysql_config --cflags --libs`
# gcc -Wall cblog.c html.c -L/usr/lib -lmysqlclient -o cblog
gcc cblog.c html.c -Wall $(shell mysql_config --cflags) -o cblog $(shell mysql_config --libs)
test: all
./cblog
Output:
ttouch cblog$ make
gcc cblog.c html.c -g -Wall -I/usr/include/mysql -fPIC -pipe -fstack-protector --param=ssp-buffer-size=4 -fno-strict-aliasing -DBIG_JOINS=1 -fomit-frame-pointer -g -DNDEBUG -o cblog -L/usr/lib -lmysqlclient -lpthread -lz -lm -lssl -lcrypto -ldl
cblog.c: In function ‘main’:
cblog.c:16:2: warning: implicit declaration of function ‘msyql_real_connect’ [-Wimplicit-function-declaration]
if (!msyql_real_connect(conn, server, username, password, database, 0, NULL, 0)) {
^
/tmp/ccs1WQsi.o: In function `main':
/media/files/Lab/cblog/cblog.c:16: undefined reference to `msyql_real_connect'
collect2: error: ld returned 1 exit status
make: *** [all] Error 1
I know this question has been answered over a thousand times, I've searched all over the internet, I've tested putting mysql_config at the beginning or at the end. I know that it gives the right paths (checked)
ls /usr/lib/ | grep mysql:
libmysqlclient.a
libmysqlclient_r.a
libmysqlclient_r.so
libmysqlclient_r.so.18
libmysqlclient_r.so.18.0.0
libmysqlclient.so
libmysqlclient.so.18
libmysqlclient.so.18.0.0
libmysqld.a
libmysqld.so
libmysqld.so.18
libmysqlservices.a
mysql
tdbcmysql1.0.0
ls /usr/include/mysql | grep mysql:
mysql_com.h
mysqld_ername.h
mysqld_error.h
mysql_embed.h
mysql.h
mysql_time.h
mysql_version.h
Sorry for the noobish question, but I've been banging my head for about 4-5 hours
You have a typo in the function name, msyql_real_connect should be mysql_real_connect