Alex Rivera | Logout

Makefile. How to exclude one particular file from compilation?

Asked 2011-03-04T03:00:48.947
9

I am trying to exclude main.cpp file from the list of files to be compiled defined by the rule below:

$(TMPDIRPATH)%.o: %.cpp
    @echo compile $<
ifneq ($(notdir $<), main.cpp)
        @$(COMPILE.cpp) $(OUTPUT_OPTION) $<
endif

This 'ifneq' condition always evaluates to true, which is bizarre. What am I doing wrong? Is there a better way to exlude one file from an explicit rule?

Edit
Report

1 Answer

5

The ifneq line is evaluated only once, when make starts up and parses the makefile. In that context, $< is empty.

To get different behavior for each of the targets matched by your pattern rule, you could do something like

$(TMPDIRPATH)%.o: %.cpp
    @echo compile $<
    @$(if $(filter main.cpp,$<),$(COMPILE.cpp) $(OUTPUT_OPTION) $<)

It might help you to think of the difference between ifneq and $(if) in a makefile as like the difference between #if and if() in C code.

Taking a step back, though: If you don't want main.cpp to be compiled by this rule, then you probably want to provide an explicit rule with $(TMPDIRPATH)main.o as its target, which will be preferred to the pattern rule always. Or, if you don't want$(TMPDIRPATH)main.o to get made at all, you should be looking for rules that have it on the right sight of the :, and removing it from there.

answered 2011-03-04T19:26:43.397

Your Answer