How do I perform arithmetic in a makefile?

后端 未结 8 1027
终归单人心
终归单人心 2020-12-01 10:12

Is it possible to perform some operations on variables in a makefile? For instance, defining

JPI=4
JPJ=2

Is it possible to define in the sa

8条回答
  •  悲哀的现实
    2020-12-01 11:01

    In GNU Make with Guile support (i.e. since version 4.0) it is easy to use call to Scheme language for arithmetic or other calculations. It is done without creating any subshell or child process.

    An example

    JP-I := 4
    JP-J := 2
    JP-IJ := $(guile (* $(JP-I) $(JP-J) ))
    
    $(info JP-IJ = $(JP-IJ) )
    # prints: JP-IJ = 8
    

    See also the manual for Guile Arithmetic Functions.

    Possible check for Guile:

    ifeq (,$(filter guile,$(.FEATURES)))
      $(error Your Make version $(MAKE_VERSION) is not built with support for Guile)
    endif
    

提交回复
热议问题