need help with makefile

I am try to make a makefile. but didnt work

i type touch makefile ---> makefile create ----> in the makefile i write this
1
2
3
4
5
6
7
8
9
10
11
12
13
  CC = gcc
  CFLAGS  = -g -Wall

  # the build target executable:
  TARGET = proj1.c

  all: $(TARGET)

  $(TARGET): $(TARGET).c
  	$(CC) $(CFLAGS) -o $(TARGET) $(TARGET).c

  clean:
  	$(RM) $(TARGET)
--->run it

but problem is i chose input file and outfile but dont see an output file


I do have test.txt and proj1.c in same dic
Last edited on
for example if i run

I will type

make -f*. make project1

project1 test.txt output.txt

which mean test.txt will be read into code and it will output the on output.txt
Fatal error in reader: Makefile, line 7: Unexpected end of line seen
use: TARGET = proj1

the target is proj1, but not pro1.c.
There are a number of problems.

The target is an executable, not a .c file:
 
TARGET = proj1.c

That should be:
 
TARGET = proj1


The program is dependent on object files. The object files are dependent on source files, but GNU make already knows that, so you don't need to tell it how to compile.

Now, make provides a way of making up object file names, from source file names using text substitution.

So, we need to name the source files:
 
SRCS = proj1.c

And you do have to tell it how to link.
1
2
$(TARGET): $(SRCS:.c=.o)
  	$(LINK.c) $^ -o $@

$^ is the list of sources, proj1.o in this case.
$@ is the name of the target, proj1 in this case.
Use $(LINK.cc) to link C++ programs.


The clean step must also delete the object files:
1
2
clean:
  	rm $(TARGET) $(SRCS:.c=.o)
Last edited on
Registered users can post here. Sign in or register to post.