61

我想尝试 GCC 整个程序优化。为此,我必须一次将所有 C 文件传递​​给编译器前端。但是,我使用 makefile 来自动化我的构建过程,而且我不是 makefile 魔术方面的专家。

如果我想只使用一次对 GCC 的调用来编译(甚至可能是链接),我应该如何修改 makefile?

供参考 - 我的 makefile 如下所示:

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)
4

3 回答 3

63
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)
于 2008-10-04T15:00:58.617 回答
53
SRCS=$(wildcard *.c)

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

all: $(OBJS)
于 2010-11-02T03:47:52.503 回答
2

您需要去掉后缀规则 (%.o: %.c) 以支持大爆炸规则。像这样的东西:

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)

如果您要试验 GCC 的整个程序优化,请确保在上面的 CFLAGS 中添加适当的标志。

在阅读这些标志的文档时,我也看到了有关链接时间优化的注释;你也应该调查这些。

于 2014-02-03T13:15:49.400 回答