Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Qt 6
  4. DLLs written in C++ QT6 cannot be linked by C programs
Forum Updated to NodeBB v4.3 + New Features

DLLs written in C++ QT6 cannot be linked by C programs

Scheduled Pinned Locked Moved Solved Qt 6
6 Posts 3 Posters 519 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • pedisChenP Offline
    pedisChenP Offline
    pedisChen
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • Christian EhrlicherC Christian Ehrlicher

      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?

      Pl45m4P Offline
      Pl45m4P Offline
      Pl45m4
      wrote on last edited by Pl45m4
      #5

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


      If debugging is the process of removing software bugs, then programming must be the process of putting them in.

      ~E. W. Dijkstra

      pedisChenP 1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by
        #2

        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-

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        pedisChenP 1 Reply Last reply
        2
        • Christian EhrlicherC Christian Ehrlicher

          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-

          pedisChenP Offline
          pedisChenP Offline
          pedisChen
          wrote on last edited by pedisChen
          #3

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

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            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?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            Pl45m4P 1 Reply Last reply
            1
            • Christian EhrlicherC Christian Ehrlicher

              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?

              Pl45m4P Offline
              Pl45m4P Offline
              Pl45m4
              wrote on last edited by Pl45m4
              #5

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


              If debugging is the process of removing software bugs, then programming must be the process of putting them in.

              ~E. W. Dijkstra

              pedisChenP 1 Reply Last reply
              1
              • Pl45m4P Pl45m4

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

                pedisChenP Offline
                pedisChenP Offline
                pedisChen
                wrote on last edited by
                #6

                @Pl45m4 It is workable. thank you!

                1 Reply Last reply
                0
                • pedisChenP pedisChen has marked this topic as solved on

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved