如何在Qt项目中包含库

时间:2016-11-18 08:07:38

标签: c++ qt taglib

我正在尝试创建一个使用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:配置=释放INSTALL.vcxproj

以相同的方式构建TagLib:

  

cd .... \ taglib-1.11.1   mkdir build&& cd build   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:配置=释放INSTALL.vcxproj

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

enter image description here

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

  

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....

1 个答案:

答案 0 :(得分:0)

您已使用TagLib编译了zlibVisual Studio,并使用mingw编译项目(至少我可以从错误消息中猜出) 。这不会奏效。您需要使用完全相同的编译器编译所有库和应用程序。

Visual Studiogcc有不同的ABI,因此gcc无法看到Visual个符号。