Makefile that compiles all cpp files in a directory into separate executable

前端 未结 4 1234
不知归路
不知归路 2021-02-09 01:12

I am now studying C++. I want a makefile which will compile all of the cpp files in the current directory to separate executables. For example:

4条回答
  •  说谎
    说谎 (楼主)
    2021-02-09 01:42

    My answer builds on top of the answer by @Haatschii

    I don't prefer to have the .out prefix to my binaries. Also I used his existing Make syntax to perform clean as well.

    CXX=clang++
    CXXFLAGS=-Wall -Werror -std=c++11
    
    all: $(patsubst %.cpp, %.out, $(wildcard *.cpp))
    
    %.out: %.cpp Makefile
            $(CXX) $(CXXFLAGS) $< -o $(@:.out=)
    
    clean: $(patsubst %.cpp, %.clean, $(wildcard *.cpp))
    
    %.clean:
            rm -f $(@:.clean=)
    

提交回复
热议问题