Search code examples
mysqlcmakefile

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

Solution

  • 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 write the target multiple times; there can only be one target-specific variable assignment per line.

    You want this:

    all: LDLIBS += $$(mysql_config --libs_r) -lm
    all: 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)