开发者

Makefile sometimes ignores variable in recipe

开发者 https://www.devze.com 2023-03-08 04:29 出处:网络
I have the following Makefile CXX = g++ CXXFLAGS = -g -Wall COMPILE = ${CXX} ${CXXFLAGS} -c LINK = ${CXX} -lpthread

I have the following Makefile

CXX = g++
CXXFLAGS = -g -Wall
COMPILE = ${CXX} ${CXXFLAGS} -c
LINK = ${CXX} -lpthread
LIB_INC = -Ilib -Iwrappers -Iprocesses

src := $(wildcard lib/*.cpp) $(wildcard wrappers/*.cpp)
obj = $(src:.cpp=.o)

src_1 := processnetwork_part001.cpp sc_application_1.cpp
obj_1 = $(src_1:.cpp=.o)
src_2 := processnetwork_part002.cpp sc_application_2.cpp
obj_2 = $(src_2:.cpp=.o)

all : sc_application_1 sc_application_2
.PHONY : all

sc_application_1 : ${obj} ${obj_1}
    ${LINK} -o sc_application_1 $(obj) ${obj_1}

sc_application_2 : ${obj} ${obj_2}
    ${LINK} -o sc_application_2 $(obj) ${obj_2}

%.o : %.cpp %.h
    ${COMPILE} -o $@ $< $(LIB_INC)

clean :
    rm sc_application_1 sc_application_2 ${obj} ${obj_1} ${obj_2}

Where lib, wrappers and processes are subdirectories of the directory where the Makefile and the two main applications sc_application_1 and sc_application_2 are stored. When I run make, I get the following output (only the last few lines w/o compiler warnings).

g++ -g -Wall -c -o lib/Scheduler.o lib/Scheduler.cpp -Ilib -Iwrappers -Iprocesses
g++ -g -Wall -c -o wrappers/consumer_wrapper.o wrappers/consumer_wrapper.cpp -Ilib -Iwrappers -Iprocesses
g++ -g -Wall -c -o wrappers/generator_wrapper.o 开发者_如何学Cwrappers/generator_wrapper.cpp -Ilib -Iwrappers -Iprocesses
g++ -g -Wall -c -o wrappers/square_wrapper.o wrappers/square_wrapper.cpp -Ilib -Iwrappers -Iprocesses
g++ -g -Wall -c -o processnetwork_part001.o processnetwork_part001.cpp -Ilib -Iwrappers -Iprocesses
g++ -g -Wall   -c -o sc_application_1.o sc_application_1.cpp
In file included from wrappers/wrappers.h:4:0,
                 from sc_application_1.cpp:10:
wrappers/generator_wrapper.h:4:28: fatal error: ProcessWrapper.h: No such file or directory
compilation terminated.
make: *** [sc_application_1.o] Error 1

Compilation fails because for some reason that I don't understand, the variable LIB_INC isn't added anymore to

g++ -g -Wall   -c -o sc_application_1.o sc_application_1.cpp

But it is (as I intended) on all previous lines. Can anyone explain me this behaviour? Thank you.

edit: The error doesn't occur when I ommit the "%.h" in the "%.o" target.


I'm going to go out on a limb, and guess that there is no sc_application_1.h, but there is a header file for every previous source (e.g. Scheduler.h, consumer_wrapper.h ...).

Your %.o: %.cpp %.h rule doesn't apply if there is no %.h, so Make falls back on its default rule, which does not use LIB_INC. The simplest way to fix this is to add another %.o rule:

%.o : %.cpp %.h
    ${COMPILE} -o $@ $< $(LIB_INC)

%.o : %.cpp
    ${COMPILE} -o $@ $< $(LIB_INC)
0

精彩评论

暂无评论...
验证码 换一张
取 消