Adding a dependency to make file c++ - mysql

I'm trying to connect my app to a MySQL database on Linux.
I compile my app using this command:
g++ -o mysqlconnect $(mysql_config --cflags) db.cpp $(mysql_config --libs)
and it works fine. But I can't seem to find a way to add $(mysql_config --cflags) and $(mysql_config --libs) to my make file. Is there any way?

If we are talking GNU Makefiles, the synthax is
g++ -o mysqlconnect $(shell mysql_config --cflags) db.cpp $(shell mysql_config --libs)
See the GNU make documentation.

Related

makefile CFLAGS ignore $(mysql_config --libs)?

I make a toy makefile example to test mysql, but the makefile does not recognize mysql_config.
this is the makefile script:
CFLAGS = -g -O2 -Wall -Wextra -Isrc -rdynamic $(OPTFLAGS)
LDLIBS = $(OPTLIBS)
SOURCES =$(wildcard *.c)
OBJECTS = asd
all: LDLIBS += $(mysql_config --libs_r) -lm
CFLAGS += -Isrc $(mysql_config --cflags)
all: $(OBJECTS)
When i run make all, it only execute:
cc -g -O2 -Wall -Wextra -Isrc -rdynamic -Isrc asd.c -lm -o asd
Where did all the mysql CFLAGS and LDLIBS go? Or is there something wrong with my script?
this returns when i type 'mysql_config --cflags' in the shell, for demonstration:
-I/usr/include/mysql
The content $(mysql_config --libs_r) is intended to ask the shell to invoke that command and replace the string with its output.
But, make uses the $(...) syntax to expand variables. So, your attempt at running a shell command mysql_config --libs_r is actually being interpreted as expanding a make variable named mysql_config --libs_r, of which there is not one, and so you get an empty string here.
You need to escape the $(...) syntax from make so that it's passed to the shell.
Also, your indentation seems to imply you want both LDLIBS and CFLAGS to be target-specific variables on the all target, however if that's really what you want you have to use a backslash at the end of the first line. Simply indenting the line doesn't make it a continuation of the previous line.
You want this:
all: LDLIBS += $$(mysql_config --libs_r) -lm \
CFLAGS += -Isrc $$(mysql_config --cflags)
There are some efficiency issues with this as it will run mysql_config twice for every compile and link operation. Much more efficient would be something like:
mysql_LIBS := $(shell mysql_config --libs_r)
mysql_FLAGS := $(shell mysql_config --cflags)
then use the make variables $(mysql_LIBS) and $(mysql_FLAGS)

How to create a shared object library (*.so) with mysql connector?

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).

How to setup Netbeans for MySQL programming in C

I am trying to develop an application in C in a target Linux system which requires Mysql Conectivity but i dont know where to include in NETBEANS the required directives for the libmysqlclient-dev library.
I have the following:
A laptop with NETBEANS IDE 8.0.2 and remote build host setup.
A remote Ubuntu linux target which is the remote build host for netbeans.
apt-get install libmysqlclient-dev in the Ubuntu Target
mysql_config --libs gives:
-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -ldl
mysql_config --libs gives:
-L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -ldl
According to the Mysql C Api Building I have to include the following:
gcc -c `mysql_config --cflags` progname.c
gcc -o progname progname.o `mysql_config --libs`
Although I am able to build my program manually in the target system, I am not sure where to add the above information in Netbeans.
P.S.1 at the momment my Netbeans build command looks like this:
gcc -o dist/Debug/GNU-Linux-x86/arguments_1 build/Debug/GNU-Linux-x86/src/args.o
P.S.2 Please be gentle. I am a newbie with Netbeans, Remote builds, C and Linux development.
Ok I have managed to get it working.
First I needed to include the Mysql Library paths to the Netbeans makefile as per this post:
gcc wont compile and run MySQL C libraries
# These are the flags that gcc requires in order to link correctly against our installed
# client packages
MYSQL_LIBS := $(shell mysql_config --cflags --libs)
Then right click on my project node , select Properties->Build->Linker->Compilation Line->Additional Options and add $(MYSQL_LIBS) to the Additional options parameter.
My problem was that I was adding it into the C compiler Additional options parameter.
But this post helped to clarify the order:
Why does the order in which libraries are linked sometimes cause errors in GCC?
So now my Netbeans gcc command looks like:
gcc -c -g -MMD -MP -MF "build/Debug/GNU-Linux-x86/src/args.o.d" -o build/Debug/GNU-Linux-x86/src/args.o src/args.c
gcc -o dist/Debug/GNU-Linux-x86/arguments_1 build/Debug/GNU-Linux-x86/src/args.o -I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -g -DNDEBUG -L/usr/lib/x86_64-linux-gnu -lmysqlclient -lpthread -lz -lm -ldl
Thank you Lumi, Thanassis

