如何在Qt项目中包含库 [英] How to include a library in a Qt project
问题描述
我正在尝试创建一个使用TagLib库的项目.我不太确定该怎么做.
我已经下载了
然后我使用Qt在 E:\ workspace \ lib \ installed \ lib
中从上述TagLib构建中添加 tag.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-Swings of Swing.mp3");返回a.exec();}
taglibtest.pro
QT + =核心QT-=桂配置+ = c ++ 11TARGET = taglibtestCONFIG + =控制台配置-= app_bundle模板=应用来源+ = main.cppwin32:CONFIG(发布,调试|发布):LIBS + = -L $$ PWD/taglib/builds/-ltagelse:win32:CONFIG(debug,debug | release):LIBS + = -L $$ PWD/taglib/builds/-ltagdelse:unix:LIBS + = -L $$ PWD/taglib/builds/-ltagINCLUDEPATH + = $$ PWD/taglib/内部版本DEPENDPATH + = $$ PWD/taglib/内部版本win32:CONFIG(发布,调试|发布):LIBS + = -L $$ PWD/taglib/builds/-ltagelse:win32:CONFIG(debug,debug | release):LIBS + = -L $$ PWD/taglib/builds/-ltagdelse:unix:LIBS + = -L $$ PWD/taglib/builds/-ltagINCLUDEPATH + = $$ PWD/taglib/内部版本DEPENDPATH + = $$ PWD/taglib/内部版本标题+ = \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 \等等....等等....
每当我尝试在Qt中构建项目时,都会出现以下错误:
F:\ taglibtest \ main.cpp:-1:错误:未定义对_imp___ZN6TagLib8FileNameC1EPKc的引用F:\ taglibtest \ main.cpp:-1:错误:对`_imp___ZN6TagLib7FileRefC1ENS_8FileNameEbNS_15AudioProperties9ReadStyleE'的未定义引用F:\ taglibtest \ main.cpp:-1:错误:对`_imp___ZN6TagLib7FileRefD1Ev'的未定义引用F:\ taglibtest \ main.cpp:-1:错误:对`_imp___ZN6TagLib7FileRefD1Ev'的未定义引用:-1:错误:release/main.o:`.ctors'节中错误的重定位地址0x0:-1:错误:最终链接失败:无效的操作collect2.exe:-1:错误:错误:ld返回1退出状态
该如何解决此问题并开始使用TagLib?
taglibtest.pro
QT + =核心QT-=桂配置+ = c ++ 11TARGET = taglibtestCONFIG + =控制台配置-= app_bundle模板=应用来源+ = main.cppwin32:CONFIG(发布,调试|发布):LIBS + = -L $$ PWD/taglib/builds/-ltagelse:win32:CONFIG(debug,debug | release):LIBS + = -L $$ PWD/taglib/builds/-ltagdelse:unix:LIBS + = -L $$ PWD/taglib/builds/-ltagINCLUDEPATH + = $$ PWD/taglib/内部版本DEPENDPATH + = $$ PWD/taglib/内部版本win32:CONFIG(发布,调试|发布):LIBS + = -L $$ PWD/taglib/builds/-ltagelse:win32:CONFIG(debug,debug | release):LIBS + = -L $$ PWD/taglib/builds/-ltagdelse:unix:LIBS + = -L $$ PWD/taglib/builds/-ltagINCLUDEPATH + = $$ PWD/taglib/内部版本DEPENDPATH + = $$ PWD/taglib/内部版本标题+ = \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 \等等....等等....
您已经使用 Visual Studio
编译了 TagLib
和 zlib
您的项目使用 mingw
(至少我可以从错误消息中猜测出这一点).这行不通.您需要使用完全相同的编译器来编译所有库和应用程序.
Visual Studio
和 gcc
具有不同的 ABI
,因此 gcc
无法看到 Visual
的符号.
I'm trying to create a project that uses the TagLib library. I'm not really sure of how exactly to go about it.
I have downloaded TagLib 1.11.1.
I built it as follows:
Build zlib, by first having CMake create a Visual Studio solution file, then building this solution with 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
Build TagLib much in the same way:
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:Configuration=Release INSTALL.vcxproj
I create a simple Qt Console Application:
I then add tag.lib
from the TagLib Build above in E:\workspace\lib\installed\lib
using Qt
Qt -> Add Library -> Library Type (External Library) -> .......
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....
I get the following errors whenever I try Building the project in 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
What should I do to fix this and get working with 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....
You have compiled TagLib
and zlib
with Visual Studio
and you compile your project with mingw
(at least that's what I can guess from error messages). This won't work. You need to compile all your libraries and apps with the very same compiler.
Visual Studio
and gcc
have different ABI
's so gcc
cannot see Visual
's symbols.
这篇关于如何在Qt项目中包含库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!