Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How can i static build qt project in Visual Studio
Forum Updated to NodeBB v4.3 + New Features

How can i static build qt project in Visual Studio

Scheduled Pinned Locked Moved Unsolved General and Desktop
33 Posts 5 Posters 4.4k Views 2 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.
  • Chris KawaC Chris Kawa

    Read the third line.

    V Offline
    V Offline
    vandal_op
    wrote on last edited by
    #22

    @Chris-Kawa

    • cd c:/qt-static/build
    • c:/qt-static/src/configure -debug-and-release -commercial -confirm-license -static -platform win32-msvc2019 -nomake examples -nomake tests -prefix C:\Qt-Static\install
    • cmake --build . --parallel

    build success.
    alt text
    install error
    alt text

    I think I did all the steps correctly but the result is still like this.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #23

      Don't re-run configure in a directory that you have a failed build in. If a build fails clear the build directory and start over from clean state: empty dir, configure, build, install.
      Don't pass -platform win32-msvc2019. Platform is deduced from the environment i.e. the command prompt bat you used. Note that you're building 32bit version. If you want to build 64bit version start the x64 command prompt.
      Don't try to build modules you don't need. In the src dir you have directories like this:

      qt3d
      qt5compat
      qtactiveqt
      qtcharts
      ...
      

      Each of them is a Qt module. You need qtbase and qttools. Others are optional. Go through the list and for each module you're not planning to use exclude it via parameter of configure. Some of those are also not supported in a static build (you got a message about it in your previous post). Exclude those too. For example to exclude qtwebengine pass -skip qtwebengine. Do that for every module that is not supported or you're not planning to use.

      1 Reply Last reply
      0
      • V Offline
        V Offline
        vandal_op
        wrote on last edited by vandal_op
        #24

        @Chris-Kawa I need this modules with static

        • core;gui;network;widgets

        i try build with this code still error in a few minute

        • c:/qt-static/src/configure -debug-and-release -commercial -confirm-license -static -nomake examples -nomake tests -prefix C:\Qt-Static\install -skip qtwebengine -skip qt3d -skip qt5compat -skip qtactiveqt -skip qtcharts
          alt text
        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #25

          Please put some effort in. Try to understand what you're doing. Don't just copy/paste stuff.

          First - you're running this from a Developer Command Prompt. I said you should be running this from x64 Native Tools Command Prompt (or x86 if you want 32bit build).

          Second - these were just the couple modules I listed as an example. There's 38 modules in there. All of core;gui;network;widgets are in the qtbase module, so you can skip everything except base and tools.

          Clean your build dir, reconfigure correctly and build again.

          1 Reply Last reply
          1
          • V Offline
            V Offline
            vandal_op
            wrote on last edited by vandal_op
            #26

            @Christian-Ehrlicher
            I made a list of static occurrences and passed the unnecessary ones with the "skip" command, but there was still an error..
            I did some research and found out that I needed to upgrade my Windows SDK version, doing that fixed the problem and it built!

            I made the necessary bindings with visual studio, but I need to compile as x64, is there a command in the "configure" file for this?
            thank you for your help!!!
            alt text

            Chris KawaC 1 Reply Last reply
            0
            • V vandal_op

              @Christian-Ehrlicher
              I made a list of static occurrences and passed the unnecessary ones with the "skip" command, but there was still an error..
              I did some research and found out that I needed to upgrade my Windows SDK version, doing that fixed the problem and it built!

              I made the necessary bindings with visual studio, but I need to compile as x64, is there a command in the "configure" file for this?
              thank you for your help!!!
              alt text

              Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #27

              @vandal_op I told you multiple times - platform is determined from the environment. To build x64 run configure from x64 Native Tools Command Prompt.

              V 1 Reply Last reply
              1
              • Chris KawaC Chris Kawa

                @vandal_op I told you multiple times - platform is determined from the environment. To build x64 run configure from x64 Native Tools Command Prompt.

                V Offline
                V Offline
                vandal_op
                wrote on last edited by
                #28

                @Chris-Kawa
                x64 build is succesfull but when i try build on visual studio i get to much error
                alt text
                alt text
                alt text

                Cobra91151C 1 Reply Last reply
                0
                • Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by Chris Kawa
                  #29

                  The std and Windows library implementations used by an app can be used in two ways: static and dynamic. Static means these runtime libraries are linked into your executable and deployed as part of it. Dynamic means that they use external dlls - either deployed explicitly along with your app, installed separately via Visual C++ Redistributable installer or provided by the system fallback (only in case of older versions).

                  Whether you choose static or dynamic runtime libraries is up to you, but whatever you choose all components of your app need to use the same setting. Go to your project settings and see C/C++ -> Code Generation -> Runtime Library option. There are 4 options, 2 for Release and 2 for Debug builds:

                  Multi-threaded (/MT) - this is the static Release version
                  Multi-threaded DLL (/MD) - this is the dynamic Release version
                  Multi-threaded Debug (/MTd) - this is the static Debug version
                  Multi-threaded Debug DLL (/MDd) - this is the dynamic Debug version

                  The mismatch detected for ... message says you have a mismatch of those settings between what your application uses and what your Qt build used.

                  If you passed -static to Qt's configure it was built with /MD option (dynamic). If you'd configure like I said with -static -static-runtime it would've been configured with the /MT option (static).

                  Since you have a mismatch you have two options now:

                  • Change the settings of your app to match that of Qt. Note that this is the dynamic option, so you'll have to distribute the runtime library dlls along with your exe or execute the Visual C++ Redistributable installer when you install your app.
                  • Go back and configure Qt with -static -static-runtime to match the setting of your app. This will create a self contained runtime library linked with your exe.
                  1 Reply Last reply
                  2
                  • V vandal_op

                    @Chris-Kawa
                    x64 build is succesfull but when i try build on visual studio i get to much error
                    alt text
                    alt text
                    alt text

                    Cobra91151C Offline
                    Cobra91151C Offline
                    Cobra91151
                    wrote on last edited by
                    #30

                    @vandal_op

                    Hello!
                    Additionally, I would recommend to check the libs information after each Qt build. Please, do the following steps:

                    1. From x64 Native Tools Command Prompt for VS 2019 navigate to the Qt lib directory, in my case: cd C:\QtStatic\6.3.2\msvc2019_64\lib
                    2. Run this command: lib /list Qt6Core.lib, this command will output some info regarding your libs.
                      static lib will display .obj files
                      dynamic lib will display .dll files
                      
                    3. Run the following command: dumpbin /directives Qt6Core.lib. It will output the compiler version and runtime lib mode (Debug or Release), for example:
                      /FAILIFMISMATCH:_MSC_VER=1900
                      /FAILIFMISMATCH:_ITERATOR_DEBUG_LEVEL=0
                      /FAILIFMISMATCH:RuntimeLibrary=MT_StaticRelease
                      

                    You can copy/paste the output here, so we can get more details about your current Qt build as well.

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      vandal_op
                      wrote on last edited by vandal_op
                      #31

                      @Christian-Ehrlicher @Cobra91151
                      I added the -static-runtime code and built it again, it's working fine now with exe project.

                      Actually, my project is a dll and I get such an error when I compile and inject it, but when I compile and run it with exe, it works for everyone without any problems.
                      This error comes when I inject the dll I compiled with qt into any application.

                      alt text

                      My dllmain code :

                      BOOL WINAPI DllMain(HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
                      {
                          if (ul_reason_for_call == DLL_PROCESS_ATTACH)
                          {
                              MessageBoxA(NULL, "inject success", NULL, NULL);
                          }
                          return TRUE;
                      }
                      
                      Chris KawaC 1 Reply Last reply
                      0
                      • V vandal_op

                        @Christian-Ehrlicher @Cobra91151
                        I added the -static-runtime code and built it again, it's working fine now with exe project.

                        Actually, my project is a dll and I get such an error when I compile and inject it, but when I compile and run it with exe, it works for everyone without any problems.
                        This error comes when I inject the dll I compiled with qt into any application.

                        alt text

                        My dllmain code :

                        BOOL WINAPI DllMain(HANDLE hModule, DWORD  ul_reason_for_call, LPVOID lpReserved)
                        {
                            if (ul_reason_for_call == DLL_PROCESS_ATTACH)
                            {
                                MessageBoxA(NULL, "inject success", NULL, NULL);
                            }
                            return TRUE;
                        }
                        
                        Chris KawaC Offline
                        Chris KawaC Offline
                        Chris Kawa
                        Lifetime Qt Champion
                        wrote on last edited by
                        #32

                        @vandal_op
                        How do you "inject" it and why are you doing that in the first place?
                        Is the exe built with the same runtime library setting as the dll (like I said - it has to match everywhere)?
                        Is the exe and dll built with the same compiler version?
                        Is the exe using the same runtime library version?
                        Is the exe using the same Windows SDK version?
                        Have you tried to debug this app to see why it fails?

                        1 Reply Last reply
                        0
                        • V Offline
                          V Offline
                          vandal_op
                          wrote on last edited by vandal_op
                          #33

                          @Chris-Kawa
                          I will inject this qt project into an application coded in VC++ (my application) and call a function from the project I compiled using qt with dllexport.

                          Qt Dll project code like this.

                          int  __declspec(dllexport) getNumericID(const char* username)
                          {
                              QString q_username = QString::fromStdString(username);
                              for (std::vector < AccountsID >::iterator it = ids.begin(); it != ids.end(); ++it)
                              {
                                  if (it->username == q_username)
                                      return it->accountNumericId;
                              }
                              return 0;
                          }
                          

                          now i will inject this qt dll project into my own application and use the function

                          LoadLibraryA("myQtHelper.dll");
                          DWORD function_addr = (DWORD)GetProcAddress(GetModuleHandleA("myQtHelper.dll"), "getNumericID");
                          typedef int (*getNumericID)(const char* username);
                          getNumericID getID = (getNumericID)function_addr;
                          
                          int userID = getID("myUserName");
                          

                          I thought that the loadlibrary code was not working, I injected it with the cheat engine program and it gave the error you see above.
                          i have done this many times but this time the helper dll had to be compiled with qt sorry i am very new to qt.

                          1 Reply Last reply
                          0

                          • Login

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