Avoid linking and compiling in Make if not needed

断了今生、忘了曾经 提交于 2019-12-05 20:22:01
Etan Reisner

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.

The objects are really the dependencies of your output, and your "all" target should depend on the output(s). So you should do something like this instead:

all: $(OUT)

$(OUT): $(OBJS)
    $(CXX)  $(OBJS) -o  $(OUT)  $(CXXFLAGS) $(LDFLAGS)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!