简体   繁体   English

Makefile没有指定源文件

[英]Makefile without specifying the source file

I have a makefile as follows: 我有一个makefile如下:

# Makefile for VocabLearn

MACHTYPE=$(shell uname -m)

GCC         = g++

CC=gcc
# OPTFLAGS=-g2
OPTFLAGS=-O3 -ffast-math -Wall -mfpmath=sse -msse2 -funroll-loops -march=core2
OTHERFLAGS=-Wall -fopenmp

INCLUDE_PATH=-I../lib/ann_1.1/include/ANN -I../lib/ann_1.1_char/include/ANN \
    -I../lib/imagelib -I../VocabLib -I../lib/zlib/include
LIB_PATH=-L../lib -L../VocabLib -L../lib/zlib/lib

OBJS=VocabLearn.o

LIBS=-lvocab -lANN -lANN_char -limage -lz

CPPFLAGS=$(INCLUDE_PATH) $(LIB_PATH) $(OTHERFLAGS) $(OPTFLAGS)

BIN=VocabLearn

all: $(BIN)

$(BIN): $(OBJS)
    g++ -o $(CPPFLAGS) -o $(BIN) $(OBJS) $(LIBS)

clean:
    rm -f *.o *~ $(LIB)

When I 'make' it in the prompt, it works fine and output the following info:(I use Mac OS, c++ means clang compiler) 当我在提示中'make'时,它工作正常并输出以下信息:(我使用Mac OS,c ++表示clang编译器)

c++ -I../lib/ann_1.1/include/ANN -I../lib/ann_1.1_char/include/ANN -I../lib/imagelib -I../VocabLib -I../lib/zlib/include -L../lib -L../VocabLib -L../lib/zlib/lib -Wall -fopenmp -O3 -ffast-math -Wall -mfpmath=sse -msse2 -funroll-loops -march=core2 -c -o VocabLearn.o VocabLearn.cpp c ++ -I ../ lib / ann_1.1 / include / ANN -I ../ lib / ann_1.1_char / include / ANN -I ../ lib / imagelib -I ../ VocabLib -I ../ lib / zlib / include -L ../ lib -L ​​../ VocabLib -L ​​../ lib / zlib / lib -Wall -fopenmp -O3 -ffast-math -Wall -mfpmath = sse -msse2 -funroll-loops -march = core2 -c -o VocabLearn.o VocabLearn.cpp

g++ -o -I../lib/ann_1.1/include/ANN -I../lib/ann_1.1_char/include/ANN -I../lib/imagelib -I../VocabLib -I../lib/zlib/include -L../lib -L../VocabLib -L../lib/zlib/lib -Wall -fopenmp -O3 -ffast-math -Wall -mfpmath=sse -msse2 -funroll-loops -march=core2 -o VocabLearn VocabLearn.o -lvocab -lANN -lANN_char -limage -lz g ++ -o -I ../ lib / ann_1.1 / include / ANN -I ../ lib / ann_1.1_char / include / ANN -I ../ lib / imagelib -I ../ VocabLib -I ../ lib / zlib / include -L ../ lib -L ​​../ VocabLib -L ​​../ lib / zlib / lib -Wall -fopenmp -O3 -ffast-math -Wall -mfpmath = sse -msse2 -funroll-loops - march = core2 -o VocabLearn VocabLearn.o -lvocab -lANN -lANN_char -limage -lz

I just want to know how this makefile works. 我只是想知道这个makefile是如何工作的。 As you can see, since this makefile doesn't specify which source code to compile, how does the compiler know it is 'VocabLearn.cpp' that it should process? 如您所见,由于此makefile未指定要编译的源代码,编译器如何知道它应该处理的是“VocabLearn.cpp”? (My guess is that it will search source file according to the name of the object file, VocabLearn.o) Also this line seems a bit strange for me: (我的猜测是它将根据目标文件的名称搜索源文件,VocabLearn.o)这条线对我来说似乎有点奇怪:

g++ -o $(CPPFLAGS) -o $(BIN) $(OBJS) $(LIBS)

Why is there a '-o' before '$(CPPFLAGS)'? 为什么'$(CPPFLAGS)'之前有'-o'?

This makefile is using implicit rules to compile the source files. 此makefile使用隐式规则来编译源文件。 The rule: 规则:

$(BIN): $(OBJS)

asks for the object files in OBJS , and make already knows how to build VocabLearn.o if there is a file VocabLean.cpp . 要求在目标文件OBJS ,并已经知道如何建立VocabLearn.o如果有文件VocabLean.cpp

Basically there is an implicit rule to convert *.cpp files to *.o files, however you have to have *.o as a dependency in one your targets. 基本上有一个隐式规则将* .cpp文件转换为* .o文件,但是你必须在一个目标中使用* .o作为依赖项。 In the given Makefile, you have VocabLearn.o as a dependency for $(BIN). 在给定的Makefile中,您将VocabLearn.o作为$(BIN)的依赖项。 So, VocabLearn.o gets auto-generated from VocabLearn.cpp file. 因此,VocabLearn.o从VocabLearn.cpp文件中自动生成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM