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

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

本文关键字:目标 规则 第三方 命令 链接      更新时间:2023-10-16

在我的代码中,我像这样引用这个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 作为一个额外的翻译单元,并在我的 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 中给它不是自己的"目标"?

编辑这是我的文件系统:

  • 根/生成文件
  • root/src/main.cpp
  • root/include/pugi/pugixml.hpp
  • root/include/pugi/pugixml.cpp
  • root/include/pugi/pugiconfig.hpp
在 Makefile 中,

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

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

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