DLLs written in C++ QT6 cannot be linked by C programs
-
Cause: I wrote a DLL file that uses QT6's QTCPSocket component. This file can be linked and used by QLibrary without any problems, but it cannot be linked and used by LoadLibrary in a C program, which will result in an error. I want to know why, thanks.
example code like this:
C program that links DLL by LoadLibrary
#include <windows.h> #include <stdio.h> int main() { HMODULE hModule = LoadLibrary("myDLL.dll"); if (hModule) { printf("ok!"); FreeLibrary(hModule); } else { DWORD error = GetLastError(); printf("Could not load the DLL. Error: %lu\n", error); } return 0; }
and Then, the err message like this
PS D:\Code\CProgram> & 'c:\Users\12037\.vscode\extensions\ms-vscode.cpptools-1.20.5-win32-x64\debugAdapters\bin\WindowsDebugLauncher.exe' '--stdin=Microsoft-MIEngine-In-z0eabn4e.l2k' '--stdout=Microsoft-MIEngine-Out-a2ifctuv.t5z' '--stderr=Microsoft-MIEngine-Error-5vdihyxk.yrk' '--pid=Microsoft-MIEngine-Pid-i1z3tzaa.qen' '--dbgExe=C:\TDM-GCC-64\bin\gdb.exe' '--interpreter=mi' Could not load the DLL. Error: 126
Supplement:I have packaged all relevant DLLs using the windeployqt6.exe.
-
@Christian-Ehrlicher said in DLLs written in C++ QT6 cannot be linked by C programs:
Also I don't see how this dll should work at all - where is your running Q(Core)Application?
I think it's related to this topic and the dll contains the complete Qt GUI program including the
QApplication
instance.
(No idea if this works like this... never linked a Qt GUI app to a C program)@pedisChen try an absolute path to your DLL, if the only issue is that it can't locate it...
Check your working directory (where your C program is executed in) and how this is relative to the directory where your DLL is. -
Your dll is not found according the error number as you can see here: https://learn.microsoft.com/en-us/windows/win32/debug/system-error-codes--0-499-
-
@Christian-Ehrlicher thank you for your reply, so how can I solve this problem, I have already used windeployqt6.exe to ship all DLL dependency files to my c program project, I don't know which dll files I need to add.
-
The error message is 'dll not found' - not some dependencies not found. Make sure the dll you want to load is in the current work dir.
Also I don't see how this dll should work at all - where is your running Q(Core)Application? -
@Christian-Ehrlicher said in DLLs written in C++ QT6 cannot be linked by C programs:
Also I don't see how this dll should work at all - where is your running Q(Core)Application?
I think it's related to this topic and the dll contains the complete Qt GUI program including the
QApplication
instance.
(No idea if this works like this... never linked a Qt GUI app to a C program)@pedisChen try an absolute path to your DLL, if the only issue is that it can't locate it...
Check your working directory (where your C program is executed in) and how this is relative to the directory where your DLL is. -