代码之家  ›  专栏  ›  技术社区  ›  Nils Pipenbrinck

makefiles-一次编译所有C文件

  •  55
  • Nils Pipenbrinck  · 技术社区  · 16 年前

    我想用gcc整个程序优化进行试验。要做到这一点,我必须立即将所有C文件传递给编译器前端。但是,我使用makefiles来自动化构建过程,在makefile magic方面我不是专家。

    如果我只想使用一个对gcc的调用来编译(甚至链接),我应该如何修改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)
    
    3 回复  |  直到 11 年前
        1
  •  56
  •   Alex B    13 年前
    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)
    
        2
  •  40
  •   Danica MrDrFenner    12 年前
    SRCS=$(wildcard *.c)
    
    OBJS=$(SRCS:.c=.o)
    
    all: $(OBJS)
    
        3
  •  1
  •   James McPherson    11 年前

    你需要拿出你的后缀规则(%.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。

    在阅读这些标志的文档时,我看到了关于链接时间的注释 优化,你也应该研究一下。