minimum c++ make file for linux

后端 未结 11 1880
眼角桃花
眼角桃花 2020-12-23 17:32

I\'ve looking to find a simple recommended \"minimal\" c++ makefile for linux which will use g++ to compile and link a single file and h file. Ideally the make file will not

11条回答
  •  渐次进展
    2020-12-23 18:17

    I was hunting around for what a minimal Makefile might look like other than

    some_stuff:
        @echo "Hello World"
    

    I know I am late for this party, but I thought I would toss my hat into the ring as well. The following is my one directory project Makefile I have used for years. With a little modification it scales to use multiple directories (e.g. src, obj, bin, header, test, etc). Assumes all headers and source files are in the current directory. And, have to give the project a name which is used for the output binary name.

    NAME = my_project
    
    FILES = $(shell basename -a $$(ls *.cpp) | sed 's/\.cpp//g')
    SRC = $(patsubst %, %.cpp, $(FILES))
    OBJ = $(patsubst %, %.o, $(FILES))
    HDR = $(patsubst %, -include %.h, $(FILES))
    CXX = g++ -Wall
    
    %.o : %.cpp
            $(CXX) $(HDR) -c -o $@ $<
    
    build: $(OBJ)
            $(CXX) -o $(NAME) $(OBJ)
    
    clean:
            rm -vf $(NAME) $(OBJ)
    

提交回复
热议问题