Results 1 to 8 of 8
Hi, I am trying to build a makefile from a shell script with the following command lines:
cat << EOF > makefile
FFLAGS = -s
LINK = f77 -o
MACH ...
- 07-18-2011 #1Just Joined!
- Join Date
- Jul 2011
- Posts
- 4
Help making a makefile from shell script
Hi, I am trying to build a makefile from a shell script with the following command lines:
cat << EOF > makefile
FFLAGS = -s
LINK = f77 -o
MACH = amach.o
OBJS = driver.o psr.o eqlib.o stanlib.o cklib.o twopnt.o cputim.o \
math.o $(MACH)
EXES = chem.exe psr.exe
INPS = therm.dat chem.inp psr.inp
OUTS = chem.bin chem.out save.bin recov.bin psr.out
chem.exe : ckinterp.o
$(LINK) chem.exe ckinterp.o
psr.exe : $(OBJS)
$(LINK) psr.exe $(OBJS)
EOF
But I ended up getting this errors:
psr.sh: line 4: MACH: command not found
psr.sh: line 4: LINK: command not found
psr.sh: line 4: OBJS: command not found
psr.sh: line 4: LINK: command not found
psr.sh: line 4: OBJS: command not found
and my makefile has blanks where MACH, LINK and OBJS are.
FFLAGS = -s
LINK = f77 -o
MACH = amach.o
OBJS = driver.o psr.o eqlib.o stanlib.o cklib.o twopnt.o cputim.o math.o
EXES = chem.exe psr.exe
INPS = therm.dat chem.inp psr.inp
OUTS = chem.bin chem.out save.bin recov.bin psr.out
chem.exe : ckinterp.o
chem.exe ckinterp.o
psr.exe :
psr.exe
It seems like the shell tries to read the macros instead of writing them into my makefile. Can anyone help me to solve this? Sorry I am new to fortran /linux. Thank you very much.
- 07-18-2011 #2Linux Guru
- Join Date
- May 2011
- Posts
- 1,842
you need to escape all the instances of the '$' char, e.g.
Code:psr.exe : \$(OBJS) \$(LINK) psr.exe \$(OBJS)
- 07-18-2011 #3Just Joined!
- Join Date
- Jul 2011
- Posts
- 4
nope it doesnt work. It still blank for $(OBJS) etc in the built makefile, hence exe cant be compiled. Any other ideas?
- 07-19-2011 #4Linux Guru
- Join Date
- May 2011
- Posts
- 1,842
i put it all in a shell script and it worked for me. and now that i look at it, i think i need to escape a continuation character, and add a missing continuation character. taking that into account, i made a file called makefile.sh containing this:
then i ran it like this:Code:cat << EOF > makefile FFLAGS = -s LINK = f77 -o MACH = amach.o OBJS = driver.o psr.o eqlib.o stanlib.o cklib.o twopnt.o cputim.o \\ math.o \$(MACH) EXES = chem.exe psr.exe INPS = therm.dat chem.inp psr.inp OUTS = chem.bin chem.out save.bin recov.bin psr.out chem.exe : ckinterp.o \\ \$(LINK) chem.exe ckinterp.o psr.exe : \$(OBJS) \\ \$(LINK) psr.exe \$(OBJS) EOF
and it successfully made the makefile.Code:sh makefile.sh
or am i misunderstanding you, and the makefile is made, but it does not compile properly?
- 07-19-2011 #5Just Joined!
- Join Date
- Jul 2011
- Posts
- 4
Yeah you are right with the escape thing and it works for me too if I make a new sh file with the above coding. However, it doesnt work in the original sh file which is trying to make a few files at the same time. I think the problem comes from the following coding which is located in the beginning and end of the sh file.
sh 1> ${1}.log 2>&1 << ENDSH
....
....
....
ENDSH
From the comments, I know the codes are to ensure the sh output will be printed onto a text file instead of screen. But I am not sure why it prevents the backslashes from working.
Running the sh file will make a makefile with $(OBJS) $(LINK) missing and obviously compiling it doesnt work as the makefile is incomplete.
If I remove the above coding, everything works perfectly.
- 07-19-2011 #6Linux Guru
- Join Date
- May 2011
- Posts
- 1,842
try adding an additional two escape backslashes before each escape backslash that was added, e.g.:
does that work?Code:psr.exe : \\\$(OBJS) \\\\ \\\$(LINK) psr.exe \\\$(OBJS) EOF
- 07-21-2011 #7Just Joined!
- Join Date
- Jul 2011
- Posts
- 4
wow it works magically. Thank you very much. But do u know why is that?
- 07-21-2011 #8Linux Guru
- Join Date
- May 2011
- Posts
- 1,842
well, i think it is b/c each time you invoke a sub-shell, that sub-shell (Bash) wants to interpolate the variables, indicated by the $ sign. And as I said before, if you want to preserve the $ char, you have to escape it. You have two sub-shells in your code, which means escaping it twice. The first sub-shell is the "sh" at the very beginning, the 2nd one is the "here document" you create using the cat and redirectors.


Reply With Quote