Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while trying to load module environment in makefile

Tags:

bash

makefile

I have a makefile and I'm trying to load some modules before running the target. Here is the makefile:

CC=g++        
all:
        . /usr/share/Modules/init/bash
        module load gcc/4.8.1
        module load opencv
        module load python/2.7.5
        $(CC) -std=gnu++11 -lstdc++ -fPIC -shared -o .......
clean:
        rm ../../lib/linux/extract_features.so

But when I run make command, it gives me this error:

/usr/share/Modules/init/bash_completion: line 14: syntax error near unexpected token `('
/usr/share/Modules/init/bash_completion: line 14: ` comm -23  <(_module_avail|sort)  <(tr : '\n' <<<${LOADEDMODULES}|sort)'
/usr/share/Modules/init/bash_completion: line 14: warning: syntax errors in . or eval will cause future versions of the shell to abort as Posix requires
make: *** [all] Error 1

If I just run . /usr/share/Modules/init/bash in the terminal, it does not give me any error.

How can I load modules in a makefile?Any suggestion?

like image 598
user2308191 Avatar asked Dec 24 '22 15:12

user2308191


1 Answers

The default shell for running make recipes is /bin/sh not /bin/bash. You can override that by assigning to the SHELL variable. (Note the addition of .SHELLFLAGS in make 4.0.)

Additionally, each line of a recipe is run in its own shell session so a line like module load opencv will load that module in a shell session that then immediately exits so the next line module load python/2.7.5 will not have it loaded.

You need to either write all the commands on a single line or tell make that they are one continued line by using \ at the end of each line (and making sure to have the right ;/&/etc. terminators between the lines for the shell (as make will run it as one single line when it runs it).

CC=g++
SHELL:=/bin/bash

all:
        . /usr/share/Modules/init/bash; \
        module load gcc/4.8.1; \
        module load opencv; \
        module load python/2.7.5; \
        $(CC) -std=gnu++11 -lstdc++ -fPIC -shared -o .......

clean:
        rm ../../lib/linux/extract_features.so
like image 82
Etan Reisner Avatar answered Mar 23 '23 22:03

Etan Reisner