I'm trying to learn how to write for MySQL in C, following the Zetcode tutorial on the subject and using GCC in Xubuntu 12.04 with a LAMP server installed and the MySQL development library installed using apt-get. Does anyone have suggestions?
I'm getting the error:
undefined reference to `mysql_get_client_info'
collect2: ld returned 1 exit status
using the compile line:
gcc -I/usr/include/mysql -std=c99 -o mysqlc99.cgi mysqlc99.c
with the following code:
#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);
}
Related
I'm practicing with the MySQL C API, because I need to do a project for the university. I have a problem after compiling with gcc on ubuntu.
I'm working with MariaDB on simple example like this:
#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 with GCC with this command:
$ gcc version.c -o version `mysql_config --cflags --libs`
I receive this error that i can't resolve:
/usr/bin/ld: cannot find -lssl
/usr/bin/ld: cannot find -lssl
collect2: error: ld returned 1 exit status
Can someone help me?
I want to run C programs with a connection to mariaDB, for that I have installed mariaDB version:
Server: MariaDB
Server version: 10.1.11-MariaDB MariaDB Server
Protocol version: 10
Connection: Localhost via UNIX socket
Server characterset: latin1
Db characterset: latin1
Client characterset: utf8
Conn. characterset: utf8
But I came through an article
So, i didn't install C connector (due to similar nature of mysql-mariaDB) and tried this sample program from this link http://zetcode.com/db/mysqlc/.
But it says:
[root#localhost Desktop]# gcc version.c -o version `mysql_config --cflags --libs`
bash: mysql_config: command not found...
version.c:1:23: fatal error: my_global.h: No such file or directory
#include <my_global.h>
^
compilation terminated.
Then I tried installing libmysqlclient-dev/libmariaclient-dev & it says no package libmysqlclient-dev/libmariaclient-dev available.
Now what should I do?
You need to find out which package provides you with mysql_config. From what I've checked you can do it by yum whatprovides mysql_config. In case of Arch I got:
[root#main]# pkgfile mysql_config
extra/libmariadbclient
Try installing MariaDB-devel package from MariaDB repository.
I my case, the compiler couldn't find my_global.h
I tried:
$ mysql_config --include
-I/usr/include/mysql
But, the file was under /usr/include/mysql/server/my_global.h
This worked:
gcc [......] `mysql_config --include`/server
Its a pretty old thread and this probably has been resolved. However, I was able to make it work after deploying:
yum install mariadb-devel
You should #include <mysql/mysql.h> after installing the developer package. You should not include <my_globals.h> directly.
You might also be interested in MySQL Connector/C Developer Guide and 23.8 MySQL C API in the Oracle docs.
$ sudo dnf install mariadb-devel
...
And then:
$ find /usr/include -name my_global.h
/usr/include/mysql/my_global.h
/usr/include/mysql/server/my_global.h
$ cat /usr/include/mysql/my_global.h
/* Do not edit this file directly, it was auto-generated by cmake */
#warning This file should not be included by clients, include only <mysql.h>
And finally:
$ find /usr/include -name mysql.h
/usr/include/mysql/server/mysql.h
/usr/include/mysql/mysql.h
And a test:
$ cat test.c
#include <mysql/mysql.h>
int main(int argc, char* argv[])
{
return 0;
}
And:
$ gcc -Wall test.c -o test.exe -lmysqlclient
I have mysql-devel installed. Relevant libraries live under /usr/inlcude/mysql
Here's my sample code to be compiled on Centos7:
#include <my_global.h>
#include <mysql/mysql.h>
int main(int argc, char **argv)
{
printf("MySQL client version: %s\n", mysql_get_client_info());
exit(0);
}
Error message:
Mysql_test.c:1:23: fatal error: my_global.h: No such file or directory
#include <my_global.h>
What is the problem? Should I create echos of all libraries under /usr/include/mysql to the primary path /usr/include?
You probably need to add mysql include to your compilation, as you suspected:
-I/usr/include/mysql
Then just
#include <mysql.h>
#include <my_global.h>
If you using gcc include like this :
#include <mysql/my_global.h>
If you use gcc dont need to specify -I
Depend where is mysql include dir . And where is located my_global.h
EDIT :
g++ -g -Wall -I/usr/local/include test2.o Test.o -o test
Soo add to you compiler this : -I/usr/local/include and include like this #include <mysql/my_global.h>
TO solve undefined reference you need to like to the library path . mysqlclient.a/.so
And add this too to link the library
g++ -g -Wall -I/usr/local/include -L/path_to_lib -lmysqlclient test2.o Test.o -o test
I have followed the tutorial here to build a simple nginx module with success: https://github.com/perusio/nginx-hello-world-module
I can build and run that code with no issue and it functions correctly.
I now want to add a dependancy of MySQL to the simple nginx module so I have used the MySQL C Connector http://dev.mysql.com/doc/refman/5.6/en/c-api-building-clients.html.
The simplest program you can write is this (taken from http://zetcode.com/db/mysqlc/):
mysql_test.c
#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);
}
You then compile like so:
$ gcc mysql_test.c -o mysql_test `mysql_config --cflags --libs`
This works fine.
Now, when I try to include the two header files my_global.h and mysql.h with my simple nginx hello-world module, I need a way of specifying those build flags, else it won't find those header files. At the moment, when I run make, after it successfully builds all other modules, I now get:
cc -c -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -I src/core -I src/event -I src/event/modules -I src/os/unix -I objs -I src/http -I src/http/modules -I src/mail \
-o objs/addon/nginx-hello-world-module/ngx_http_hello_world_module.o \
/vagrant/nginx-hello-world-module/ngx_http_hello_world_module.c
/vagrant/nginx-hello-world-module/ngx_http_hello_world_module.c:33:23: fatal error: my_global.h: No such file or directory
#include <my_global.h>
^
compilation terminated.
make[1]: *** [objs/addon/nginx-hello-world-module/ngx_http_hello_world_module.o] Error 1
make[1]: Leaving directory `/vagrant/nginx'
make: *** [build] Error 2
My question is: How do I get nginx to include those build flags when building my module?
I haven't found any information regarding the building of dependencies with Nginx.
Thanks!
Compilation Controls
--with-cc-opt=parameters
sets additional parameters that will be added to the CFLAGS variable.
--with-ld-opt=parameters
sets additional parameters that will be used during linking.
Source
For the MySQL Connector C it will be something like:
--with-cc-opt="-I/usr/include/mysql"
--with-ld-opt="-L/usr/lib64/mysql"
And in you module's config file:
CORE_LIBS="$CORE_LIBS -lmysqlclient"
During compilation it might also complain about _GNU_SOURCE being redefined and it will fail if you use -Werror without implementing a workaround.
I have a project that compiles and runs on my system but when I try to run it on server it is giving compile-time errors. Here are the portion of the Makefile I use to compile it on my system:
CC = gcc
CFLAGS = -g -Wall
COMPLILEFLAGS = `mysql_config --include`
LINKERINFO = `mysql_config --cflags --libs`
exec: main.o
$(CC) $(CFLAGS) -o exec main.o $(LINKERINFO)
main.o: main.c
$(CC) $(CFLAGS) $(COMPLILEFLAGS) -c main.c
When I run the same file on the server I get the following error:
gcc -g -Wall -I/usr/include/mysql -c main.c
In file included from main.c:13:
/usr/include/mysql/my_global.h:688: error: redefinition of typedef ‘my_bool’
/usr/include/mysql/mysql.h:55: note: previous declaration of ‘my_bool’ was here
In file included from main.c:13:
/usr/include/mysql/my_global.h:699: error: redefinition of typedef ‘my_socket’
/usr/include/mysql/mysql.h:69: note: previous declaration of ‘my_socket’ was here
In file included from main.c:13:
/usr/include/mysql/my_global.h:1102: error: redefinition of typedef ‘my_ulonglong’
/usr/include/mysql/mysql.h:132: note: previous declaration of ‘my_ulonglong’ was here
In file included from main.c:14:
/usr/include/mysql/my_getopt.h:64: warning: ‘enum loglevel’ declared inside parameter list
The main.c file is:
#include <stdio.h>
#include <mysql.h>
#include <my_global.h>
int main (void)
{
return 0;
}
Can anyone suggest a possible work around this problem. All I want is to increase the portability of this code. One option I can think of is to make the files compiler and linker are using within my source and use them for compiling and linking.
By the way I am using mysql-server version 5.5 and server has mysql-server version 5.3
Looking at MySQL's documentation I found that old versions included a warning that stated that my_global.h should be included before mysql.h for compiling on Windows.
Try reversing the order of the includes and see if that fixes the issue. If it does, a bug is to be filed with MySQL urgently.