Change Makefile variable value depending on a target

后端 未结 2 699

I am not proficient with makefiles, but am used to simple ones. Right now, I have a task on hand.

I need to compile and link a test application with a different libr

2条回答
  •  春和景丽
    2020-12-31 09:58

    If you are using GNU Make, you can use target-specific variables:

    target1: CFLAGS = -IINCLUDEPATH1
    target1: LDLIBS = -lLIB1
    
    target2: CFLAGS = -IINCLUDEPATH2
    target2: LDLIBS = -lLIB2
    
    all: target1 target2
    
    target1: target1.o misc.o
    target2: target2.o
    

    However this doesn't work quite as well as you'd like: if target1 and target2 share some source files, you'll need to arrange for them to each be compiled twice and to differently-named .o files -- which will rather complexify your makefile.

    Also, if you type make target1 then -IINCLUDEPATH1 will be propagated to the compilation of misc.c, as desired. However if you type make misc.o it has no way to know that this is eventually destined for target1 and the compilation of misc.c will get no special $CFLAGS value (though it'll get the global one, if there is one).

    So this is really only useful in simple cases. But maybe your case is sufficiently simple.

提交回复
热议问题