我正在设计我的第一个C++程序,但是我很难理解如何安装和使用名为pugixml的第三方库。

这是我的结构:

  • root / Makefile
  • root / src / main.cpp
  • root / include / pugi / pugixml.hpp
  • root / include / pugi / pugixml.cpp
  • root / include / pugi / pugiconfig.hpp

  • 我相信我打算将所有的pugixml都转储到我的include文件夹中的pug文件夹中,然后像这样从我的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
    

    有人告诉我这是由于您已将pugi cpp文件放入include中。

    问题
  • 各种pugi文件应该放在哪里,如果没有放到哪里?
  • 我应该如何相应地修改我的Makefile?

  • 代码编辑
    #include "pugi/pugixml.hpp"
    
    #include <iostream>
    #include <string>
    #include <map>
    
    int main() {
        pugi::xml_document doca, docb;
        std::map<std::string, pugi::xml_node> mapa, mapb;
    
        if (!doca.load_file("a.xml") || !docb.load_file("b.xml"))
            return 1;
    
        for (auto& node: doca.child("site_entries").children("entry")) {
            const char* id = node.child_value("id");
            mapa[id] = node;
        }
    
        for (auto& node: docb.child("site_entries").children("entry"))
            const char* idcs = node.child_value("id");
            if (!mapa.erase(id)) {
                mapb[id] = node;
            }
        }
    

    最佳答案

    我现在无法验证,但是很可能您只需要用pugi/pugixml.cpp替换include/pugi/pugixml.cpp即可。

    09-08 09:20