I’m using CentOS to write a Makefile to generate these files from a C program:
hello.i
(Modified source program, text)hello.s
(Assembly program, text)hello.o
(Relocatable object program, binary)hello
(Executable object program, binary)
The idea is to open each file to see its content.
C program (called hello.c)
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Makefile
all: hello
hello:
gcc -I hello.c -o hello.i
hello.o:
gcc -c hello.c -o hello.o
hello.s:
gcc -S hello.i -o hello.s
hello.i:
gcc ???
clean:
rm -rf *.o hello
Also, a clean command to delete all.
Error that I receive when I do: make all
compilation terminated. make: *** [hello] Error 4
I know that there is a single command to generate all at once, but, I want to do it by steps. This is my first Makefile that I try to do and I’m not 100% familiar yet.
What I’m doing wrong, maybe a wrong flag?
My goal is to generate all the files mentioned above to open them and see their content.
2
Answers
Well, you did not declare a recipe for
hello.i
, and you did not declare the appropriate dependencies for the other recipes. Additionally, the command line option to createhello.i
is-E
, not-I
.Let’s see:
hello
needs the filehello.o
hello.i
needs the filehello.c
hello.o
needs the filehello.c
hello.s
needs the filehello.i
This would be the correct makefile:
Additionally, taking advantage of Make special variables, you can use
$@
to indicate the current recipe, and$<
to indicate the first dependency, making it less verbose, like this:Without the
hello.i
, which is for me unclear what is should be, your make file should look like that:You issue is that in Makefile, after the semicolon you should indicate the dependencies (the files the current output will need to be produced).
In you case,
all
declares thathello
andhello.s
are required. Thenmake
looks to produce them using the given commands.Hello
declares thathello.o
is required, so it also look to the given command to produce it.hello.o
andhello.o
declare thathello.c
is required, somake
will check if it finds the file and in yes it will run thegcc
command. Now thathello.o
is produced, it will go back to producehello
.The commands should be indented with proper tabs, so if you copy paste check that you have tabs and no multiple spaces.