简体   繁体   中英

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:

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

but it includes all the files from my src directory. Say I have some file (eg "x.cpp") that I don't want as a part of my compilation. 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?

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 $@ $<

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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