我正在制作一次,它编译了我制作的一些排序算法。
我有这样的结构:
|/sorting_algorithms
|----/bin
|----/build
|----/include
|--------tiempo.h
|----/src
|--------bubble_sort.c
|--------enhanced_bubble_sort.c
|--------selection_sort.c
|--------insertion_sort.c
|--------shellsort.c
|--------heapsort.c
|--------tiempo.c
|----makefile
我的makefile是这样的:
CC := gcc
CFLAGS := -g -Wall
SRCDIR := src
BUILDDIR := build
TARGETDIR := bin
SRCEXT := c
INC := -I include
# need to create rhis object code,before binding code with objects to make the binaries
TIME_CODE = tiempo.$(SRCEXT)
TIME_OBJECT = $(BUILDDIR)/tiempo.o
# get source list
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
# create object list
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
# create target list
TARGETS := $(patsubst $(BUILDDIR)/%,$(TARGETDIR)/%,$(OBJECTS:%.o=%))
# it this is necesary?
ALL: $(TIME_OBJECT) $(TARGETS)
$(TIME_OBJECT):
@mkdir -p $(BUILDDIR)
$(CC) $(CFLAGS) $(SRCDIR)/$(TIME_CODE) -c $(INC) -o $(TIME_OBJECT)
$(TARGETS): $(OBJECTS)
@echo "$(CC) -o $@ $< $(TIME_OBJECT)"
@$(CC) -o $@ $< $(TIME_OBJECT)
$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
@echo "$(CC) $(CFLAGS) $(INC) -c $< -o $@"
@$(CC) $(CFLAGS) $(INC) -c $< -o $@
clean:
@echo "$(RM) -r $(BUILDDIR) $(TARGETDIR)/*"
@$(RM) -r $(BUILDDIR) $(TARGETDIR)/*
.PHONY: clean
[我使用 tiempo.h 库来测量时间,这就是我使用它的原因]
所以,当我用这个编译时,它会正确创建目标代码,但是在创建二进制文件时,自动变量'$<'只给我带来一个值,即“build/bubble_sort.o”,所以它创建相同的二进制文件,但名称不同,输出如下:
gcc -g -Wall src/tiempo.c -c -I include -o build/tiempo.o
gcc -g -Wall -I include -c src/bubble_sort.c -o build/bubble_sort.o
gcc -g -Wall -I include -c src/enhanced_bubble_sort.c -o build/enhanced_bubble_sort.o
gcc -g -Wall -I include -c src/heapsort.c -o build/heapsort.o
gcc -g -Wall -I include -c src/insertion_sort.c -o build/insertion_sort.o
gcc -g -Wall -I include -c src/selection_sort.c -o build/selection_sort.o
gcc -g -Wall -I include -c src/shellsort.c -o build/shellsort.o
gcc -o bin/bubble_sort build/bubble_sort.o build/tiempo.o
gcc -o bin/enhanced_bubble_sort build/bubble_sort.o build/tiempo.o
gcc -o bin/heapsort build/bubble_sort.o build/tiempo.o
gcc -o bin/insertion_sort build/bubble_sort.o build/tiempo.o
gcc -o bin/selection_sort build/bubble_sort.o build/tiempo.o
gcc -o bin/shellsort build/bubble_sort.o build/tiempo.o
gcc -o bin/tiempo build/bubble_sort.o build/tiempo.o
另外,如果您能给我一些构建 makefile 的提示、良好做法或告诉我可以做些什么来提高性能,我将不胜感激。