Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle setting up environment in makefile?

Tags:

makefile

So, to compile my executable, I need to have the library locations set up correctly. The problem is, the setup comes from a bunch of scripts that do the env variable exporting, and what needs to be set up may change (beyond my control) so I need to use those scripts instead of copying their functionality. To compile in regular command line, I need to do something like:

setup library1
setup library2
source some_other_setup_script.bash
g++ blah.c
# setup is a executable on my system that run some scripts

How would I write a makefile that accomplishes that? As far as I tried, the env variable exporting does not carry over (i.e. "export VAR=remember; echo $VAR" won't work)

like image 201
polyglot Avatar asked Oct 09 '09 23:10

polyglot


2 Answers

You can also add environment variables properly with the machinery of GNU make, like so:

export TEST:="Something Good!"
test:
    echo $$TEST

This (I think) has different semantics from:

TEST2:="Something not quite so useful?"
test2:
    echo ${TEST2}

Which (again, I think) does the substitution within make before passing along to the shell. Note that the export command doesn't work within a target block, just unindented as an immediately executed command.

like image 187
Dav Clark Avatar answered Oct 25 '22 02:10

Dav Clark


If variable exporting is not working the way it does on your command line, that suggests that Make is choosing a shell different from the one you're using, with different syntax for handling variables (export VAR=remember; echo $VAR works fine for me). Make uses /bin/sh by default, but you can override this with the SHELL variable, which Make does not import from the environment. I suggest setting SHELL (in the Makefile) to whatever you're using in your environment and trying the export VAR=remember experiment again.

like image 41
Beta Avatar answered Oct 25 '22 01:10

Beta