简体   繁体   English

如何使用Makefile(g ++)仅将某些文件链接到对象?

[英]How to link only some files to objects with Makefile (g++)?

I have a Makefile that compiles the source to object files, and it works: 我有一个Makefile将源文件编译为目标文件,并且可以正常工作:

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
    g++ $(FLAGS) $(INCLUDES) -c -o $@ $<

but it includes all the files from my src directory. 但它包括我src目录中的所有文件。 Say I have some file (eg "x.cpp") that I don't want as a part of my compilation. 假设我有一些我不想包含在编译中的文件(例如“ x.cpp”)。 I'd like to manually specify only some files to compile into objects. 我只想手动指定一些文件以编译成对象。 For example, 例如,

SRC = src/one.cpp src/two.cpp
OBJ = obj/one.o obj/two.o
$(OBJ_DIR)/%.o: $(SRC)
    g++ $(FLAGS) $(INCLUDES) -c -o $@ $<

I'd get one.o, two.o, but NOT xo Is this possible somehow? 我会得到one.o,two.o,但不是xo这有可能吗?

Define a list of modules, construct the list of objects you actually want: 定义模块列表,构造您实际想要的对象列表:

MODULES = one two
OBJECTS = $(addprefix ${OBJ_DIR}/, $(addsuffix .o, ${MODULES}))

all : ${OBJECTS}

Restrict the rule to those in the list: 将规则限制为列表中的规则:

${OBJECTS}: $(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
        g++ $(FLAGS) $(INCLUDES) -c -o $@ $<

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

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