gpt4 book ai didi

makefile - 更改 makefile 中的变量值

转载 作者:行者123 更新时间:2023-12-02 20:15:12 25 4
gpt4 key购买 nike

如何在 makefile 中动态更改变量值?我想根据宏的值调用特定目标。

例如。

STATIC_LIB = TRUE

all: makelib $(var)

makelib:
ifeq (${STATIC_LIB}, TRUE)
var=staticlib
else
var=sharedlib
endif

这里我想根据目标的值调用 staticlib 目标或共享库目标。

代码:

.SUFFIXES: .cpp .hpp

# Programs
SHELL = bash
CC = g++
LD = ld
RM = rm
ECHO = /bin/echo
CAT = cat
PRINTF = printf
SED = sed
DOXYGEN = doxygen
STATIC_LIB = TRUE
######################################
# Project Name (generate executable with this name)
TARGET = cs296_exe_28

# Project Paths
PROJECT_ROOT=$(HOME)/Desktop/cs296/cs296_base_code
EXTERNAL_ROOT=$(PROJECT_ROOT)/external
SRCDIR = $(PROJECT_ROOT)/src
OBJDIR = $(PROJECT_ROOT)/myobjs
BINDIR = $(PROJECT_ROOT)/mybins
LIBDIR = $(PROJECT_ROOT)/mylibs
DOCDIR = $(PROJECT_ROOT)/doc

# Library Paths
BOX2D_ROOT=$(EXTERNAL_ROOT)
GLUI_ROOT=/usr
GL_ROOT=/usr

#Libraries
LIBS = -lBox2D -lglui -lglut -lGLU -lGL

# Compiler and Linker flags
CPPFLAGS =-g -O3 -Wall
CPPFLAGS+=-I $(BOX2D_ROOT)/include -I $(GLUI_ROOT)/include
LDFLAGS+=-L $(BOX2D_ROOT)/lib -L $(GLUI_ROOT)/lib

######################################

NO_COLOR=\e[0m
OK_COLOR=\e[1;32m
ERR_COLOR=\e[1;31m
WARN_COLOR=\e[1;33m
MESG_COLOR=\e[1;34m
FILE_COLOR=\e[1;37m

OK_STRING="[OK]"
ERR_STRING="[ERRORS]"
WARN_STRING="[WARNINGS]"
OK_FMT="${OK_COLOR}%30s\n${NO_COLOR}"
ERR_FMT="${ERR_COLOR}%30s\n${NO_COLOR}"
WARN_FMT="${WARN_COLOR}%30s\n${NO_COLOR}"
######################################

SRCS := $(wildcard $(SRCDIR)/*.cpp)
INCS := $(wildcard $(SRCDIR)/*.hpp)
OBJS := $(SRCS:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o)
OBJSNMAIN := $(filter-out $(OBJDIR)/main.o,$(OBJS))

.PHONY: all setup doc clean distclean

ifndef STATIC_LIB
makelib : sharedlib # line 5
else ifeq ( ${STATIC_LIB}, TRUE )
makelib : staticlib # line 7
else
makelib : sharedlib # line 9
endif


all: setup makelib exelib

setup:
@$(ECHO) "Setting up compilation..."
@mkdir -p myobjs
@mkdir -p mybins
@mkdir -p mylibs

$(BINDIR)/$(TARGET): $(OBJS)
@$(PRINTF) "$(MESG_COLOR)Building executable:$(NO_COLOR) $(FILE_COLOR) %16s$(NO_COLOR)" "$(notdir $@)"
@$(CC) -o $@ $(LDFLAGS) $(OBJS) $(LIBS) 2> temp.log || touch temp.err
@if test -e temp.err; \
then $(PRINTF) $(ERR_FMT) $(ERR_STRING) && $(CAT) temp.log; \
elif test -s temp.log; \
then $(PRINTF) $(WARN_FMT) $(WARN_STRING) && $(CAT) temp.log; \
else $(PRINTF) $(OK_FMT) $(OK_STRING); \
fi;
@$(RM) -f temp.log temp.err

-include $(OBJS:.o=.d)

$(OBJS): $(OBJDIR)/%.o : $(SRCDIR)/%.cpp
@$(PRINTF) "$(MESG_COLOR)Compiling: $(NO_COLOR) $(FILE_COLOR) %25s$(NO_COLOR)" "$(notdir $<)"
@$(CC) $(CPPFLAGS) -c $< -o $@ -MD 2> temp.log || touch temp.err
@if test -e temp.err; \
then $(PRINTF) $(ERR_FMT) $(ERR_STRING) && $(CAT) temp.log; \
elif test -s temp.log; \
then $(PRINTF) $(WARN_FMT) $(WARN_STRING) && $(CAT) temp.log; \
else printf "${OK_COLOR}%30s\n${NO_COLOR}" "[OK]"; \
fi;
@$(RM) -f temp.log temp.err

exe: $(BINDIR)/$(TARGET)

makelib:
ifeq (${STATIC_LIB}, TRUE)
LIB_TYPE = staticlib
else
LIB_TYPE = sharedlib
endif

staticlib:
@$(ECHO) "static"
@ar rcs $(LIBDIR)/libCS296.a $(OBJSNMAIN)

sharedlib:
@$(ECHO) "shared"
@$(CC) -shared -o $(LIBDIR)/libCS296.so $(OBJSNMAIN)

exelib:
$(CC) -L $(LIBDIR) $(LDFLAGS) -o cs296_exelib_28 $(OBJDIR)/main.o -lCS296 $(LIBS)

doc:
@$(ECHO) -n "Generating Doxygen Documentation ... "
@$(RM) -rf doc/html
@$(DOXYGEN) $(DOCDIR)/Doxyfile 2 > /dev/null
@$(ECHO) "Done"

clean:
@$(ECHO) -n "Cleaning up..."
@$(RM) -rf $(OBJDIR) *~ $(DEPS) $(SRCDIR)/*~ $(LIBDIR)
@$(ECHO) "Done"

distclean: clean
@$(RM) -rf $(BINDIR) $(DOCDIR)/html

最佳答案

您提出的解决方案不起作用,因为 $(var) 将在执行 makelib 之前进行评估。

您可以做的是有条件地定义 makelib:

STATIC_LIB = TRUE
all : makelib

ifndef STATIC_LIB
makelib : sharedlib # line 5
else ifeq (${STATIC_LIB}, TRUE)
makelib : staticlib # line 7
else
makelib : sharedlib # line 9
endif

ifndef 条件指令可以避免在未定义 STATIC_LIB 的情况下发生的错误。

关于makefile - 更改 makefile 中的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14413171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com