问题描述
我想在 Windows 7 中加载 qt5 psql 驱动程序.
I want to load qt5 psql driver in windows 7.
我以这种方式加载了库:
I've load the library as this way:
qDebug() << QCoreApplication::libraryPaths ();
QString driverName = "QPSQL";
QSqlDatabase::addDatabase(driverName,"A connection to postgres");
输出显示:
("C:/QT/Qt5.1.0/5.1.0/msvc2010/plugins", "D:/xxx/build-xxx-Desktop_Qt_5_1_0_MSVC2010_32bit-Release/release")
QSqlDatabase: QPSQL driver not loaded
QSqlDatabase: available drivers: QSQLITE QODBC QODBC3 QPSQL QPSQL7
并且在目录:C:/QT/Qt5.1.0/5.1.0/msvc2010/plugins/
中,确实有sqldrivers
子目录和qsqlpsql.子目录中的dll、qsqlpsqld.dll、qsqlpsqld.pdb
.
And in directory: C:/QT/Qt5.1.0/5.1.0/msvc2010/plugins/
, there does have sqldrivers
subdirectory and qsqlpsql.dll, qsqlpsqld.dll, qsqlpsqld.pdb
in the subdirectory.
我在 ubuntu 中成功加载了 psql 库.
I load the psql library successful in ubuntu.
而我的.pro文件主要是这样的:
and my .pro file is mainly like this:
QT += core gui sql concurrent
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
unix {
LIBS += -lpython2.7
QMAKE_CXXFLAGS += -std=c++0x
}
win32 {
LIBS += \
C:/Python27/libs/python27.lib
}
我找不到失败的原因.
我需要在 .pro 文件中添加 libpqdll.lib 吗?但是我没有在ubuntu中使用LIBS += -lpq
?
Do I need to add libpqdll.lib in .pro file? But I didn't use LIBS += -lpq
in ubuntu?
顺便说一句:我使用 Visual Studio 2010 在 Windows 7 中构建 libpq 失败.但这是另一个问题.
BTW: I failed to build libpq in windows 7, using visual studio 2010. But this is another problem.
这个问题可能很简单.但我已经尝试了一整天.
The question may be easy. But I've tried a wholely day.
推荐答案
很可能是在之后调用 LoadLibrary
时无法动态链接 qsqlpsql.dll
文件您的程序请求该驱动程序.LoadLibrary
返回与目标 DLL 丢失和目标 DLL 所依赖的 DLL 之一丢失或无法加载相同的错误代码.因此,Qt 很可能会收到未找到库"错误并假设哦,Pg 驱动程序不能在此 Qt 版本中",实际上它存在并且由于依赖性问题、库兼容性问题等而无法加载.
Most likely it's failing to dynamically link the qsqlpsql.dll
file when it invokes LoadLibrary
on it after your program requests that driver. LoadLibrary
returns the same error code for when the target DLL is missing as for when one of the DLLs the target DLL depends on is missing or cannot be loaded. So Qt quite possibly gets a "library not found" error and assumes "oh, the Pg driver must not be in this Qt build", where in fact it's present and failing to load due to dependency issues, library compatibility problems, etc.
要确定加载失败的原因,您可以按照 Frank Osterfeld 建议的那样使用 Dependency Walker (depends.exe
).如果您在 64 位机器上编译 32 位代码,您希望获得 32 位依赖项walker.
To determine why it's failing to load, you can as Frank Osterfeld suggested use a tool like Dependency Walker (depends.exe
). If you're compiling 32-bit code on a 64-bit machine you want to get the 32-bit dependency walker.
它很可能会失败,因为缺少 qsqlpsql.dll
的依赖项 - libpq.dll
或 libpq的库之一代码> 构建需要.Dependency walker 将有助于隔离这一点.也有可能您正在尝试在 32 位程序中加载 64 位
libpq.dll
,反之亦然.
It's quite likely to be failing because there's a dependency of qsqlpsql.dll
missing - either libpq.dll
, or one of the libraries that libpq
build requires. Dependency walker will help isolate this. It's also possible you're trying to load a 64-bit libpq.dll
in a 32-bit program, or vice versa.
您可能还会发现使用进程监视器跟踪程序运行您的程序会提供很多信息.这将产生很多关于它尝试打开/检查哪些文件的详细信息,并有助于追踪诸如意外 PATH
问题之类的事情.
You may also find it informative to run your program with Process Monitor tracing it. This will produce a lot of detail about which files it attempts to open / examine, among other things, and can help track down things like unexpected PATH
issues.
如果您仍然卡住,总有一个选项可以通过调试器逐步完成.
If you're still stuck, there's always the option of stepping through with a debugger.
这篇关于QT5:无法在 Windows 中加载 psql 驱动程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!