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
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 have written a very simple application in C, for which I would like to have a connection with MySQL database. Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <mysql.h>
#include <my_global.h>
void runtime_tile_opt(void);
int main(void) {
runtime_tile_opt();
return 0;
}
void runtime_tile_opt() {
printf("MySQL client version: %s\n", mysql_get_client_info());
}
If i run gcc -o runtime $(mysql_config --cflags) runtime.c $(mysql_config --libs), everything works fine, all libraries seemed to be properly linked, and if I run the executable, I get a legitimate-looking output:
MySQL client version: 5.5.49
However, I would like to make a shared library out of it. So I'm trying to create an object file, with -fPIC flag:
gcc -c -fPIC runtime.c $(mysql_config --cflags) -o runtime.o $(mysql_config --libs)
The outputs of mysql_config --libs mysql_config --cflags are:
-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -ldl
-I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -g -DNDEBUG
Finally, I create my .so:
gcc runtime.o -shared -o runtime.so
All of the above went with no errors whatsoever. However, some library is not linked apparently, since when I run a file with this plugin loaded, I get:
/home/michal/thesis/Drafts/runtime/runtime.so: undefined reference to `mysql_get_client_info'
This function is defined in the header files I included. I'm not very experienced with gcc, but I look at it and it looks as though I linked what I had to link. Any ideas where did I fail?
You still need to pass the correct -l flags to the compiler when creating a shared library. They should be the same flags you pass when creating a binary, i.e.
gcc -shared -o runtime.so $(mysql_config --cflags) runtime.o $(mysql_config --libs)
Note that the order of options is important! First pass all options, then all files and lastly libraries (-l... operands).
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'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);
}
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.