Compile the static Qt library on Windows

Source: Internet
Author: User

How to compileWindowsPlatform Implementation in the true senseStatic Qt LibraryThis is the content to be introduced in this article. I have seen such an article on the Internet that helps you to share with you.

Compile WindowsStatic Qt LibraryIs the content to be introduced in this article. One of the most common problems I have seen on Qt forums at home and abroad is how to compileStatic Qt Library, Visible in the dynamic link library (Windows:. Dll, Linux:. so) Today, static link libraries are still reasonable and widely used. First, let's take a look at what the dynamic library brings to us:

1. reusability during development: you can start to use the functions provided by thousands of practical libraries including their header files;

2. Quick Compilation: anyone who has compiled static links knows that the time consumed in the compilation process is twice or more than that of dynamic links;

3. Lightweight deployment: If your program uses the dynamic library that comes with the operating system, or some very popular libraries. When you deploy your software, these libraries do not need to be included in your

4. packge greatly reduces its volume;

Maintainability during use: if an updated version is released for a library, you only need to replace the dynamic library file without re-compiling your entire program.

It is quite convenient to patch software from time to time. What troubles does she bring to us? Software is easy to use. The so-called ease of use not only refers to how convenient it is to use the software, but also to use the previous processes as part of ease of use.

If you have used Linux, you may feel the same way. It is really difficult to install a software. You will be prompted that this so library is missing, and this so library is incompatible with that so later. What's more, you also need to set a lot of parameters in the configuration file of your account ...... The idea of using the software has been abandoned before the installation is complete. I think the usability problem is that Linux is so powerful, but the penetration rate is far from enough.WindowsAfter all, most users need an operating system like a dumb camera.

HoweverWindowsYou will often encounter prompts such as "a dll cannot be found, the program cannot be started", and "A. net framework is required. You can also download the missing dll library or the. net re-release library for installation. However, this is already a big violation of the software usability principle. You must know that the vast majority of software audiences are people not related to the IT industry. When they encounter these problems, they will be easy to understand.

The versions after Visual Studio 2005 are even more unreasonable. If the compiled C/C ++ programs run on a system without the Visual Studio re-release library, the system prompts "the application is not correctly configured, and re-installation may solve this problem", which is just a matter of fact, yunyun ......

The rationality of the existence of the static link is here. an executable file is deployed to the user, and the user does not need to do anything. Double-click it to enjoy it. As for compilation and maintenance, it is a developer's business, even if one compilation takes a day, it cannot waste the user's time to do things that could have been done by the developer in the cloud. Based on the above belief, I have studied the true Qt static library Compilation Method in Windows. As for what is "true", read the following.

Visual Studio compilation options

People who have compiled Qt on their own may find it strange to use this question to write such a large section. Isn't the configuration option of Qt clearly written?-static is the static library compilation. But have you really tried it? If the program compiled with this static library runs on a "clean" machine, it prompts "the application is not properly configured ......" Or "msvcrpxx. dll" is not found. It seems that Qt is static, but Qt is written in C ++, and the linked C ++ Runtime Library is dynamic. To solve this problem, we should start with the four compilation options of Visual Studio, which determine the link mode of the C/C ++ library in the Link stage of the program.

1. link the C/C ++ multi-threaded dynamic library and use these compilation options to re-release the VC library during software deployment. Otherwise, the above error message will appear.

/MD: dynamically links multi-threaded libraries (msvcrt. lib ). When using this option, you must use the/nodefalib lib option to ignore the libc. lib, libcmt. lib, libcd. lib, libcmtd. lib, and msvcrtd. lib libraries; otherwise, a link error occurs;

/MDd: dynamically link the multi-thread debugging Library (msvcrtd. lib ). When using this option, you must use the/nodefalib lib option to ignore the libc. lib, libcmt. lib, msvcrt. lib, libcd. lib, and libcmtd. lib libraries; otherwise, a link error occurs;

2. link the C/C ++ multi-threaded static library and use these compilation options. The re-release library of VC is not required during software deployment.

/MT: static link multi-threaded Library (libcmt. lib ). When using this option, you must use the/nodefalib lib option to ignore the libc. lib, msvcrt. lib, libcd. lib, libcmtd. lib, and msvcrtd. lib libraries; otherwise, a link error occurs;

/MTd: static link multi-thread debugging Library (libcmtd. lib ). When this option is used, you must use the/nodefalib lib option to ignore the libc. lib, libcmt. lib, msvcrt. lib, libcd. lib, and msvcrtd. lib libraries. Otherwise, a link error occurs.

OK, the preparation is ready. Next we will start to compile the static Qt library in the true sense :-)

Compile static Qt Library

Since the solution is found, this compilation should be okay. However, if you use-static to configure Qt in configure, the compilation result is still counterproductive. Check the generated makefile carefully to find that the compilation options are/MD and/MDd. This is where the problem is. But if there are hundreds of makefiles, do you need to manually modify them one by one ...... There must be something that determines the makefile generation parameters. Good! In the mkspecs directory in the Qt root directory, the name is the abbreviation of make specification. In this directory, each compiler on various platforms has a subdirectory. Hey, the compiler compilation options are all here. Open qmake. conf in win32-msvc2008 and check whether the following two lines are found:

 
 
  1. [cc lang="make"]  
  2. QMAKE_CFLAGS_RELEASE = -O2 -MD -GL  
  3. QMAKE_CFLAGS_DEBUG = -Zi -MDd  
  4. [/cc] 

I believe you already know the answer to the question and change them:

 
 
  1. [cc lang="make"]  
  2. QMAKE_CFLAGS_RELEASE = -O2 -MT -GL  
  3. QMAKE_CFLAGS_DEBUG = -Zi -MTd  
  4. [/cc] 

Also, do not forget to add the ignore library option mentioned above. Modify the QMAKE_LFLAGS_RELEASE and QMAKE_LFLAGS_DEBUG parameters:

 
 
  1. [cc lang="make"]  
  2. QMAKE_LFLAGS_RELEASE = /INCREMENTAL:NO /LTCG /NODEFAULTLIB:libc.lib /NODEFAULTLIB:msvcrt.lib/NODEFAULTLIB:libcd.lib 
  3. /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:msvcrtd.lib  
  4. QMAKE_LFLAGS_DEBUG = /DEBUG /NODEFAULTLIB:libc.lib /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:msvcrt.lib 
  5. /NODEFAULTLIB:libcd.lib /NODEFAULTLIB:msvcrtd.lib  
  6. [/cc] 

In this case, configure-static… in the root directory of Qt... (Select other options as needed), and then nmake, two or three hours later, the static Qt library will be generated in the true sense. I have a small suggestion. In fact, it takes a lot of time to compile demos and examples. You only use cd src and then nmake to compile.Qt LibraryIf you need cd tools to compile tools such as QtDesigner.

In addition, the compilation of the webkit library is the most time-consuming. If you do not want to use this library, add-no-webkit to configure. In this way, the entire compilation process takes about 40-60 minutes. At this time, you can try to build the first pure static Qt program, but when you connect, there will still be a lot of Link errors, prompting unresolved external symbol. At this time, you only need to add the following in your Qt project file. pro:

 
 
  1. [cc lang="make"]  
  2. win32:LIBS += Imm32.lib Winmm.lib ws2_32.lib  
  3. [/cc] 

You can. So far, your "pure"StaticThe Qt application can be published to users.

Summary: detailed compilationWindowsPlatformStatic Qt LibraryI hope this article will help you!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.