2

可能重复:
将外部库添加到 Qt Creator 项目中

这是我的 QT 项目。我添加了外部库。当我运行这个程序时,出现以下错误:-:-“1:错误:找不到-libxml2”,当我删除“LIBS += -L/usr/local/lib -libxml2”行时,它显示“未定义参考“xmlstrcmp”和更多错误与此相同。提前致谢。

“Test.pro”文件(项目文件):-

QT       += core gui xml
TARGET = test
TEMPLATE = app

SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

INCLUDEPATH = /usr/local/include/libxml2

LIBS += -L/usr/local/lib -libxml2

***"MainWindow.h" File(Header File):-***

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

#include <libxml/parser.h>
#include <libxml/xmlmemory.h>
#include <libxml/xmlstring.h>
#include <libxml/list.h>
#include <libxml/tree.h>
#include <libxml/SAX.h>

namespace Ui {
    class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

    void parseDocument(char *docName);
    void parse(xmlDocPtr doc, xmlNodePtr cur);

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

“MainWindow.cpp”(类文件):-

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    char *docName;

    docName = "/home/garima/Documents/test-build-desktop/test.xml";
    parseDocument(docName);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::parseDocument(char *docName)
{
    xmlDocPtr doc;
    xmlNodePtr cur;

    doc = xmlParseFile(docName);
    if(doc == NULL)
    {
        qDebug() << "Document is not parsed successfully.";
        return;
    }

    cur = xmlDocGetRootElement(doc);

    if(cur == NULL)
    {
        qDebug() << "Document is empty.";
        xmlFreeDoc(doc);
        return;
    }

    if(xmlStrcmp(cur->name,(const xmlChar *) "story"))
    {
        qDebug() << "document is of wrong type. Story is not a root node.";
        xmlFreeDoc(doc);
        return;
    }

    cur = cur->xmlChildrenNode;

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

        cur = cur->next;
    }

    xmlFreeDoc(doc);
    return;
}

void MainWindow::parse(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);
            qDebug() << "Key:" << key;
            xmlFree(key);
        }

        cur = cur->next;
    }

    return;
}

**"Main.cpp" (Main Class) :-**

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    return a.exec();
}
4

1 回答 1

4
LIBS += -L/usr/local/lib -libxml2

我认为-libxml2寻找一个名为“ibxml2”的库,这可能不是你想要的。

如果您正在使用一个名为“libxml2”的库,我会尝试使用

LIBS += -L/usr/local/lib -lxml2
于 2012-05-10T11:53:00.080 回答