5

If I have something like this:

PROJECTS += path/to/first
PROJECTS += path/to/second
PROJECTS += path/to/third

and

LIBS += lib_output/first.lib
LIBS += lib_output/second.lib
LIBS += lib_output/third.lib

How could I associate the project from PROJECTS += path/to/first with LIBS += lib_output/first.lib? Is there something like a hashmap available in a makefile? Or possibility to search in an array?

1
  • 2
    Make isn't really good at complex variables. Could you give us a better idea of what you're trying to do? Commented Sep 2, 2011 at 12:44

2 Answers 2

14

You can simulate lookup tables using computed variable names and the fact that make variable names can include some special characters like dot and forward slash:

PROJECTS += path/to/first
PROJECTS += path/to/second
PROJECTS += path/to/third

LIBS += lib_output/first.lib
LIBS += lib_output/second.lib
LIBS += lib_output/third.lib

lookup.path/to/first  := lib_output/first.lib
lookup.path/to/second := lib_output/second.lib
lookup.path/to/third  := lib_output/third.lib

path := path/to/first
$(info ${path} -> ${lookup.${path}})
path := path/to/second
$(info ${path} -> ${lookup.${path}})
path := path/to/third
$(info ${path} -> ${lookup.${path}})

Outputs:

$ make
path/to/first -> lib_output/first.lib
path/to/second -> lib_output/second.lib
path/to/third -> lib_output/third.lib
Sign up to request clarification or add additional context in comments.

8 Comments

can you use it in a rule? I have rules like foo.o: randompath/foo.c, I have plenty of such files under various path, I can generate object names easily $(notdir $(SOURCES:.c=.o)), but then I cannot derive source name in pattern rule. Can I use your solution to create a single rule for them all?
You can try using VPATH to help make locate the sources from different directories. A better solution is to replicate the source directory hierarchy in the object directory. That would avoid filename clashes.
Good pointer. I think vpath (lowercase) is what I need. My object file name is actually 'flattened' path, e.g. some_5_steps_deep_path_foo.c.o. Much more efficient than replicating folder structure if you only cherry pick few files from each of bunch of projects.
@MaximEgorushkin is there a way to do this expansion in dependency place. Like: target: $(lookup.$(path)) I find that it does not get evaluated.
@MohammadAzim There is, it is called Secondary Expansion.
|
2

I'm not sure if I completely understand your question, but I think the word function might be what you need (it may be a GNU make extension):

$(word 2, $(PROJECTS)) returns path/to/second,
$(word 2, $(LIBS)) returns lib_output/second.lib.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.