如何在QT项目中包含外部库?

时间:2012-05-10 08:34:30

标签: c++ qt qmake

  

可能重复:
  Adding external library into Qt Creator project

这是我的QT项目。我添加了外部库。当我运行这个程序时,会出现以下错误: - : - “1:错误:找不到-libxml2”,当我删除“LIBS + = -L / usr / local / lib -libxml2”行时,显示“undefined”

“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();
}

1 个答案:

答案 0 :(得分:2)

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

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

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

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