4

I believe it is possible to use the value of an environment variable inside a make file.

Since I'm new to bash and make, had a tough time figuring out how.

I tried the following, but no success.

In Bash

TEST_VAR=1

export TEST_VAR

In make file

ifeq ($(TEST_VAR),1)

COMMON_OBJECTS               += Test1.o

endif

But It doesn't compile Test1.cpp.

My make file contains only these lines and I am adding COMMON_OBJECTS to another variable defined in another file.

If I comment this ifeq condition Test1 is getting compiled. But I am looking for a way to do this conditionally.

What I am missing here?

NeonGlow
  • 1,659
  • 4
  • 18
  • 36
  • execute **export TEST_VAR=1** in your prompt, then do a *make* – tuxuday Nov 28 '12 at 10:30
  • 1
    How do you execute your makefile, with gmake? Just after the export, in the same shell session? Everything you show here looks ok so there must be something else that is wrong. – Benoit Thiery Nov 28 '12 at 10:43
  • Thanks. Now my variable got listed when I execute export from console. But still the file Test1.cpp is not compiled. :( – NeonGlow Nov 28 '12 at 10:44
  • @BenoitThiery Thanks. I am exporting the var from bash script and below that calling the command for building the code which is another script. – NeonGlow Nov 28 '12 at 10:48
  • it should, show us your complete makefile. try after *rm Test1.o* – tuxuday Nov 28 '12 at 10:51
  • @tuxuday : My make file doesn't contain anything else since I wrote this just for testing. What I am doing is trying to add a few own cpp files to the build of a large code base which contains lot of source and make files. I am just including my make file in one of existing make files . Also I am getting Test1.o if I remove the Ifeq condition. – NeonGlow Nov 28 '12 at 11:22
  • It worked after running a dos2unix for the make file and a system restart. I suspect my Windows editor was the culprit. You guys are just great. Thanks. :) – NeonGlow Nov 28 '12 at 12:44
  • 2
    http://stackoverflow.com/questions/9606079/exporting-environment-variables-to-makefile-shell – Oleksandr Kravchuk Nov 29 '12 at 19:12
  • http://stackoverflow.com/questions/7507810/howto-source-a-script-from-makefile – Oleksandr Kravchuk Nov 29 '12 at 19:12

1 Answers1

1

if you are executing the "In bash" commands in a bash script file, then you have to execute it with preceeding ". ". that is if the bash file is called "script", then

$ . script
$ make

this is because each script runs in it's own instance of the bash, except when you preceed it with the ". ". it is then executed on the same bash that calls it.

kdehairy
  • 2,630
  • 22
  • 27
  • Do you mean to say, to access the value in make file, we need to run the script using `. script_name` rather than `./script_name`? In my case the bash script itself calls the build command. – NeonGlow Dec 27 '12 at 05:05
  • this is the bash script http://pastebin.com/qPgBAfGt. this is the Makefile http://pastebin.com/wFRDdatA. these scripts work as expected. please review them and let me know if i didn't get your case right. – kdehairy Dec 27 '12 at 11:18