【问题标题】:How to include a library in a Qt project如何在 Qt 项目中包含库
【发布时间】:2016-11-18 08:07:37
【问题描述】:

我正在尝试创建一个使用 TagLib 库的项目。我不确定该怎么做。

我已经下载了TagLib 1.11.1

我是这样构建的:

构建 zlib,首先让 CMake 创建一个 Visual Studio 解决方案文件,然后使用 Visual Studio 构建此解决方案:

mkdir build && cd build cmake .. -G"Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX="e:\workspace\lib\installed" msbuild /P:Configuration=Debug INSTALL.vcxproj msbuild /P:Configuration=Release INSTALL.vcxproj

以同样的方式构建 TagLib:

cd ....\taglib-1.11.1 mkdir 构建 && cd 构建 cmake .. -G"Visual Studio 14 2015 Win64" -DCMAKE_INSTALL_PREFIX="e:\workspace\lib\installed" -DZLIB_INCLUDE_DIR="e:\workspace\lib\installed\include" -DZLIB_LIBRARY="e:\workspace\lib \installed\lib\zlib.lib" -DWITH_ASF=on -DWITH_MP4=on -DBUILD_EXAMPLES=on msbuild /P:Configuration=Release INSTALL.vcxproj

我创建了一个简单的 Qt 控制台应用程序:

然后我使用 Qt 从上面的 TagLib Build 中添加 tag.libE:\workspace\lib\installed\lib 中使用 Qt

Qt -> 添加库 -> 库类型(外部库) -> .......

main.cpp:

#include <QCoreApplication>
#include <QDebug>

#include <taglib/fileref.h>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    TagLib::FileRef f("D:/Dire Straits - Sultans of Swing.mp3");
    return a.exec();
}

taglibtest.pro

QT += core
QT -= gui

CONFIG += c++11

TARGET = taglibtest
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltag
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltagd
else:unix: LIBS += -L$$PWD/taglib/builds/ -ltag

INCLUDEPATH += $$PWD/taglib/builds
DEPENDPATH += $$PWD/taglib/builds

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltag
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltagd
else:unix: LIBS += -L$$PWD/taglib/builds/ -ltag

INCLUDEPATH += $$PWD/taglib/builds
DEPENDPATH += $$PWD/taglib/builds

HEADERS += \
    taglib/aifffile.h \
    taglib/aiffproperties.h \
    taglib/apefile.h \
    taglib/apefooter.h \
    taglib/apeitem.h \
    taglib/apeproperties.h \
    taglib/apetag.h \
    taglib/asfattribute.h \
    taglib/asffile.h \
    taglib/asfpicture.h \
    taglib/asfproperties.h \
    etc....
    etc....

每当我尝试在 Qt 中构建项目时,都会出现以下错误:

F:\taglibtest\main.cpp:-1: error: undefined reference to `_imp___ZN6TagLib8FileNameC1EPKc'  

F:\taglibtest\main.cpp:-1: error: undefined reference to `_imp___ZN6TagLib7FileRefC1ENS_8FileNameEbNS_15AudioProperties9ReadStyleE'  

F:\taglibtest\main.cpp:-1: error: undefined reference to `_imp___ZN6TagLib7FileRefD1Ev'  

F:\taglibtest\main.cpp:-1: error: undefined reference to `_imp___ZN6TagLib7FileRefD1Ev'  

:-1: error: release/main.o: bad reloc address 0x0 in section `.ctors'  

:-1: error: final link failed: Invalid operation  

collect2.exe:-1: error: error: ld returned 1 exit status  

我应该怎么做才能解决这个问题并开始使用 TagLib?

taglibtest.pro

QT += core
QT -= gui

CONFIG += c++11

TARGET = taglibtest
CONFIG += console
CONFIG -= app_bundle

TEMPLATE = app

SOURCES += main.cpp

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltag
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltagd
else:unix: LIBS += -L$$PWD/taglib/builds/ -ltag

INCLUDEPATH += $$PWD/taglib/builds
DEPENDPATH += $$PWD/taglib/builds

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltag
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/taglib/builds/ -ltagd
else:unix: LIBS += -L$$PWD/taglib/builds/ -ltag

INCLUDEPATH += $$PWD/taglib/builds
DEPENDPATH += $$PWD/taglib/builds

HEADERS += \
    taglib/aifffile.h \
    taglib/aiffproperties.h \
    taglib/apefile.h \
    taglib/apefooter.h \
    taglib/apeitem.h \
    taglib/apeproperties.h \
    taglib/apetag.h \
    taglib/asfattribute.h \
    taglib/asffile.h \
    taglib/asfpicture.h \
    taglib/asfproperties.h \
    etc....
    etc....

【问题讨论】:

    标签: c++ qt taglib


    【解决方案1】:

    您已经使用Visual Studio 编译了TagLibzlib,并且使用mingw 编译了您的项目(至少这是我可以从错误消息中猜到的)。这行不通。您需要使用相同的编译器编译所有库和应用程序。

    Visual Studiogcc 有不同的ABI,所以gcc 看不到Visual 的符号。

    【讨论】:

      猜你喜欢
      • 2013-01-18
      • 2012-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-10
      • 1970-01-01
      • 2014-01-30
      • 2015-01-31
      相关资源
      最近更新 更多