1

Previously I had a directive pictDBM that was something like this:

    all: pictDBM

    pictDBM : db_list.o db_delete.o db_utils.o error.o pictDBM.o 
          db_create.o dedup.o image_content.o pictDBM_tools.o db_read.o
                      gcc $(CFLAGS) -o $@ $^ $(LDLIBS)

This part worked very well.

Then I thought I wanted to be able to add an optional argument given from the command line. This option would then go into the gcc command line that I execute in the pictDBM subpart.

I approched the problem by using a OPTS="" variable that I declare in my code(at the beginning of the makefile before any directive). My aim is to call make like this: make all OPT='-g'.

Then I declared a variable base=gcc $(CFLAGS) -o $@ $^ $(LDLIBS)

Part of my solution was to use a if else construct to include or not my option variable ${OPT} to the ${base} gcc command that I execute in pictDBM.

I red this topic, that helped build my multi-line if else construct:

pictDBM : db_list.o db_delete.o db_utils.o error.o pictDBM.o 
    db_create.o dedup.o image_content.o pictDBM_tools.o db_read.o
        if [ ! -z "$$OPT" ];    \
        then    \
                echo "$$base $$OPT" ; \ #for debugging purposes
                eval "$$base $$OPT"  ;   \
        else    \
                eval "$$base" ;  \
        fi ;

When I simply execute make all everything seems to be ok with make, and the output program works well.

but when I execute make all OPTS='-g' I get the following output:

 -g
/bin/sh: 1: eval: -g: not found
makefile:16: recipe for target 'pictDBM' failed
make: *** [pictDBM] Error 127

you can see echo prints only -g, how can I fix this?

Community
  • 1
  • 1
Simonlbc
  • 591
  • 1
  • 4
  • 16

1 Answers1

2

You don't need the conditional or eval, just specify CFLAGS directly on the command line:

make all CFLAGS=-g

and override the variable in the makefile:

override CFLAGS += # Your local options

all: pictDBM

pictDBM: db_list.o db_delete.o db_utils.o error.o pictDBM.o \
          db_create.o dedup.o image_content.o pictDBM_tools.o db_read.o
    gcc $(CFLAGS) -o $@ $^ $(LDLIBS)

CFLAGS is kind of odd here however, you shouldn' t be passing compilation flags during linking.

Linking options can be specified with LDFLAGS; since pictDBM has pictDBM.o as a prerequisite, and you're already using LDLIBS correctly, you can rely on make's implicit linking rule, no recipe needed.

override LDFLAGS += # Your local options

pictDBM: db_list.o db_delete.o db_utils.o error.o pictDBM.o \
          db_create.o dedup.o image_content.o pictDBM_tools.o db_read.o

RE your comment, you need to specify override on the very first definition of a variable, otherwise it won't work:

override CFLAGS += $(shell pkg-config vips --cflags) -std=c99 -Wall
override LDLIBS += $(shell pkg-config vips --libs) -lm -lssl -lcrypto

pictDBM: db_list.o db_delete.o db_utils.o error.o pictDBM.o db_create.o dedup.o image_content.o pictDBM_tools.o db_read.o
user657267
  • 20,568
  • 5
  • 58
  • 77
  • I've done what you advise to do my make file now looks like this: `CFLAGS += -std=c99 -Wall`
    `CFLAGS += $$(pkg-config vips --cflags)`
    `LDLIBS += $$(pkg-config vips --libs) -lm -lssl -lcrypto`
    `override CFLAGS+=`
    `override LDFLAGS+=`
    `all : pictDBM`
    `pictDBM : db_list.o db_delete.o db_utils.o error.o pictDBM.o db_create.o`
    `dedup.o image_content.o pictDBM_tools.o db_read.o`
    but when I do `make all CFLAGS=-g`
    I get:
    – Simonlbc May 16 '16 at 10:19
  • `cc -g -c -o pictDBM.o pictDBM.c In file included from pictDB.h:32:0, from pictDBM.c:11: /usr/local/include/vips/vips.h:85:18: fatal error: glib.h: No such file or directory compilation terminated. : recipe for target 'pictDBM.o' failed make: *** [pictDBM.o] Error 1` What am I missing? – Simonlbc May 16 '16 at 10:19
  • Sorry the formatting is terrible.I tried to do the maximum for putting linebreaks. But through trial and failure it ended up looking like this :( – Simonlbc May 16 '16 at 10:29