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

前端 未结 4 1237
不知归路
不知归路 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

    A minimal Makefile that does what you want would be:

    #Tell make to make one .out file for each .cpp file found in the current directory
    all: $(patsubst %.cpp, %.out, $(wildcard *.cpp))
    
    #Rule how to create arbitary .out files. 
    #First state what is needed for them e.g. additional headers, .cpp files in an include folder...
    #Then the command to create the .out file, probably you want to add further options to the g++ call.
    %.out: %.cpp Makefile
        g++ $< -o $@ -std=c++0x
    

    You'll have to replace g++ by the compiler you're using and possibly adjust some platform specific setting, but the Makefile itself should work.

提交回复
热议问题