48

Good Makefiles

 5 years ago
source link: https://www.tuicool.com/articles/hit/vMRNZbj
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

GNU Makefiles and C. One just needs a good template:

BIN = a.out

SRCS = main.c foo.c bar.c
OBJS = $(SRCS:.c=.o)
DEPS = $(SRCS:.c=.d)

CFLAGS = --std=c99 -Wshadow -Wall -pedantic -Wextra -g

LDFLAGS = -lm

$(BIN): $(OBJS)
    gcc $(CFLAGS) $(OBJS) $(LDFLAGS) -o $(BIN)

%.o : %.c Makefile
    gcc $(CFLAGS) -MMD -MP -MT $@ -MF $*.td -c $<
    mv -f $*.td $*.d

%.d: ;
-include *.d

clean:
    rm -f $(BIN) $(OBJS) $(DEPS)

So whats going on here?

While compiling, file header dependencies are stored in dependency files. If a source file is updated it is solely recompiled and relinked. If any header is updated, all source files which included the header are recompiled and relinked. If the Makefile is updated everything is recompiled and relinked.

So what do I do?

Simply add your new source files to SRCS, libraries to LDFLAGS, and more compiler flags like -O3 and -flto to CFLAGS.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK