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 .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:
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 makeall 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
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?
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.
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:
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
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)
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)
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)
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)
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)
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)
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)