D
D
Danil Tunev2018-11-03 07:47:08
C++ / C#
Danil Tunev, 2018-11-03 07:47:08

How to write a makefile?

Damn, it's dumb to admit it, but all the contents of this file are like Chinese characters for me! Explain, please! I understand that the executable file is assembled from several object files (or one) and these object files are compiled from source with included header files! How would you write the makefile if you had 0.c 1.c 2.c 3.h? Header 3.h for all sources

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2018-11-03
@lada-guy

How would you write the makefile if you had 0.c 1.c 2.c 3.h? Header 3.h for all sources

OBJS := 0.o 1.o 2.o                     # список объектников, который нам понадобится в двух местах
all: exe                                # традиционно сборка всего называется all. all зависит от единственного файла: exe
exe: $(OBJS)                            # файл exe зависит от объектникоа
        $(CC) $(LDFLAGS) -o [email protected] $^       # чтобы собрать exe -- вызвать компилятор, передать ему LDFLAGS, выводить в [email protected] (т.е. в exe), на вход брать $^ (т.е. всё от чего зависит [email protected])
%.o: %.c 3.h                            # объектники зависят от исходников с таким же именем, а так же от 3.h
        $(CC) -c $(CFLAGS) -o [email protected] $<     # чтобы собрать объектник -- вызвать компилятор с ключом -c, передать ему CFLAGS, выводить в [email protected] (т.е. в %.o), компилировать $< (т.е. %.с с тем же именем)
clean:                                  # традиционно очистка всего называется clean
        -rm -f $(OBJS) exe              # для очистки удалить объектники и exe
.PHONY: all clean                       # all и clean -- это не файлы

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question