Compiling a code with mysql_config and pkg-config

recently I've been learning how to program GTK+ applications and MySQL in C programming language.
I wanted to try writing a program with both of those libs at once, I've done it but I can't compile it...
When I compile a program which uses MySQL I do this:
gcc exa_7.c -o exa_7 -std=c99 `mysql_config --cflags --libs`
and when I compile GTK+ application I do this:
gcc -o test_5 test_5.c `pkg-config --libs --cflags gtk+-2.0`
I can't write both of them, I mean pkg-config and mysql_config at once, because compiler won't accept it. I've been trying to find some makefile scripts but haven't found anything usefull. Please help. Thanks.
You're supposed to do this:
gcc exa_7.c -o exa_7 -std=c99 `mysql_config --cflags --libs` `pkg-config --libs --cflags gtk+-2.0`
If the backticks are causing you problems, you can run the programs using $() instead:
gcc exa_7.c -o exa_7 -std=c99 $(mysql_config --cflags --libs) $(pkg-config --libs --cflags gtk+-2.0)

makefile error ----> make: *** No rule to make target `mysql.h'

This might sound like a dumb question. But here goes..... I am using a C program called db_access.c which interacts with MySQL (in Ubuntu 10.10 with MySQL Server version: 5.1.49-1ubuntu8.1 (Ubuntu)). Inside the program, I have: include "mysql.h"
When I do the following, everything works out right:
gcc -I/usr/include/mysql db_access.c -lmysqlclient -o db_access
./db_access
Problem arises when I try to integrate it into an existing (and working makefile). The contents of the makefile:
all: MappingServer
#Macro definitions
CC = gcc
CFLAGS = -lm
INCLUDES = -I/usr/include/mysql
LIBS = -L/usr/lib/mysql -lmysqlclient
MappingServer.o: MappingServer.c map_registration.h
$(CC) $(CFLAGS) -c MappingServer.c
route_aggregation.o: route_aggregation.c map_registration.h
$(CC) $(CFLAGS) -c route_aggregation.c
db_access.o: db_access.c map_registration.h mysql.h
$(CC) $(CFLAGS) $(INCLUDES) -c db_access.c
MappingServer: MappingServer.o route_aggregation.o db_access.o
$(CC) $(LIBS) -o MappingServer MappingServer.o route_aggregation.o db_access.o
clean:
-rm MappingServer.o route_aggregation.o db_access.o
I have two other C programs, MappingServer.c and route_aggregation.c. These 3 files need to be compiled together. By the way, I also did:
root#ahuq-kitchen:/home/ahuq/MappingServer# mysql_config --cflags
-I/usr/include/mysql -DBIG_JOINS=1 -fno-strict-aliasing -DUNIV_LINUX -DUNIV_LINUX
and
root#ahuq-kitchen:/home/ahuq/MappingServer# mysql_config --libs
-Wl,-Bsymbolic-functions -rdynamic -L/usr/lib/mysql -lmysqlclient
So I think the paths are OK. When I do: make all
I get:
root#ahuq-kitchen:/home/ahuq/MappingServer# make all
gcc -lm -c MappingServer.c
gcc -lm -c route_aggregation.c
route_aggregation.c: In function ‘vtysh_input’:
route_aggregation.c:602: warning: function returns address of local variable
make: *** No rule to make target `mysql.h', needed by `db_access.o'. Stop.
Why is this happening?
the line
db_access.o: db_access.c map_registration.h mysql.h
tells make that db_access.o depends on db_access.c, map_registration.h and mysql.h. make complains because mysql.h cannot be found in the current directory (it's in /usr/include/mysql).
see the question Makefile updated library dependency for how to specify libraries as dependencies in make
You put "mysql.h" as a dependency, but it's not in the current directory, so Make thinks it needs to build it, but doesn't know how.
try to remove all the lines like:
MappingServer.o: MappingServer.c map_registration.h
if the map_registration.h is included in the c file, make is smart enough to find it. The only thing to be noticed may be to set the search file path using: -I.