0

I have the following rule:

EXECS       = $(sort $(patsubst %.cpp,%$(EXESUFFIX), $(patsubst %.c,%$(EXESUFFIX), $(filter-out $(IGNORESRCS), $(EXECSRCS)))))
SRCS        = $(sort $(filter-out $(EXECSRCS), $(filter-out $(IGNORESRCS), $(wildcard *.c) $(wildcard *.cpp) $(foreach DIR,$(SUBDIRS),$(wildcard $(DIR)/*.cpp) $(wildcard $(DIR)/*.c) ) ))) 
#OBJS       = $(addprefix  $(OBJDIR), $(patsubst %.cpp,%$(OBJSUFFIX), $(patsubst %.c,%$(OBJSUFFIX), $(SRCS))))
OBJS        = $(patsubst %.cpp,%$(OBJSUFFIX), $(patsubst %.c,%$(OBJSUFFIX), $(SRCS)))
RESOURCE_SRCS= $(sort $(filter-out $(IGNORESRCS), $(wildcard *.rc) $(foreach DIR,$(SUBDIRS),$(wildcard $(DIR)/*.rc) ) ))
RESOURCES   = $(patsubst %.rc,%$(OBJSUFFIX), $(RESOURCE_SRCS)) 

%$(EXESUFFIX) : %.cpp $(LIBS) $(RESOURCES)
    $(CXX) $(DEFINES) $(CFLAGS) $(INCLUDES) $(LIBPATH) -o $(BINDIR)/$* $< $(RESOURCES) $(LIBINCLUDES)

The problem is that $(RESOURCES) doesnt exist for all platforms. The %$(EXESUFFIX) : %.cpp rule doesnt run, instead it tries to run g++ exec.cpp -o exec which as far as I can tell isnt a rule that I declared anywhere.

How do I get the rule to still build despite the fact that it is empty (and build the resources if it is not empty)?

1 Answer 1

1

If the variable is empty it has no effect on the rule. It should just work as written. What is the actual error you're seeing?

ETA:

Your question is very unclear in what, exactly, you mean by $(RESOURCES) doesn't exist. My answer was assuming you meant that the variable was empty. But given your comment below about how the makefile behaves, I now suspect what you mean is that the variable is still set to a list of files, but that those files are not present.

Because they're not there, and make doesn't know how to build them, make decides that this pattern rule cannot be used at all and it chooses a different rule.

If you want these files to only have any impact if they exist, then you can use the $(wildcard ...) function to expand only to those files that exist:

%$(EXESUFFIX) : %.cpp $(LIBS) $(wildcard $(RESOURCES))
        $(CXX) ...

One critical point here: the contents of $(RESOURCES) MUST be source files. They cannot be derived files (files that are supposed to be created by make). If they are derived, the situation is far more complex.

3
  • the rule simply isnt running, make is instead running a different default rule
    – chacham15
    Commented Nov 15, 2013 at 22:50
  • Could you edit your question to show us the other rule, and confirm that if the variable is not empty, then Make does run this rule? A minimal complete example would be even better.
    – Beta
    Commented Nov 16, 2013 at 0:47
  • I really like your hack fix, but the resource is not a source file but rather a derived file. I also updated the question to hopefully better answer your questions. Interestingly, I appended 'echo $(RESOURCES)' and it printed echo so $(RESOURCES) really is just empty. As for a minimal complete example, the original make is really complicated as is the directory/source structure so it would be very difficult to separate out the pieces. If it becomes necessary, I may do it though.
    – chacham15
    Commented Nov 21, 2013 at 2:15

Not the answer you're looking for? Browse other questions tagged or ask your own question.