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:
# 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:
# 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:
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:
#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