gpt4 book ai didi

mysql - QMYSQL 驱动程序未加载(其他解决方案?)

转载 作者:行者123 更新时间:2023-11-29 11:14:56 26 4
gpt4 key购买 nike

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

还有其他方法可以解决这个问题吗?

请记住,我的情况与上面链接中的情况完全相同。

最佳答案

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

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

qApp->addLibraryPath( "C:\\path\\something\\mysql.dll" );
<小时/>

或者您自动化整个过程:

From Qt Docs:

QMAKE_POST_LINK
Specifies the command to execute after linking the TARGET together. This variable is normally empty and therefore nothing is executed.

<小时/>
# 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 文件中的宏/键绑定(bind)/代码片段来自动执行此操作。

<小时/>

旁注:对于其他操作系统,您必须使用操作系统特定命令来编写此命令

<小时/>

编译器输出:

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.

关于mysql - QMYSQL 驱动程序未加载(其他解决方案?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39903761/

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