Find the answer to your Linux question:
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just 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.

  2. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    you need to escape all the instances of the '$' char, e.g.
    Code:
    psr.exe : \$(OBJS)
    \$(LINK) psr.exe \$(OBJS)

  3. #3
    Just 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?

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    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:
    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
    then i ran it like this:
    Code:
    sh makefile.sh
    and it successfully made the makefile.

    or am i misunderstanding you, and the makefile is made, but it does not compile properly?

  5. #5
    Just 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.

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    try adding an additional two escape backslashes before each escape backslash that was added, e.g.:
    Code:
    psr.exe : \\\$(OBJS) \\\\
    \\\$(LINK) psr.exe \\\$(OBJS)
    EOF
    does that work?

  7. #7
    Just Joined!
    Join Date
    Jul 2011
    Posts
    4
    wow it works magically. Thank you very much. But do u know why is that?

  8. #8
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,307
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •