הלינקייה: מגזין חודשי למפתחים

רוצה לשמוע על כל האירועים, המדריכים, הקורסים והמאמרים שנכתבו החודש ?
הלינקייה הינו מגזין חופשי בעברית שמשאיר אותך בעניינים.
בלי ספאם. בלי שטויות. פעם בחודש אצלך בתיבה.

Conditionals

Make provides four conditional directives that control how make sees the makefile. These directives are used to skip or include various parts of a makefile by rendering them visible or invisible to the make utility.

The four directives are: ifeq, ifneq, ifdef and ifndef. Let's have a look at how to use them

libs_for_gcc = -lgnu
normal_libs =
 
foo: $(objects)
ifeq ($(CC),gcc)
        $(CC) -o foo $(objects) $(libs_for_gcc)
else
        $(CC) -o foo $(objects) $(normal_libs)
endif
#
# Using the quoted syntax for ifeq ensures us that no extra
# whitespaces sneak in
#
 
libs_for_gcc = -lgnu
normal_libs =
 
foo: $(objects)
ifeq "$(CC)" "gcc"
        $(CC) -o foo $(objects) $(libs_for_gcc)
else
        $(CC) -o foo $(objects) $(normal_libs)
endif
#
# By using the ifeq directive when defining the variable, 
# we avoid code repetition if we want to use that value in more
# places in the makefile
# 
 
libs_for_gcc = -lgnu
normal_libs =
 
ifeq ($(CC),gcc)
  libs=$(libs_for_gcc)
else
  libs=$(normal_libs)
endif
 
foo: $(objects)
        $(CC) -o foo $(objects) $(libs)
#
# This conditional will be true if $(foo) is empty or contains only spaces
# We'll talk about the strip function later in the course
# 
 
ifeq ($(strip $(foo)),)
 text-if-empty
endif
course: