【发布时间】:2015-06-25 05:39:05
【问题描述】:
我正在设计我的第一个 C++ 程序,但我很难理解如何安装和使用名为 pugixml 的第三方库。
这是我的结构:
- 根/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 文件放入包含中。
问题
- 各种 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;
}
}
【问题讨论】:
-
您不认为您的first C++ 程序应该没有第三方库吗?不一定是因为你不能写它,但例如。测试您的编译器是否安装正确并按预期工作。
-
@deviantfan 我已经做了一个 hello world,但后来我想我会跳到最深处,所以从技术上讲,这是我的第二个。
-
如果没有看到代码,我们将无法帮助您。至少包含您的标题的部分。
-
另外,如果您正在构建“pugi”源,您应该将所有源文件和头文件放在同一个目录中,而忘记 -I include 语句。或者,考虑将它们构建到一个库中,然后将此类库链接到您的可执行文件。
-
@FranciscoAguilera 如果有帮助,我已经添加了代码