避免在Make中进行链接和编译(如果不需要) [英] Avoid linking and compiling in Make if not needed
问题描述
作为这里的后续问题:我的Makefile将执行链接,即使代码中没有更改。为什么?如何避免这种行为,所以 make
如果代码没有改变就不会做任何事情?
OBJS = main_no_mkl.o
SOURCE = main_no_mkl.cpp
HEADER = IO.h
OUT = test
CXX = ../ .. / mpich-install / bin / mpic ++
CXXFLAGS = -I ../../ intel / mkl / include -Wl, - start-group -Wl, - end-group -lpthread -lm -ldl - Wall
LDFLAGS = ../../intel/mkl/lib/intel64/libmkl_scalapack_lp64.a -Wl, - start-group ../../intel/mkl/lib/intel64/libmkl_intel_lp64.a。 ./../intel/mkl/lib/intel64/libmkl_core.a ../../intel/mkl/lib/intel64/libmkl_sequential.a -W1, - end-group ../../intel/mkl /lib/intel64/libmkl_blacs_intelmpi_lp64.a -lpthread -lm -ldl
all:$(OBJS)
$(CXX)$(OBJS)-o $(OUT)$(CXXFLAGS) $(LDFLAGS)
#创建/编译单独的文件>>单独<
main_no_mkl.o:main_no_mkl.cpp
$(CXX)-c main_no_mkl.cpp $(CXXFLAGS)
.PHONY:all
问题是你的全部
目标。它不会生成 all
(也标记为 .PHONY
)。请检查此回答有关 .PHONY
的提醒。 (您违反了第二个 Makefiles规则。)
所以第二个/ etc时间运行 make
(假设 .PHONY
标记不存在)make将查找所有
文件,找不到它,并假设它必须再次创建它。
使用 .PHONY
您将文件查找逻辑短路,只是总是假设需要运行
因此,你基本上已经告诉make 总是运行链接步骤,这样make就可以了。
$ all:$(OUT)
$(OUT):$(OBJS)
$(CXX)$(OBJS)-o $(OUT)$(CXXFLAGS)$(LDFLAGS)
对于运行 make -d
并读取输出的记录,给你。
As a follow-up question from here: My Makefile will do the linking, even if nothing is changed in the code. Why? How can I avoid that behaviour, so that make
won't do anything if the code has not changed?
OBJS = main_no_mkl.o
SOURCE = main_no_mkl.cpp
HEADER = IO.h
OUT = test
CXX = ../../mpich-install/bin/mpic++
CXXFLAGS = -I../../intel/mkl/include -Wl,--start-group -Wl,--end-group -lpthread -lm -ldl -Wall
LDFLAGS = ../../intel/mkl/lib/intel64/libmkl_scalapack_lp64.a -Wl,--start-group ../../intel/mkl/lib/intel64/libmkl_intel_lp64.a ../../intel/mkl/lib/intel64/libmkl_core.a ../../intel/mkl/lib/intel64/libmkl_sequential.a -Wl,--end-group ../../intel/mkl/lib/intel64/libmkl_blacs_intelmpi_lp64.a -lpthread -lm -ldl
all: $(OBJS)
$(CXX) $(OBJS) -o $(OUT) $(CXXFLAGS) $(LDFLAGS)
# create/compile the individual files >>separately<<
main_no_mkl.o: main_no_mkl.cpp
$(CXX) -c main_no_mkl.cpp $(CXXFLAGS)
.PHONY : all
The problem is your all
target. It doesn't generate an all
(and is marked .PHONY
as well) file. Check this answer for a reminder about .PHONY
. (You are violating the second Rule of Makefiles.)
So the second/etc time you run make
(assume the .PHONY
marking wasn't present) make would look for an all
file, not find it, and assume it must need to create it again.
With .PHONY
you short-circuit that file-finding logic and make just always assumes it needs to run the recipe again.
So, essentially, you've told make to always run the linking step so make does that.
Use this instead to fix that problem.
all: $(OUT)
$(OUT): $(OBJS)
$(CXX) $(OBJS) -o $(OUT) $(CXXFLAGS) $(LDFLAGS)
For the record running make -d
and reading through the output would have pointed this out to you.
这篇关于避免在Make中进行链接和编译(如果不需要)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!