Makefile to compile multiple C programs?

前端 未结 8 1068
予麋鹿
予麋鹿 2020-12-02 09:41

This is an incredibly simple question, but I\'m new to makefiles. I am trying to make a makefile that will compile two independent programs:

program1:
    gc         


        
相关标签:
8条回答
  • 2020-12-02 10:28
    SRC = a.cpp b.cpp
    BIN = $(patsubst %.cpp,%,$(SRC))
    
    all: $(BIN)
    
    clean:
        rm -f $(BIN)
    
    .PHONY: all clean
    

    make all will do:

    c++     a.cpp   -o a
    c++     b.cpp   -o b
    

    If you set CXX and CXXFLAGS variables make will use them.

    0 讨论(0)
  • 2020-12-02 10:31

    Do it like so

    all: program1 program2
    
    program1: program1.c
        gcc -o program1 program1.c
    
    program2: program2.c
        gcc -o program2 program2.c
    

    You said you don't want advanced stuff, but you could also shorten it like this based on some default rules.

    all: program1 program2
    
    program1: program1.c
    program2: program2.c
    
    0 讨论(0)
提交回复
热议问题