Help running a Makefile from within a .sh script?


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Help running a Makefile from within a .sh script?
# 1  
Old 10-16-2017
Help running a Makefile from within a .sh script?

Hi there! I am a undergraduate student and recently submitted an assignment for my coursework - however there was one function I could not get to work properly before the due date. Although I don't need to complete this work anymore I would still like to in order to know what was going wrong. If this needs to be in the student/homework forum I am sorry! This is my first post so I'm new to the site and unsure.

The problem:
I have a function that is meant to compile any .c files that it finds in the current directory. It allows the user to either use their own Makefile (where the error is) - or use the Makefile within the .sh script which works. The Makefile is created with this function:
Code:
# Populate makefile with example code 
populateMFiles () {
echo '    clean:
          rm -rf cfile.c cfile.out
      build: 
          gcc -g -Wall -pedantic -Wextra cfile.c -w -o cfile.out
    all: cfile.c
        clean build'
}

And a .c file that is created with this function:
Code:
# Populate .c files with example code so that they can compile 
populateCFiles () {
echo '    #include <stdio.h>
    int main()
    {
           printf("Hello, World!");
           return 0;
    }'
}

Both of these functions are then used to fill two files as shown below:
Code:
populateCFiles > cfile.c
populateMFiles > Makefile

Both files are populated with the data correctly. I know the .c file can be compiled as it will do so if I compile it using the terminal. However, whenever I try to use the Makefile from within my bash script it returns the error message "ERROR: Makefile is incorrect - will not compile." which is defined in the method. The method in question is:
Code:
#Check repository for .c files
#Compile all .c files within the current directory
compileRepository () {
    tput cup $[$lineCount] 5 ; echo "============================================================"
    if [ -f $currentPath/*Makefile* ]; then # Detect makefiles in repository and allow user to use if desired
        tput cup $[1+$lineCount] 5 ; echo "Is there an existing make file within the repository" 
        tput cup $[2+$lineCount] 5 ; echo "you would like to use? (y/n) "
        read useMakefile
        if [[ $useMakefile == Y || $useMakefile == y ]]; then 
            cd $currentPath/
            make all
            if [ $? -eq 0 ]; then
                clear
                let lineCount=3
                tput cup 2 5 ; echo -e "${GREEN}           The .c files have succesfully compiled!          ${NC}"
                displayFileMenu
            else
                clear
                let lineCount=3
                tput cup 2 5 ; echo -e "${RED}      ERROR: Makefile is incorrect - will not compile.      ${NC}"
                displayFileMenu
            fi
    
        fi
    fi
    if [ -f $currentPath/*.c ]; then
        for everyFile in *.c ; do # Locate all .c files in directory         
            gcc -g -Wall -pedantic -Wextra "$everyFile" -w -o "$currentPath/${everyFile%.c}.out" # Acts as makefile for all .c files in repository
            if [ $? -eq 0 ]; then    # Checks return value from compiling files
                   clear
                let lineCount=3
                tput cup 2 5 ; echo -e "${GREEN}           The .c files have succesfully compiled!          ${NC}"
                displayFileMenu
                   else
                clear
                let lineCount=3
                   tput cup 2 5 ; echo -e "${RED}        ERROR: Code is incorrect - will not compile.        ${NC}"
                displayFileMenu
                   fi
        done  
    else
        clear
        let lineCount=3
        tput cup 2 5 ; echo -e "${RED}            ERROR: No .c files in the directory.            ${NC}"    
        displayFileMenu
    fi
}

As you can see, the function will attempt to detect a Makefile already in the current directory and if there is it will ask the user if they wish to use it. If the user does decide to use it the function calls the make all command which will always throw up the error described previously. Why does it not work correctly?

Further information:
The $currentPath variable stores which directory the user is currently accessing and takes the form:
/tmp/Assignment1/Repository/Repository_Alpha
I thought the error could be with the Makefile itself so I've tried countless different Makefile formats taken from other forums/website - so I don't think the problem is with that. I've also tried calling the Makefile commands without the 'all' suffix i.e. just make but still returns the same error message.

Any help would be greatly appreciated, thank you!
- cherryTango
# 2  
Old 10-16-2017
Hi.

Judging by populateMFiles, which prints the contents of the makefile, the indentation is wrong. Perhaps removing leading spaces from the targets, and replacing the leading spaces of the commands with a tab would help?
# 3  
Old 10-16-2017
With the code altered to read:
Code:
# Populate makefile with example code 
populateMFiles () {
echo 'clean:
    rm -rf cfile.c cfile.out
build: 
    gcc -g -Wall -pedantic -Wextra cfile.c -w -o cfile.out
all: cfile.c
    clean build'
}

It still returns the same error! Is there anything else I can try?
# 4  
Old 10-16-2017
Are those spaces or a TAB character before rm ..., gcc ... and clean ...? Changing your function as-is as I suggested, it works fine. It needs to be a TAB, spaces won't work. If I click to edit your post, just see exactly what you pasted, I see spaces, not a TAB.
# 5  
Old 10-16-2017
They are all tab characters. Still returning same error message. Also have changed the Makefile so that 'clean' does not remove the cfile.c before it is compiled:
Code:
clean:
    rm -rf cfile.out
build: 
    gcc -g -Wall -pedantic -Wextra cfile.c -w -o cfile.out
all: cfile.c
    clean build

It will now only remove any previous compilations of the file (I think?) - if I manage to get it working!
# 6  
Old 10-16-2017
Is your editor set to replace a horizontal TAB with spaces?

Can you post the output of:
Code:
populateMFiles | od -c

# 7  
Old 10-16-2017
Running the
Code:
populateMFiles | od -c

returned:
Code:
0000000   c   l   e   a   n   :  \n                   r   m       -   r
0000020   f       c   f   i   l   e   .   o   u   t  \n   b   u   i   l
0000040   d   :      \n                   g   c   c       -   g       -
0000060   W   a   l   l       -   p   e   d   a   n   t   i   c       -
0000100   W   e   x   t   r   a       c   f   i   l   e   .   c       -
0000120   w       -   o       c   f   i   l   e   .   o   u   t  \n   a
0000140   l   l   :       c   f   i   l   e   .   c  \n                
0000160   c   l   e   a   n       b   u   i   l   d  \n
0000174

What does this mean?

---------- Post updated at 11:48 AM ---------- Previous update was at 11:41 AM ----------

UPDATE: That was after copy and pasting the makefile code from this forum, when using the original code the output of that command was:
Code:
0000000   c   l   e   a   n   :  \n  \t   r   m       -   r   f       c
0000020   f   i   l   e   .   o   u   t  \n   b   u   i   l   d   :    
0000040  \n  \t   g   c   c       -   g       -   W   a   l   l       -
0000060   p   e   d   a   n   t   i   c       -   W   e   x   t   r   a
0000100       c   f   i   l   e   .   c       -   w       -   o       c
0000120   f   i   l   e   .   o   u   t  \n   a   l   l   :       c   f
0000140   i   l   e   .   c  \n  \t   c   l   e   a   n       b   u   i
0000160   l   d  \n
0000163

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Problem running a makefile

I have written this makefile and am getting an error saying make nfd gfortran -O -Wall -fbacktrace -fno-align-commons -c -o fd.o fd.f fd.f:49: Error: Can't open included file 'fd.par' make: *** Error 1 The directory structure is as follows . ├── library │ ├── fd │ │ ├──... (3 Replies)
Discussion started by: kristinu
3 Replies

2. Shell Programming and Scripting

Need help with a script to make makefile

How do we create a shell script that creates a makefile? what if we want to use the #include header files too? (2 Replies)
Discussion started by: sslokhan
2 Replies

3. Shell Programming and Scripting

using a Shell Script in a Makefile

Hello, I have a Makefile that converts wrl (vrml) files to html files... how can I use a shell script in that makefile which works on all html files after converting? The Shell Script have to find and replace a String in every createt html file. sorry I'm a Newbie, so I hope someone can... (0 Replies)
Discussion started by: Dan_78
0 Replies

4. Homework & Coursework Questions

Help with Simple Multi-Level Makefile (Extremely New at Makefile)

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: Basically, the prompt is make a makefile with various sub makefiles in their respective subdirectories. All code... (1 Reply)
Discussion started by: Tatl
1 Replies

5. UNIX for Advanced & Expert Users

Makefile problem - How to run module load in a Makefile

Hi, I'm trying to run the module load command in a Makefile and i'm getting the following error: make: module: command not found Why is this? Is there any way to run this command in a Makefile? NOTE: command - module load msjava/sunjdk/1.5.0 works fine outside of the Makefile (2 Replies)
Discussion started by: hernandinho
2 Replies

6. UNIX for Dummies Questions & Answers

error while running a makefile

any good website to know about makefiles (3 Replies)
Discussion started by: raviravula
3 Replies

7. Shell Programming and Scripting

Shell script makefile

Is there a way to write a makefile for all the source files in a directory with a shell script? (2 Replies)
Discussion started by: zzhan
2 Replies

8. Shell Programming and Scripting

shell script in makefile

Hi, Can we execute a shell script by makefile. I mean we will write a shell script in a make file and it will be executed when we compile the C++ program using make file. (2 Replies)
Discussion started by: surjyap
2 Replies

9. Shell Programming and Scripting

embeding shell script in makefile

Hi I am new to shell scripting and makefile. I want a command's output in makefile to process further, can anyone plz suggest me a way ? I want ls -d *.dsm output in a variable and want to process it in makefile itself. It's urgent Thanks In advance (0 Replies)
Discussion started by: madhu12345
0 Replies

10. Shell Programming and Scripting

makefile sh script

Hello World ! ! ! I need libraries to use grib files. I only know the C language at the moment and I am working at the University under Red Hat 9.0. I downloaded the g2clib library (the best that I found) but I did not success to run the makefile. Here is the original file. I modified some... (4 Replies)
Discussion started by: Akeson Chihiro
4 Replies
Login or Register to Ask a Question