如何在先决条件列表中扩展makefile变量?

时间:2014-12-23 15:11:12

标签: c++ variables makefile prerequisites

在使用变量中的文件列表时,我遇到了定义目标先决条件的问题,问题如下:

在我的makefile中:

... some basic defines
SOURCES=HelloC.cpp \
        HelloS.cpp \
        HelloI.cpp \
        main.cpp
SOURCES_CLIENT=Hello_Client_impl.cpp \
               HelloC.cpp
OBJECTS_SERVER_DIR=obj_s/
OBJECTS_CLIENT_DIR=obj_c/
OBJECTS_SERVER=$(addprefix $(OBJECTS_SERVER_DIR),$(SOURCES:.cpp=.o))
OBJECTS_CLIENT=$(addprefix $(OBJECTS_CLIENT_DIR),$(SOURCES_CLIENT:.cpp=.o))
EXECUTABLE_SERVER=server
EXECUTABLE_CLIENT=client

all: dirs server_exe client_exe

dirs:
  @echo create dirs
  $(CREATE_DIR) $(OBJECTS_SERVER_DIR)
  $(CREATE_DIR) $(OBJECTS_CLIENT_DIR)

server_exe: $(EXECUTABLE_SERVER)

client_exe: $(EXECUTABLE_CLIENT)

$(EXECUTABLE_SERVER): $(OBJECTS_SERVER)
  $(CXX) $^ $(LFLAGS) $(LIBS) -o $@

$(EXECUTABLE_CLIENT): $(OBJECTS_CLIENT)                                                                                                                                                                                                        
  $(CXX) $^ $(LFLAGS) $(LIBS) -o $@

# problematic line 1    
$(OBJECTS_SERVER): $(SOURCES)
  $(CXX) -c $(CPPFLAGS) -o $@ $<

# problematic line 2
$(OBJECTS_CLIENT): %.o : %.cpp
  $(CXX) -c $(CPPFLAGS) -o $@ $<

运行它(干运行)我会得到:

$ make -n
echo create dirs
mkdir -p obj_s/
mkdir -p obj_c/
g++ -c -Wall -I. -I/usr/include/ -I/usr/include/orbsvcs/ -I/usr/include/tao/ -I/usr/include/tao/PortableServer/ -o obj_s/HelloC.o HelloC.cpp
g++ -c -Wall -I. -I/usr/include/ -I/usr/include/orbsvcs/ -I/usr/include/tao/ -I/usr/include/tao/PortableServer/ -o obj_s/HelloS.o HelloC.cpp
g++ -c -Wall -I. -I/usr/include/ -I/usr/include/orbsvcs/ -I/usr/include/tao/ -I/usr/include/tao/PortableServer/ -o obj_s/HelloI.o HelloC.cpp
g++ -c -Wall -I. -I/usr/include/ -I/usr/include/orbsvcs/ -I/usr/include/tao/ -I/usr/include/tao/PortableServer/ -o obj_s/main.o HelloC.cpp
g++ obj_s/HelloC.o obj_s/HelloS.o obj_s/HelloI.o obj_s/main.o -L/usr/lib64/ -lTAO_PortableServer -lTAO_AnyTypeCode -lTAO -lACE -o server
make: *** No rule to make target `obj_c/Hello_Client_impl.cpp', needed by `obj_c/Hello_Client_impl.o'.  Stop.

problematic line 1将不会展开,并始终将第一个源文件(HelloC.cpp)保留为参数,而第二个源文件使用前缀定义。我如何处理它以便编译?我想在{dir}和obj_cobj_s目录中的目标文件中有源文件

1 个答案:

答案 0 :(得分:4)

编辑:我原本急忙回答错误的问题,对不起。无论如何,静态模式规则是要走的路,但你必须考虑前缀。而不是

$(OBJECTS_CLIENT): %.o : %.cpp

使用

$(OBJECTS_CLIENT): $(OBJECTS_CLIENT_DIR)%.o : %.cpp