D
D
Danil Tunev2019-01-03 09:48:56
C++ / C#
Danil Tunev, 2019-01-03 09:48:56

How to build an executable file from 2 sources using make?

In general, two files with the source code main_0.c main_1.c and the third header file header.h in which all the prototypes and global variables.
Here is the text of the makefile:

/home/tr/tr: /home/tr/main_0.o /home/tr/main_1.o
        cc  -o /home/tr/main_0.o /home/tr/main_1.o
/home/tr/main_0.o: /home/tr/main_0.c /home/tr/header.h               
        cc -c /home/tr/main_0.c                  
/home/tr/main_1.o: /home/tr/main_1.c /home/tr/header.h
        cc -c /home/tr/main_1.c

When "make -f makefile" is called, the following output appears:
make -f /home/tr/tr_make
cc -c /home/tr/main_0.c
cc -o /home/tr/main_0.o /home/tr/main_1. o
/usr/lib/gcc/x86_64-linux-gnu/6/../../../x86_64-linux-gnu/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
/home/tr/tr_make:2: error executing recipe for target '/home/tr/tr'
make: *** [/home/tr/tr] Error 1

What am I doing wrong? if I just compile the sources by typing "gcc ...." in the terminal, everything compiles and runs fine. I have already spent a lot of time on the makefile script, and the sequential assembly of two object files is not the pinnacle of fame)) What's wrong? Suggest, please!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Movchan, 2019-01-03
@lada-guy

As you have already been answered in a comment, you simply didn't provide an option for -o.
In general, you should never use absolute paths in a Makefile.
And yet, if you change your header.h, then make will not know about it.
Compilers have special options to generate .c file dependencies from headers.
Here is a generic Makefile for simple projects:

PROJECT = tr
SOURCES = main_0.c main_1.c
OBJECTS = $(SOURCES:.c=.o)
HEADER_DEPS = $(SOURCES:.c=.d)

.PHONY: all

all: $(PROJECT)

$(PROJECT): $(OBJECTS)
  $(CC) $(CFLAGS) $^ -o [email protected]

-include $(HEADER_DEPS)

%.o: %.c
  $(CC) $(CFLAGS) -MM -MT [email protected] -MF $(patsubst %.o,%.d,[email protected]) $<
  $(CC) $(CFLAGS) -c $< -o [email protected]

.PHONY: clean

clean:
  $(RM) $(PROJECT) $(OBJECTS) $(HEADER_DEPS)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question