makefile CFLAGS ignore $(mysql_config --libs)? - mysql

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)

Related

Adding a dependency to make file c++

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.

How do you compile libssh, mysql and net-snmp in one Makefile for Ubuntu?

I'm writing a program to monitor a few network switches. Today I tried added the libssh library to the project, but can't seem to get it to compile. It seems like a simple thing, but I couldn't find the answer googling.
I was able to compile a test program with the command.
gcc libssh.c -lssh
I can't seem to get the -lssh to work in my makefile.
CC=gcc
CFLAGS=-I.
LIBS=`net-snmp-config --libs` `mysql_config --cflags --libs` `-lssh`
TARGET = snmpmon
$(TARGET): $(TARGET).c
$(CC) $(CFLAGS) -o $(TARGET) $(TARGET).c $(LIBS)
clean:
$(RM) $(TARGET)
I guessing it's something simple any suggestion?
gcc -I. -o snmpmon snmpmon.c `net-snmp-config --libs` `mysql_config --cflags --libs` `-lssh --libs`
/bin/sh: 1: -lssh: not found
/tmp/cc65Loeb.o: In function `GetMACinfo':
snmpmon.c:(.text+0x26): undefined reference to `ssh_new'
snmpmon.c:(.text+0x47): undefined reference to `ssh_options_set'
snmpmon.c:(.text+0x5c): undefined reference to `ssh_options_set'
snmpmon.c:(.text+0x71): undefined reference to `ssh_options_set'
snmpmon.c:(.text+0x7d): undefined reference to `ssh_free'
collect2: error: ld returned 1 exit status
Makefile:13: recipe for target 'snmpmon' failed
make: *** [snmpmon] Error 1
OK the issue was -lssh should be -lssh the makefile didn't like the extra ` marks.

crosstool-ng -- stdio.h: No such file or directory

I would like to cross compile a simple program that uses libncurses on my x86_64. Target system is MIPS.
My crosstool-ng-1.20 build went fine (with sample mips-unknown-elf)
However, a simple hello world ends badly.
#include <stdio.h>
int main(void)
{
printf("OH HAI.\n");
return 0;
}
--
x86host:~/toolchain$ mips-unknown-elf-gcc -o hello hello.c
hello.c:1:19: fatal error: stdio.h: No such file or directory
#include <stdio.h>
^
compilation terminated.
I'm clearly doing something terribly wrong here, but where do i start?
[Edit]
Thank you markgz. Codesourcery is exactly what i needed.
mips-linux-gnu-gcc.exe -static -o helloStatic hello.c
Proof of concept complete. Now off to compile my ncurses program.
[Switch:/]$ file /mnt/sd3/user/helloStatic
/mnt/sd3/user/helloStatic: ELF 32-bit MSB executable, MIPS, MIPS32 rel2 version 1,
statically linked, for GNU/Linux 2.6.16, with unknown capability
0x41000000 = 0xf676e75, not stripped
[Switch:/]$ uname -a
Linux Switch 2.6.32.59-cavium-octeon2.cge-cavium-octeon #1
SMP PREEMPT Fri May 10 11:48:14 PDT 2013 mips64 GNU/Linux
[Switch:/]$ /mnt/sd3/user/helloStatic
HOLIDAYS ARE COMING.
You probably needed to build mips-linux-elf-gcc. Are you sure your MIPS target system is configured as big-endian?
Anyway, you can avoid all these problems by downloading the free Mentor/Codesourcery MIPS gnu/gcc cross compilation tool chain from here. This toolchain is available for both Windows and Linux.
root#x86host:~/ncurses-5.9-20141101# export CC=mips-linux-gnu-gcc
root#x86host:~/ncurses-5.9-20141101# ./configure --target=mips-linux-gnu --host=mips-linux-gnu
[..]
root#x86host:~/ncurses-5.9-20141101# make
Then, i copied over a few headers from /usr/include/ to make these go away:
root#x86host:~/ninvaders-0.1.1# make
In file included from ./ncurses.h:1685:0,
from view.h:25,
from view.c:25:
./unctrl.h:54:20: fatal error: curses.h: No such file or directory
#include <curses.h>
^
compilation terminated.
make: *** [view.o] Error 1
root#x86host:~/ninvaders-0.1.1# make
mips-linux-gnu-gcc -static -c -I. -O -Wall globals.c
mips-linux-gnu-gcc -static -c -I. -O -Wall view.c
mips-linux-gnu-gcc -static -c -I. -O -Wall aliens.c
mips-linux-gnu-gcc -static -c -I. -O -Wall ufo.c
mips-linux-gnu-gcc -static -c -I. -O -Wall player.c
mips-linux-gnu-gcc -static -c -I. -O -Wall nInvaders.c
mips-linux-gnu-gcc -static -L /root/ncurses-5.9-20141101/lib -onInvaders globals.o view.o aliens.o ufo.o player.o nInvaders.o -lncurses
root#x86host:~/ninvaders-0.1.1# ls -l nInvaders
-rwxr-xr-x 1 root root 933003 Nov 6 16:18 nInvaders
root#x86host:~/ninvaders-0.1.1# mv nInvaders nInvaders_IOSXE_MIPS
For future googlers, Makefile is:
CC=mips-linux-gnu-gcc -static
CFLAGS=-O -Wall
LIBS=-lncurses
LDFLAGS=-L /root/ncurses-5.9-20141101/lib
CFILES=globals.c view.c aliens.c ufo.c player.c nInvaders.c
HFILES=globals.h view.h aliens.h ufo.h player.h nInvaders.h
OFILES=globals.o view.o aliens.o ufo.o player.o nInvaders.o
all: nInvaders
nInvaders: $(OFILES) $(HFILES)
$(CC) $(LDFLAGS) -o$# $(OFILES) $(LIBS)
.c.o:
$(CC) -c -I. $(CFLAGS) $(OPTIONS) $<
clean:
rm -f nInvaders $(OFILES)
End result:
root#x86host:~/ninvaders-0.1.1# file nInvaders_IOSXE_MIPS
nInvaders_IOSXE_MIPS: ELF 32-bit MSB executable, MIPS,
MIPS32 rel2 version 1, statically linked, for GNU/Linux 2.6.16, not stripped
Screenshots:
http://imgur.com/a/kf8cu
https://www.youtube.com/watch?v=-qbmKYQ2jCA

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.