gcc/g++ option to place all object files into separate directory

后端 未结 10 554
无人及你
无人及你 2020-11-30 19:44

I am wondering why gcc/g++ doesn\'t have an option to place the generated object files into a specified directory.

For example:

mkdir builddir
mkdir          


        
10条回答
  •  [愿得一人]
    2020-11-30 20:03

    This is the chopped down makefile for one of my projects, which compiles the sources in 'src' and places the .o files in the directory "obj". The key bit is the the use of the patsubst() function - see the GNU make manual (which is actually a pretty good read) for details:

    OUT = lib/alib.a
    CC = g++
    ODIR = obj
    SDIR = src
    INC = -Iinc
    
    _OBJS = a_chsrc.o a_csv.o a_enc.o a_env.o a_except.o \
            a_date.o a_range.o a_opsys.o
    OBJS = $(patsubst %,$(ODIR)/%,$(_OBJS))
    
    
    $(ODIR)/%.o: $(SDIR)/%.cpp 
        $(CC) -c $(INC) -o $@ $< $(CFLAGS) 
    
    $(OUT): $(OBJS) 
        ar rvs $(OUT) $^
    
    .PHONY: clean
    
    clean:
        rm -f $(ODIR)/*.o $(OUT)
    

提交回复
热议问题