Character Substitution in makefile

This is a discussion on Character Substitution in makefile within the Tech Board forums, part of the Community Boards category; Greetings. My name is Rasputin and I'm a makefilic. (Obviously not successful as a comic! ) OK, right to the ...

  1. #1
    Registered User rpaskudniak's Avatar
    Join Date
    Oct 2011
    Location
    Brooklyn, NY (USA)
    Posts
    2

    Character Substitution in makefile

    Greetings.

    My name is Rasputin and I'm a makefilic. (Obviously not successful as a comic! )

    OK, right to the point (well, maybe not so fast - it needs a build-up):

    Consider the following sequence of macros and dependencies, which I wish to refine:

    Code:
    M4_MODULES = first.m4 second.m4 third.m4 fourth.m4
    C_MODULES  = first.c  second.c  third.c  fourth.c
    O_MODULES  = first.o  second.o  third.o  fourth.o
    my_program: $(O_MODULES) $(LIBRARIES)
    (Yes, lots of steps skipped here; that's not the point of this post.) What's mainly wrong with this is that if I add a fifth module, I have to add fifth.m4, fifth.c, and fifth.o to the respective lines. In a complicated makefile that is error prone. So I came up with the following fix, which does work. (I've tested it.) Note the dot at the end of each module name:

    Code:
    MODULES = first. second. third. fourth.
    M4_MODULES = ${MODULES:.=.m4}
    C_MODULES  = ${MODULES:.=.c}
    O_MODULES  =  ${MODULES:.=.o}
    So if it works, what's my problem? Well, it seems the original module names have their own dependency issues. So it really is necessary to specify:
    Code:
    MODULES = first second third fourth
    without the terminating dots. Thus, I need to substitute for the end of a string, something like:
    Code:
    M4_MODULES = ${MODULES:$=.m4}

    and so on.

    The problem is that the above substitution does not work. Neither does $$=.m4 nor does \$=.m4; I'm plumb out of guesses.

    SO finally the obvious question:

    Does anyone know of an syntax in a makefile macro that will perform end-of-string substitutions?

    Thanks much for some assistance with this.

    -- Rasputin Paskudniak

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,423
    Use the make function patsubst for pattern substitution, and possibly look into the wildcard function for grabbing all .m4 files as a starter:
    Code:
    M4_MODULES = $(wildcard *.m4)
    C_MODULES = $(patsubst %.m4, %.c, $(M4_MODULES))
    O_MODULES = $(patsubst %.c, %.o, $(C_MODULES))
    Make has fantastic documentation with examples online at GNU `make'.

  3. #3
    Registered User rpaskudniak's Avatar
    Join Date
    Oct 2011
    Location
    Brooklyn, NY (USA)
    Posts
    2

    Solved: Character Substitution in makefile

    Quote Originally Posted by anduril462 View Post
    Use the make function patsubst for pattern substitution, and possibly look into the wildcard function for grabbing all .m4 files as a starter:
    Code:
    M4_MODULES = $(wildcard *.m4)
    C_MODULES = $(patsubst %.m4, %.c, $(M4_MODULES))
    O_MODULES = $(patsubst %.c, %.o, $(C_MODULES))
    Make has fantastic documentation with examples online at GNU `make'.
    Whoopie! That did it, with some slight difference WRT your sample code.

    Thank you, Anduril! Here's the syntax I used:

    Code:
    MODULES = first second third fourth
    M4_MODULES = $(patsubst %,%.m4,$(MODULES))
    
    And it did exactly what I needed!

    Yours Gratefully,
    Rasputin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Substitution from C to C++
    By nepper271 in forum C++ Programming
    Replies: 10
    Last Post: 12-10-2010, 10:33 AM
  2. Simple(?) substitution cipher help
    By kalor_alros in forum C++ Programming
    Replies: 23
    Last Post: 09-02-2009, 12:23 AM
  3. Liskov substitution principle
    By George2 in forum C++ Programming
    Replies: 9
    Last Post: 02-25-2008, 04:30 AM
  4. Substitution problem...
    By Junior89 in forum C++ Programming
    Replies: 21
    Last Post: 06-27-2007, 04:13 PM
  5. EQU substitution??
    By theeld in forum C Programming
    Replies: 1
    Last Post: 10-28-2006, 07:52 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21