Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 链接器命令-没有规则为第三方库创建目标_C++_C++11_Makefile - Fatal编程技术网

C++ 链接器命令-没有规则为第三方库创建目标

C++ 链接器命令-没有规则为第三方库创建目标,c++,c++11,makefile,C++,C++11,Makefile,在我的代码中,我引用了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: link

在我的代码中,我引用了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
make: *** No rule to make target `pugi/pugixml.cpp', needed by `main'.  Stop.
根据另一个问题,我被告知将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时,会出现以下错误:

  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
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
    文件放在那里

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


我无法复制此问题。您的文件系统上确实有
pugi/pugixml.cpp
吗?@myaut我有,在与.hppI相同的目录中,您认为可以使用
Makefile
从目录开始发布文件系统层次结构helpful@myaut编辑!希望这有助于了解情况。我真的很感激你的帮助。非常感谢你的帮助。我是否需要将.coo与.hpp保存在同一文件夹中?我是新手,那么你提到的第一种方法,哪种方法是最标准和推荐的方法?这更像是一个库而不是一个包含吗?@JamesWillson:include子目录(.hpp)中的源文件(.cpp)令人困惑。您可以这样做,它也会起作用,但如果您将源代码显示给其他人,他可能无法找到
pugixml.cpp
,因为hi不希望它位于
include/
子目录中