gpt4 book ai didi

c++ - 如何在 Qt 项目中包含库

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:45:00 26 4
gpt4 key购买 nike

我正在尝试创建一个使用 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 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

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

enter image description here

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

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

每当我尝试在 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....

最佳答案

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

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

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

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com