Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9149669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:30:31+00:00 2026-06-17T11:30:31+00:00

How do I change variable value dynamically in makefile? I want to call specific

  • 0

How do I change variable value dynamically in makefile?
I want to call specific target depending on value of macro.

For eg.

STATIC_LIB = TRUE

all: makelib $(var)

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

Here i want to call either staticlib target or sharedlib target depending on the value of target.

Code:

.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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:30:32+00:00Added an answer on June 17, 2026 at 11:30 am

    The solution that you came up with would not work since $(var) would be evaluated before makelib is executed.

    What you could do instead is to define makelib conditionally:

    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
    

    An ifndef conditional directive saves from an error that would occur in case STATIC_LIB is not defined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to change the variable value which is a member of a structure
Is there a way to dynamically change a variable value inside a string? What
i have a loop where a variable value will change in each loop and
How do I change the value of a variable in the package used by
I can't understend why variable s_return dont work $('.codeinput').change(function() { var s_return=; var to_check=this.value
I simply want to change a variable of an object from another class. I
I'm trying to change a hardcoded variable value to dynamic, but can't seem to
If you want to use global functions and variable dynamically you can use: window[functionName](window[varName]);
I am having a session variable whose value can be changed from java and
I'm trying to change a variable of NSString type by calling a method. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.