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

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

    This is the Makefile that I use

    CC = gcc
    CFLAGS = -g -O2 -std=gnu99 -static -Wall -Wextra -Isrc -rdynamic -fomit-frame-pointer
    all: $(patsubst %.c, %.out, $(wildcard *.c))
    %.out: %.c Makefile
        $(CC) $(CFLAGS) $< -o $@ -lm
    clean:
        rm *.out                      
    

    You should paste it somewhere in your home and whenever you change the dirctory just copy it there. I use an alias in my ~/.basrc to copy it

    alias get_makefile_here='cp ~/Makefile ./'
    

    Simply press make and bam, you're done. Also notice the fact that once you're done with the old files it will not rebuild their executable.

提交回复
热议问题