首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >没有加载QMYSQL驱动程序(其他解决方案?)

没有加载QMYSQL驱动程序(其他解决方案?)
EN

Stack Overflow用户
提问于 2016-10-06 19:01:25
回答 2查看 64关注 0票数 0

我遇到了一个问题,我无法连接到MySQL数据库。然而,的回答对我来说是有效的。问题是:我不想将"libmysql.dll“和"libmysql.lib”复制并粘贴到每个使用MySQL的项目中(正如"Basti“所述)。

还有别的办法来解决这个问题吗?

请记住,我的情况与上述链接中的情况完全一样。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-10-06 23:50:24

您有几个选项可以使此过程自动化:

一种方法是将库路径添加到main.cpp中。

代码语言:javascript
复制
qApp->addLibraryPath( "C:\\path\\something\\mysql.dll" );

或者让整个过程自动化:

来自:

QMAKE_POST_LINK 指定将目标链接到一起后执行的命令。此变量通常为空,因此不执行任何操作。

代码语言:javascript
复制
# If operating system is Windows perform this V
win32 {
    # Create variable containing the path to your project compile folder ( probably no need to change since this is automated by Qt )
    OUT_PWD_WINDOWS         = $$OUT_PWD
    # Switch frontslashes with double backslashes
    OUT_PWD_WINDOWS         ~= s,/,\\,g

    # Create variables to set debug path and release path
    # Gotta change the \\debug and \\release to the correct debug and release folders
    OUT_PWD_WINDOWS_DEBUG   = $$quote( $$OUT_PWD_WINDOWS\\debug   )
    OUT_PWD_WINDOWS_RELEASE = $$quote( $$OUT_PWD_WINDOWS\\release )

    # Create variables to set the path to libs and dlls you want to copy ( *.lib will copy all files ending in .lib from the specified path )
    # you can change that however to mysql.lib for example if you only want 1, same goes for .dll
    LIBS_TO_COPY            = $$quote( C:\\MySql\\libs\\*.lib )
    DLLS_TO_COPY            = $$quote( C:\\MySql\\libs\\*.dll )


    # Copy libraries and dlls into debug path / else copys them into the release folder
    # essentially this is saying if( config == debug ) perform tasks else perform tasks for release, which is add actions to be executed after linking
    CONFIG( debug , debug|release ) {
        # Here we add the copy command to the QMAKE_POST_LINK variable which will be executed once the linking is done
        # We have 1 entry for libs and 1 for dlls, same goes for the release which is in the else
        QMAKE_POST_LINK += $$quote( xcopy $$LIBS_TO_COPY $$OUT_PWD_WINDOWS_DEBUG    $$escape_expand( \n\t ) )
        QMAKE_POST_LINK += $$quote( xcopy $$DLLS_TO_COPY $$OUT_PWD_WINDOWS_DEBUG    $$escape_expand( \n\t ) )
    } else {
        QMAKE_POST_LINK += $$quote( xcopy $$LIBS_TO_COPY $$OUT_PWD_WINDOWS_RELEASE  $$escape_expand( \n\t ) )
        QMAKE_POST_LINK += $$quote( xcopy $$DLLS_TO_COPY $$OUT_PWD_WINDOWS_RELEASE  $$escape_expand( \n\t ) )
    }
}

现在重命名/移除构建文件夹,将其放入.pro文件,进行路径调整,然后将其粘贴到所有.pro文件中,以便将这些库提供给需要它们的地方。

您甚至可以通过创建加载到.pro文件中的宏/ keybind /代码段来实现自动化。

附带注意:对于其他操作系统,您必须使用OS特定的命令编写此命令

编译器输出:

代码语言:javascript
复制
xcopy C:\Actions\*.lib C:\Users\xyz\dev\C++\Qt\build-Test-Desktop_Qt_5_7_0_MSVC2015_64bit-Debug\debug
C:\Actions\Test - Copy (2).lib
C:\Actions\Test - Copy.lib
C:\Actions\Test.lib
3 File(s) copied
xcopy C:\Actions\*.dll C:\Users\xyz\dev\C++\Qt\build-Test-Desktop_Qt_5_7_0_MSVC2015_64bit-Debug\debug
C:\Actions\test - Copy (2).dll
C:\Actions\test - Copy.dll
C:\Actions\test.dll
3 File(s) copied
01:45:08: The process "C:\Qt\Qt5.7.0\Tools\QtCreator\bin\jom.exe" exited normally.
票数 1
EN

Stack Overflow用户

发布于 2016-10-06 20:16:07

您可以做的不多-- Windows在少数地方搜索.dll,对于来自不同地方的共享库来说,这一切都很糟糕(想想看,如果多个应用程序想安装自己的副本/版本,会发生什么情况):

引用https://msdn.microsoft.com/en-us/library/7d83bc18(v=vs.140).aspx的话:

通过隐式和显式链接,Windows首先搜索“已知DLL”,如Kernel32.dll和User32.dll。然后,Windows按以下顺序搜索DLL:

  1. 当前进程的可执行模块所在的目录。
  2. 当前目录。
  3. Windows系统目录。GetSystemDirectory函数检索此目录的路径。
  4. Windows目录。GetWindowsDirectory函数检索此目录的路径。
  5. PATH环境变量中列出的目录。

你在做1,这是唯一明智的做法。

在您的系统上的用户上,您也可以使用5.并修改路径(但是如果您想要创建可部署的包,这是行不通的!)

2.、3.和4.对于应用程序开发人员来说是完全无用的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39903761

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档