【问题标题】:Why is my makefile not generating object files?为什么我的 makefile 没有生成目标文件?
【发布时间】:2020-03-22 04:39:36
【问题描述】:

这是我的生成文件:

LIBS = libxml2.so.2.9.4 
LDFLAGS = -lstdc++ -lpthread -lxml2

vpath %.hpp ./
vpath %.cpp ./
vpath %.h ./libxml2/include/libxml

all: KeyGenerator

KeyGenerator: main.o ProjectXmlParser.o xmlparser.o
    g++ ${CXXFLAGS} ${LDFLAGS} -o KeyGenerator $+ ${LIBS}

main.o: main.cpp

ProjectXmlParser.o: ProjectXmlParser.cpp ProjectXmlParser.hpp

xmlparser.o: xmlparser.cpp xmlparser.hpp global.hpp

phony:  clean

clean: 
        rm -f *.o

输出:

/usr/bin/make -f Makefile CONF=Debug
g++ -g -Wall -std=gnu++11 -pthread -I./ -I./src -I./libxml2/include -lstdc++ -lpthread -lxml2 -o KeyGenerator main.o ProjectXmlParser.o xmlparser.o libxml2.so.2.9.4 
g++: error: ProjectXmlParser.o: No such file or directory
g++: error: xmlparser.o: No such file or directory
Makefile:12: recipe for target 'KeyGenerator' failed
make: *** [KeyGenerator] Error 1

在项目目录中,我所有的 hpp 和 cpp 文件都存在于同一级别。我确实有另一个正在使用的名为 libxml 的项目。当我进行构建时,会创建 main.o,但没有创建任何 .o 文件。我已经看到其他帖子提出了这个问题,但到目前为止,没有任何东西对我有用。

谢谢。

【问题讨论】:

  • 您尚未定义如何创建目标文件的方法
  • 您希望 makefile 中的哪一行触发丢失的 .o 文件的生成以及如何触发?
  • 如果注释掉vpath 指令会发生什么?
  • @Jacob GNU Make 为此使用 implicit rules。
  • 根据您提供的信息,您不应该收到此错误,这意味着您没有告诉我们有问题。此处的 makefile 与您的实际 makefile 在某些实质性方面有所不同,或者有些奇怪。您向我们显示了错误消息,但没有显示编译行 make run;这些会很有帮助。此外,您可以运行 make -d 并检查输出以了解 make 如何处理 ProjectXmlParser.o 文件。

标签: c++ makefile


【解决方案1】:

基于make -d 输出:

Must remake target 'ProjectXmlParser.o'.
Successfully remade target file 'ProjectXmlParser.o'.

似乎 make 认为你有一个空的配方来构建这个目标。可能在 ProjectxXmlParser.o 和 xmlparser.o 规则之后,您的 makefile 中有一些杂散的 TAB 字符,而您粘贴到此问题的 makefile 中没有显示这些字符?

【讨论】:

  • 是的...在 xmlparser.o: xmlparser.cpp xmlparser.hpp global.hpp 和 phony: clean 之间的空白行上有一个选项卡
  • 编辑问题时可以看到。好收获!
  • 非常感谢您。
猜你喜欢
  • 2021-04-28
  • 1970-01-01
  • 1970-01-01
  • 2017-10-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多