链接器命令 - 没有为第 3 方库制定目标的规则

Linker command - No rule to make target for 3rd party lib

在我的代码中,我像这样引用这个 pugixml:

#include "pugi/pugixml.hpp"

编译时出现此错误:

  main in main-bf0b72.o
  "pugi::xml_node::children(char const*) const", referenced from:
      _main in main-bf0b72.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [main] Error 1

另一个问题告诉我 pugi 作为一个额外的翻译单元并且 link 他们在我的 Makefile 中相应地像这样:

CC = g++
CFLAGS = -g -Wall -std=c++11
SRC = src
INCLUDES = include
TARGET = main

all: $(TARGET)

$(TARGET): $(SRC)/$(TARGET).cpp pugi/pugixml.cpp
    $(CC) $(CFLAGS) -I $(INCLUDES) -o $@ $^ 

clean:
    $(RM) $(TARGET)

然而,当我尝试 运行 我的 Makefile 时,我得到这个错误:

make: *** No rule to make target `pugi/pugixml.cpp', needed by `main'.  Stop.

我真的不确定我要做什么。 pugi 应该有自己的 Makefile 来单独构建它,还是我应该在我的 Makefile 中给它不自己的 "target"?

编辑 这是我的文件系统:

在您的 Makefile 中,您尝试编译 pugi/pugixml.cpp 文件,但在文件系统中它位于 include/pugi/pugixml.cpp(相对于 Makefile 本身)。你应该:

  • 创建 root/pugi 目录并将 pugixml.cpp 文件放在那里

  • 或者在您的 Makefile 中将 pugi/pugixml.cpp 替换为 include/pugi/pugixml.cpp(但将源文件保留在 include/ 子目录中是个坏主意)。