简体   繁体   English

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

[英]How to expand makefile variable in prerequisites list?

I am having an issue defining a prerequisites for my targets while using file lists in variables the problem is as follows: 我在使用变量中的文件列表时定义目标的先决条件时遇到问题,问题如下:

in my makefile: 在我的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 $@ $<

Running it (as dry run) I will get: 运行它(作为空运行),我将得到:

$ 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 will not expand and will always keep the first source file ( HelloC.cpp ) as a parameter while the second one is defined with prefix. problematic line 1行将不会扩展,并且始终将第一个源文件( HelloC.cpp )作为参数,而第二个源文件则用前缀定义。 How can I handle this so that it compiles? 我该如何处理以便编译? I would like to have source files in root dir and object files in obj_c and obj_s directories 我想在根目录中obj_c文件,在obj_cobj_s目录中有目标文件

EDIT: I originally answered the wrong question in haste, sorry about that. 编辑:我最初是匆忙回答了错误的问题,对此感到抱歉。 Anyway, the static pattern rule is the way to go, but you have to factor in the prefix. 无论如何,静态模式规则是可行的方法,但是您必须考虑前缀。 Instead of 代替

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

Use 采用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM