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.
Related
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 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'd like to test the very simple C API connection with my MySQL server. And I'm using it on mac. So Here are the codes:
#include <stdio.h>
#include <mysql.h>
int main(void)
{
printf("MySQL client version: %s\n", mysql_get_client_info());
exit(0);
}
I use gcc to compile it
gcc -c `mysql_config --cflags` main.c
It succeeds. But when I tried to run it
./main.o
The error is like this
-bash: ./main.o: Permission denied
I can log in to the MySQL by anonymous and run select version():
shell>mysql
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.6.25 |
+-----------+
1 row in set (0.00 sec)
Thanks to Sean, there is a mistake in my compilation. It supposes to be
gcc -o main `mysql_config --cflags` main.c `mysql_config --libs`
After that when I run ./main
I get another error:
dyld: Library not loaded: libmysqlclient.18.dylib
Thanks everyone!
I've solved this by adding the path of libmysqlclient.dylib to the DYLD_LIBRARY_PATH environment variable.
Here is the command for someone may need it.
export DYLD_LIBRARY_PATH=/usr/local/mysql/lib
The -c argument to gcc means:
Compile or assemble the source files, but do not link.
Which means you are not producing a valid executable. You need an additional step:
gcc -c `mysql_config --cflags` main.c
gcc -o main main.o `mysql_config --libs`
This can also be combined into a single line:
gcc -o main `mysql_config --cflags` main.c `mysql_config --libs`
Then you should be able to run ./main
Your compilation ststement looks wrong.
gcc -c `mysql_config --cflags` main.c
does not produce an executable binary. -c option is used to produce the object file, not an executable. It stops after compilation, linking is not done.
Rather, to generate the binary, you should write
gcc main.c -o outfile <any other oprions>
where the -o <file> is used to place the output (binary) to <file>and run the outfile like
./outfile
I have the cuda 4.0 toolkit installed in my Ubuntu 12.04 server. When I am trying to build a cuda code present in Rodinia benchmark suite. I was getting the following error. i.e, after running the make command
ncclab#slave13:~/Downloads/rodinia_2.4$ make
cd cuda/cfd; make; cp euler3d euler3d_double pre_euler3d pre_euler3d_double /home/ncclab/Downloads/rodinia_2.4/bin/linux/cuda
make[1]: Entering directory `/home/ncclab/Downloads/rodinia_2.4/cuda/cfd'
nvcc -O2 -Xptxas -v --gpu-architecture=compute_20 --gpu-code=compute_20 euler3d.cu -o euler3d -I/if10/kw5na/NVIDIA_GPU_Computing_SDK4/C/common/inc -L/if10/kw5na/NVIDIA_GPU_Computing_SDK4/C/lib
euler3d.cu:35: warning: #warning "the kernels may fail too launch on some systems if the block length is too large"
euler3d.cu:35: warning: #warning "the kernels may fail too launch on some systems if the block length is too large"
By here the euler3d was built
nvcc -Xptxas -v -O3 --gpu-architecture=compute_20 --gpu-code=compute_20 euler3d_double.cu -o euler3d_double -I/if10/kw5na/NVIDIA_GPU_Computing_SDK4/C/common/inc -L/if10/kw5na/NVIDIA_GPU_Computing_SDK4/C/lib
/tmp/tmpxft_0000532d_00000000-12_euler3d_double.o: In function `main':
tmpxft_0000532d_00000000-1_euler3d_double.cudafe1.cpp:(.text+0x22d9): undefined reference to `cutCreateTimer'
tmpxft_0000532d_00000000-1_euler3d_double.cudafe1.cpp:(.text+0x22f7): undefined reference to `cutStartTimer'
tmpxft_0000532d_00000000-1_euler3d_double.cudafe1.cpp:(.text+0x2457): undefined reference to `cutStopTimer'
tmpxft_0000532d_00000000-1_euler3d_double.cudafe1.cpp:(.text+0x246c): undefined reference to `cutGetAverageTimerValue'
collect2: ld returned 1 exit status
make[1]: *** [euler3d_double] Error 1
make[1]: Leaving directory `/home/ncclab/Downloads/rodinia_2.4/cuda/cfd'
cp: cannot stat `euler3d_double': No such file or directory
cp: cannot stat `pre_euler3d': No such file or directory
cp: cannot stat `pre_euler3d_double': No such file or directory
make: *** [CUDA] Error 1
By searching I came to know that there may be a problem with libcutil
But I had the libcutil_x86_64.a in ~/NVIDIA_GPU_Computing_SDK/C/lib
Edit: The contents of various folders are
ncclab#slave13:~/NVIDIA_GPU_Computing_SDK/C/lib$ ls
libcutil_x86_64.a libparamgl_x86_64.a librendercheckgl_x86_64.a
~/Downloads/rodinia_2.4/cuda/cfd$ ls
euler3d euler3d_double.cu Makefile~ pre_euler3d.cu README
euler3d.cu Makefile Makefile_nvidia pre_euler3d_double.cu run
ncclab#slave13:~/NVIDIA_GPU_Computing_SDK/C/common/inc$ ls
bank_checker.h cutil_inline.h GL nvVector.h stopwatch_base.h
cmd_arg_reader.h cutil_inline_runtime.h multithreading.h nvWidgets.h stopwatch_base.inl
cuda_drvapi_dynlink.c cutil_math.h nvGlutWidgets.h paramgl.h stopwatch.h
cutil_gl_error.h dynlink nvGLWidgets.h param.h stopwatch_linux.h
cutil_gl_inline.h dynlink_d3d10.h nvMath.h rendercheck_d3d10.h
cutil.h dynlink_d3d11.h nvMatrix.h rendercheck_d3d11.h
cutil_inline_bankchecker.h error_checker.h nvQuaternion.h rendercheck_d3d9.h
cutil_inline_drvapi.h exception.h nvShaderUtils.h rendercheck_gl.h
the partial contents of the makefile are
all: euler3d euler3d_double pre_euler3d pre_euler3d_double
euler3d: euler3d.cu
nvcc -O2 -Xptxas -v --gpu-architecture=compute_20 --gpu-code=compute_20 euler3d.cu -o euler3d -I$(CUDA_SDK_PATH)/common/inc -L$(CUDA_SDK_PATH)/lib $(CUTIL_LIB)
euler3d_double: euler3d_double.cu
nvcc -Xptxas -v -O3 --gpu-architecture=compute_20 --gpu-code=compute_20 euler3d_double.cu -o euler3d_double -I$(CUDA_SDK_PATH)/common/inc -L$(CUDA_SDK_PATH)/lib $(CUTIL_LIB)
pre_euler3d: pre_euler3d.cu
nvcc -Xptxas -v -O3 --gpu-architecture=compute_20 --gpu-code=compute_20 pre_euler3d.cu -o pre_euler3d -I$(CUDA_SDK_PATH)/common/inc -L$(CUDA_SDK_PATH)/lib $(CUTIL_LIB)
pre_euler3d_double: pre_euler3d_double.cu
nvcc -Xptxas -v -O3 --gpu-architecture=compute_20 --gpu-code=compute_20 pre_euler3d_double.cu -o pre_euler3d_double -I$(CUDA_SDK_PATH)/common/inc -L$(CUDA_SDK_PATH)/lib $(CUTIL_LIB)
The euler3d has been successfully built. The errors are occurring for all the remaining three files.So I don't think there is any problems in the directories they are referring to
I really don't understand why the undefined reference to `cutCreateTimer' and others are occurring. But searching in SE I came to know that when there is any problem with cutil.h or libcutil these problems exits. I was a newbie of linux environment. So please help me
Edit 2: In a include file named make.config the following lines were present
# CUDA SDK installation path
#SDK_DIR = $(HOME)/NVIDIA_GPU_Computing_SDK/C
SDK_DIR =/if10/kw5na/NVIDIA_GPU_Computing_SDK4/C
#SDK_DIR =/if10/kw5na/NVIDIA_CUDA_Computing_SDK4/C
Here by default the 2nd option was selected and the euler3d was built. So I thought there may be a problem because of this. But changing it does not change the problem I was facing
and about cutil library present in make file
CUDA_SDK_PATH := $(SDK_DIR)
# Determine the correct version of the cutil library
CUTIL_LIB = # -lcutil
ifeq ($(shell uname -m), x86_64)
ifeq ($(shell if test -e $(SDK_DIR)/lib/libcutil_x86_64.a; then echo T; else echo F; fi), T)
CUTIL_LIB = #-lcutil_x86_64
endif
endif
Which I think was correct
The problem is that your compile command is specifying the library path, but not the library itself that is providing the cut... functionality.
This is apparently the makefile line that is generating the nvcc compile command for euler3d_double:
euler3d_double: euler3d_double.cu
nvcc -Xptxas -v -O3 --gpu-architecture=compute_20 --gpu-code=compute_20 euler3d_double.cu -o euler3d_double -I$(CUDA_SDK_PATH)/common/inc -L$(CUDA_SDK_PATH)/lib $(CUTIL_LIB)
This is the generated compile command:
nvcc -Xptxas -v -O3 --gpu-architecture=compute_20 --gpu-code=compute_20 euler3d_double.cu -o euler3d_double -I/if10/kw5na/NVIDIA_GPU_Computing_SDK4/C/common/inc -L/if10/kw5na/NVIDIA_GPU_Computing_SDK4/C/lib
You'll note that the last two tokens in the makefile line are:
-L$(CUDA_SDK_PATH)/lib $(CUTIL_LIB)
The -L$... token is getting converted to the correct library path. But the $(CUTIL_LIB) token should be getting converted to something like -lcutil or perhaps -lcutil_x86_64, but that is not happening. As a result, the library that provides the cut...Timer... functions is missing, and so those appear to be undefined.
I would inspect the Makefile carefully to see if there are any end-of-line characters or other oddities that are different between the line that specifies how euler3d_double should be built and the previous lines that specify how euler3d should be built.
You should also be able to confirm this by looking at the nvcc compile command line generated for euler3d (which you don't show) and comparing it to the one for euler3d_double (which is failing).
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.