2017-06-21 53 views
0

我不知道是否有任何隱式編譯目標文件的方式。這是「Learn C the Hard Way」一書中的Makefile。這些對象文件是如何在Makefile中編譯的?

CFLAGS=-g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG $(OPTFLAGS) 
LIBS=-ldl $(OPTLIBS) 
PREFIX?=/usr/local 

SOURCES=$(wildcard src/**/*.c src/*.c) 
OBJECTS=$(patsubst %.c,%.o,$(SOURCES)) 

TEST_SRC=$(wildcard tests/*_tests.c) 
TESTS=$(patsubst %.c,%,$(TEST_SRC)) 

TARGET=build/liblcthw.a 
SO_TARGET=$(patsubst %.a,%.so,$(TARGET)) 

# The Target Build 
all: $(TARGET) $(SO_TARGET) tests 

dev: CFLAGS=-g -Wall -Isrc -Wall -Wextra $(OPTFLAGS) 
dev: all 

$(TARGET): CFLAGS += -fPIC 

$(TARGET): build $(OBJECTS) 
    ar rcs [email protected] $(OBJECTS) 
    ranlib [email protected] 
$(SO_TARGET): $(TARGET) $(OBJECTS) 
    $(CC) -shared -o [email protected] $(OBJECTS) 

build: 
    @mkdir -p build 
    @mkdir -p bin 

# The Unit Tests 
.PHONY: tests 
tests: CFLAGS += $(TARGET) 
tests: $(TESTS) 
    sh ./tests/runtests.sh 

# The Cleaner 
clean: 
    rm -rf build $(OBJECTS) $(TESTS) 
    rm -f tests/tests.log 
    find . -name "*.gc*" -exec rm {} \; 
    rm -rf `find . -name "*.dSYM" -print` 

# The Install 
install: all 
    install -d $(DESTDIR)/$(PREFIX)/lib/ 
    install $(TARGET) $(DESTDIR)/$(PREFIX)/lib/ 

# The Checker 
check: 
    @echo Files with potentially dangerous functions. 
    @egrep '[^_.>a-zA-Z0-9](str(n?cpy|n?cat|xfrm|?dup|str|pbrk|tok|_)\ 
     |stpn?cpy|a?sn?printf|byte_)' $(SOURCES) || true 

我沒有看到,編譯.c文件.o文件中的任何行。但是當我運行make時,結果如下:

cc -g -O2 -Wall -Wextra -Isrc -rdynamic -DNDEBUG -fPIC -c -o src/lcthw/list.o src/lcthw/list.c 
ar rcs build/liblcthw.a src/lcthw/list.o 
ranlib build/liblcthw.a 

任何人都可以解釋嗎?

回答

0

知道如何編譯.c文件到相應的.o文件 - 這是一個內置的規則。與語言類似。它也知道如何從單個源文件構建一個程序。等

您可以使用make -p -f /dev/null來查看make定義的所有內置規則和默認宏。我從GNU Make 3.81獲得的部分輸出是:

… 
LINK.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) $(TARGET_ARCH) 
… 
COMPILE.c = $(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c 
… 
%: %.c 
# commands to execute (built-in): 
    $(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o [email protected] 
… 
%.o: %.c 
# commands to execute (built-in): 
    $(COMPILE.c) $(OUTPUT_OPTION) $< 
… 
相關問題