How do I substitute a string and add a prefix or suffix in Microsoft NMake?

nmake

Solution

Once you have the string in either an environment variable or an internal nmake variable, you can use the following to substitute one fixed string for another:

$(MY_VAR:REPLACE_THIS=WITH_THIS)

"WITH_THIS" can be an empty string.

Example makefile:

MY_VAR=123451234512345
ALL:
   @echo $(MY_VAR:12=XX)
   @echo $(MY_VAR:12=)

outputs:

XX345XX345XX345
345345345

From the Microsoft documentation:

Macro substitution is case sensitive and is literal; string1 and string2 cannot invoke macros. Substitution does not modify the original definition. You can substitute text in any predefined macro except `$$@`.

No spaces or tabs precede the colon; any after the colon are interpreted as literal. If string2 is null, all occurrences of string1 are deleted from the macro's definition string.

Problem

I am translating a GNU Make makefile to Microsoft Visual Studio Makefile. I have a three doubts: 1) How do I substitute a string. For example in a folder containing: ``` namespace_type_function1.cpp namespace_type_function2.cpp namespace_type_function3.cpp ``` I want to change type to lets say "INT" string, so I finally get ``` namespace_INT_function1.cpp namespace_INT_function2.cpp namespace_INT_function3.cpp ``` 2) How do I add a prefix in the similar manner 3. How do I add a suffix in the same way.

Original source