You need a Makefile that would compile only those files that are specified in the SRCS variable.

For example, there is the following file structure:

 test/ a.cpp b.cpp c.cpp d.cpp ... Makefile 

Makefile :

 SRCS = a.cpp c.cpp ... OBJS = $(SRCS:.cpp=.o) ??? 

What to write next so that each file from SRCS compiled into the corresponding object file from OBJS . Not all .cpp files, but only selected ones.

The variable SRCS may change over time. And this is necessary not only for compiling .cpp files to object .o , but also for other tasks.

  • one
    The second question, submitted as an “add-on,” is best asked separately. - aleksandr barakin

1 answer 1

minimum one line is enough:

 SRCS = a.cpp c.cpp OBJS = $(SRCS:.cpp=.o) all: $(OBJS) 

then, if there are a.cpp and c.cpp files in the current directory, the result will be:

 $ make g++ -c -o ao a.cpp g++ -c -o co c.cpp 

and in the current directory the files “ ao and “ co ” will appear.

details about the applied "magic" - here .


And this is necessary not only for compiling .cpp files to object .o , but also for other tasks.

When there are concretized new tasks with which there will be difficulties, ask new questions. There is no general answer “for all occasions”.

  • Where to specify --std=c++14 ? - pank
  • one
    Is this a compiler option? add it to a variable, for example, CPPFLAGS : CPPFLAGS+=--std=c++14 - aleksandr barakin