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

标签 c++ qt qmake

Possible Duplicate:
Adding external library into Qt Creator project

这是我的 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();
}

最佳答案

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

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

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

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

关于c++ - 如何在QT项目中包含外部库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10532538/

相关文章:

c++ - qmake在命令行中分配变量

c++ - Qt 如何知道何时连接了新的 USB 存储设备?

c++ - QLineEdit 在 Qt 中调用 SetText() 后无法撤消

c++ - 体系结构 x86_64 的 undefined symbol :cv::CascadeClassifier::CascadeClassifier()

c++ - 迭代 C++ 中的类继承

mysql - 无法使用 qt 4.8.5 和 Ubuntu 12.04 连接到 mariadb 数据库服务器

cmake - SCons 或 CMake 而不是 qmake

c++ - 按空格拆分一串正/负整数

c++ - 指向采用抽象参数的函数的指针

c++ - 禁止 Clang-Format 乱用评论