c++ - 在 OSX 山狮上使用带有 Qt 5 的 libzip

标签 c++ qt zip unzip

我正在尝试制作一个简单的训练程序,使用 libzip 列出存档的内容(目前的目标 :)。我正在使用 libzip 源。

.pro 文件:

QT += widgets

SOURCES += \
    main.cpp \
    myZip.cpp \

INCLUDEPATH = /path/to/libzip

HEADERS += \
    myZip.h \

MCZip.h

#include <QtWidgets>
#include "zip.h"

class myZip : public QDialog
{
public:
    myZip(const char* filePath);

private:
    QTextEdit *m_fileListView;
    QString m_fileList;

    QPushButton *m_closeButton;

};

我的Zip.cpp

#include "my.h"

myZip::myZip(const char* filePath)
{
    setFixedSize(400, 400);

    m_fileListView = new QTextEdit;
    m_closeButton = new QPushButton("Close");

    int err = 0;
    struct zip *f_zip=NULL;

    f_zip = zip_open(filePath, ZIP_CHECKCONS, &err);

    if(err != ZIP_ER_OK)
    {
        QMessageBox::critical(this, "Error", "Cannot open the file!");
        return;
    }

    if(f_zip == NULL)
    {
        QMessageBox::critical(this, "Error", "File not found");
        return;
    }

    int fileCount = zip_get_num_files(f_zip);
    if (fileCount ==-1)
    {
        QMessageBox::critical(this, "Error", "The archive seems corrupted");
        return;
    }

    qDebug() << QString("There are %1 files in the archive").arg(fileCount);

    for (int i = 0; i < fileCount; i++)
    {
        m_fileList += QString(QString::fromLocal8Bit(zip_get_name(f_zip, i, ZIP_FL_UNCHANGED)) + "\n");
    }

    zip_close(f_zip);
    f_zip = NULL;

    m_fileListView->setText(m_fileList);

    QHBoxLayout *layout = new QHBoxLayout;
    layout->addWidget(m_fileListView);
    layout->addWidget(m_closeButton);

    setLayout(layout);

    QObject::connect(m_closeButton, SIGNAL(clicked()), this, SLOT(close()));

}

主要.cpp

#include <QApplication>
#include "MCCover.h"

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    QString filePath = QFileDialog::getOpenFileName(this, "Open a file", QString(),
                                             "Zip archive (*.zip)"); 

    QByteArray byteArray = filePath.toUtf8();
    const char* cFilePath = byteArray.constData();

    myZip *fileList = new myZip(cFilePath);
    fileList.show();

    return app.exec();
}

问题是当我编译时,我收到以下消息:

symbol(s) not found for architecture x86_64
collect2: ld return exit status

当我查看输出时

Undefined symbols for architecture x86_64:
        "_zip_close", referenced from:
              myZip::myZip(char const*)in myZip.o
              myZip::myZip(char const*)in myZip.o
        "_zip_get_name", referenced from:
              myZip::myZip(char const*)in myZip.o
              myZip::myZip(char const*)in myZip.o
        "_zip_get_num_files", referenced from:
              myZip::myZip(char const*)in myZip.o
              myZip::myZip(char const*)in myZip.o
        "_zip_open", referenced from:
              myZip::myZip(char const*)in myZip.o
              myZip::myZip(char const*)in myZip.o
 ld: symbol(s) not found for architecture x86_64
 collect2: ld returned 1 exit status

关于如何解决问题的任何想法?我找不到任何解决方案...

关于如何使用 libzip [法语] 的信息,我遵循了这个教程: http://slash.developpez.com/tutoriels/c/utilisation-libzip/

配置:OSX Mountain Lion 上的 Qt 5.0.0,带有 clang_64 编译器(默认)

谢谢

死侍

PS:修改代码时,qt 4.8.1 和gcc 都不行。

最佳答案

你有两个选择:

  • 将 libzip 构建为静态库,然后将您的项目链接到该静态库
  • 将 libzip 的源文件添加到您的项目中

关于c++ - 在 OSX 山狮上使用带有 Qt 5 的 libzip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14249327/

相关文章:

C++ 使用 Map 中的 Map 检查键是否存在

c++ - WiFi 工作时,您可以使用所有 ESP32 的 GPIO 引脚吗?

java - 如何使用 Java 读取 Winzip 自解压 (exe) zip 文件?

c++ - 更改 QTabWidget 的小部件的内容,只知道选项卡索引

linux - Qt 链接错误

java - 下载和解压缩过程仍在运行

c++ - zip 文件可以是稀疏的/不连续的吗?

c++ - 我的字符串末尾有 4 个相同的垃圾字符

c++ - 如何使用 Qt 在 Opencv 2.4.9 中创建工具栏

c++ - QT:如何同时打开多个窗口(QWidgets)?