68

I want to experiment with GCC whole program optimizations. To do so I have to pass all C-files at once to the compiler frontend. However, I use makefiles to automate my build process, and I'm not an expert when it comes to makefile magic.

How should I modify the makefile if I want to compile (maybe even link) using just one call to GCC?

For reference - my makefile looks like this:

LIBS  = -lkernel32 -luser32 -lgdi32 -lopengl32
CFLAGS = -Wall

OBJ = 64bitmath.o    \
      monotone.o     \
      node_sort.o    \
      planesweep.o   \
      triangulate.o  \
      prim_combine.o \
      welding.o      \
      test.o         \
      main.o

%.o : %.c
    gcc -c $(CFLAGS) $< -o $@

test: $(OBJ)
    gcc -o $@ $^ $(CFLAGS) $(LIBS)

3 Answers 3

71
LIBS  = -lkernel32 -luser32 -lgdi32 -lopengl32
CFLAGS = -Wall

# Should be equivalent to your list of C files, if you don't build selectively
SRC=$(wildcard *.c)

test: $(SRC)
    gcc -o $@ $^ $(CFLAGS) $(LIBS)
8
  • Why should it be the actual list of C files instead of wildcarding?
    – nornagon
    Jun 28, 2011 at 2:00
  • 12
    @nornagon, s/should be/should be equivalent to/
    – Alex B
    Jun 28, 2011 at 2:02
  • 16
    @AlexB Speak English. What does that even mean?
    – Overv
    Jul 13, 2012 at 22:39
  • 22
    @Overv it's sed syntax, meaning replace "should be" with "should be equivalent to"
    – kirb
    Jul 19, 2012 at 8:00
  • 4
    This makefile is explained at stackoverflow.com/questions/3932895/makefile-aliases/…
    – naught101
    Apr 7, 2014 at 12:33
64
SRCS=$(wildcard *.c)

OBJS=$(SRCS:.c=.o)

all: $(OBJS)
1
  • This strikes me as not actually an answer to the original question, as it's likely to do many calls to ${CC}. That said, it was essentially what I wanted for what I was trying to do. (I had a conversion I wanted to do on a bunch of text files, so I added my own .SUFFIXES line and such, but the $(wildcard) and the $(VARIABLE:.ext1=.ext2) conversion were exactly what I was looking for.) Explicitly not voting either up or down, though I'd say it's arguable that this deserves a down-vote for this question.
    – lindes
    Apr 30, 2015 at 21:26
4

You need to take out your suffix rule (%.o: %.c) in favour of a big-bang rule. Something like this:

LIBS  = -lkernel32 -luser32 -lgdi32 -lopengl32
CFLAGS = -Wall

OBJ = 64bitmath.o    \
      monotone.o     \
      node_sort.o    \
      planesweep.o   \
      triangulate.o  \
      prim_combine.o \
      welding.o      \
      test.o         \
      main.o

SRCS = $(OBJ:%.o=%.c)

test: $(SRCS)
    gcc -o $@  $(CFLAGS) $(LIBS) $(SRCS)

If you're going to experiment with GCC's whole-program optimization, make sure that you add the appropriate flag to CFLAGS, above.

On reading through the docs for those flags, I see notes about link-time optimization as well; you should investigate those too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.