【问题标题】:Libxml program in CPPCPP 中的 Libxml 程序
【发布时间】:2015-02-25 10:46:42
【问题描述】:

我用C++写了一个程序来演示libxml2的使用,代码如下

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
void parseStory (xmlDocPtr doc, xmlNodePtr cur) {
 xmlChar *key;
cur = cur->xmlChildrenNode;
while (cur != NULL) {
    if ((!xmlStrcmp(cur->name, (const xmlChar *)"keyword"))) {
        key = xmlNodeListGetString(doc, cur->xmlChildrenNode, 1);
        printf("keyword: %s\n", key);
        xmlFree(key);
    }
cur = cur->next;
}
return;
}

static void parseDoc(char *docname) {

xmlDocPtr doc;
xmlNodePtr cur;

doc = xmlParseFile(docname);

if (doc == NULL ) {
    fprintf(stderr,"Document not parsed successfully. \n");
    return;
}

cur = xmlDocGetRootElement(doc);

if (cur == NULL) {
    fprintf(stderr,"empty document\n");
    xmlFreeDoc(doc);
    return;
}

if (xmlStrcmp(cur->name, (const xmlChar *) "story")) {
    fprintf(stderr,"document of the wrong type, root node != story");
    xmlFreeDoc(doc);
    return;
}

cur = cur->xmlChildrenNode;
while (cur != NULL) {
    if ((!xmlStrcmp(cur->name, (const xmlChar *)"storyinfo"))){
        parseStory (doc, cur);
    }

cur = cur->next;
}

xmlFreeDoc(doc);
return;
}

int main(int argc, char **argv) {

char *docname;

if (argc <= 1) {
    printf("Usage: %s docname\n", argv[0]);
    return(0);
}

docname = argv[1];
parseDoc (docname);

return (1);
 }

我使用的是 Ubuntu,我已经安装了 libxml2-dev 包并使用

编译
g++ Libxml2Example.cpp -I/usr/include/libxml2/libxml -lxml2 -o output

但我收到以下错误

Build of configuration Debug for project Libxml2Example ****
make all 
Building file: ../src/Libxml2Example.cpp
 Invoking: GCC C++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Libxml2Example.d" -      MT"src/Libxml2Example.d" -o"src/Libxml2Example.o" "../src/Libxml2Example.cpp"
../src/Libxml2Example.cpp:12:30: fatal error: libxml/xmlmemory.h: No such file or     directory
compilation terminated.
make: *** [src/Libxml2Example.o] Error 1

如何解决这个错误?请帮助我。

【问题讨论】:

  • 您使用哪种操作系统?您需要在编译中添加 -I。
  • 我在 ubuntu 中运行这个
  • 请原谅我,但在c++ 中使用#include &lt;stdio.h&gt; 不是非常规的吗?恕我直言,您应该考虑使用#include &lt;cstdio&gt;

标签: c++ c eclipse ubuntu


【解决方案1】:

目录libxml 必须存在于由以下任何环境变量指定的现有隐式搜索包含路径中:

CPATH
CPLUS_INCLUDE_PATH
GCC_INCLUDE_DIR

或其路径必须由 `-I 显式指定。

我敢打赌,在编译后,您在链接、查找 libxml.a 文件(需要设置-L&lt;path&gt;)或设置环境变量LIBRARY_PATH 时也会遇到类似的问题。

通常应该避免使用环境变量方法,以使您的项目及其构建文件可移植到其他安装,而无需设置构建环境。

【讨论】:

  • iam 使用以下命令编译它 g++ Libxml2Example.cpp -I/usr/include/libxml2/libxml -lxml2 -o 输出现在 iam 收到此错误:致命错误:libxml/xmlversion.h:否这样的文件或目录
  • @Chinnu 只需删除编译命令的最后一个libxml。这个目录命令包含在#include &lt;libxml/xmlmemory.h&gt;
  • @Chinnu :这是一个不同的问题。要么更新这个问题,要么开始一个新问题。
【解决方案2】:

首先需要安装libxml2的开发包

​​>
sudo apt-get install libxml2-dev

这将安装库include 文件。

在你的项目中你需要用-I/usr/include/libxml2/编译

g++ -O0 -g3 -Wall ../src/Libxml2Example.cpp -I/usr/include/libxml2/ -lxml2 -L/usr/lib/x86_64-linux-gnu/

在 x86_64 ubuntu 系统中,这些库安装在 /usr/lib/x86_64-linux-gnu/ 中。

【讨论】:

  • 但是在执行这个命令时没有生成a.out文件而是创建了一个.o文件,为什么??
  • @Chinnu 你应该删除-c。此标志仅创建目标文件。我编辑了...
猜你喜欢
  • 1970-01-01
  • 2018-01-14
  • 1970-01-01
  • 1970-01-01
  • 2021-05-26
  • 2017-11-15
  • 1970-01-01
  • 1970-01-01
  • 2020-08-31
相关资源
最近更新 更多