Compiling a true static QT program using VC2005

Source: Internet
Author: User

First of all, you should know what is called static reference compilation, what is called Dynamic Reference compilation. I'm just a simple mention here, the specific can be Google a bit.

Dynamic reference compilation refers to a library that references libraries in the form of DLLs. Dynamically compiled EXE programs are small in size because the associated libraries are not included. Of course, when the program is published, it will also publish the relevant library.

Static reference compilation, refers to the relevant library is also introduced into the EXE file. This is the size of the program will be very large, but the release of the program will become much simpler.

Second, you might notice that I wrote the word "real" in the title. Why should I emphasize the true two words? Because C or C + + programs compiled with VC need the relevant C runtime library to run. If you are VC6, the corresponding library is called MSVCR, if it is vc2005, that is msvcr08,vc2008 is MSVCR09. I assume that you are installing VC2005, please go to the following directory: ${vs install dir}\vc\redist\x86 and ${system Driver}:\windows\winsxs, you will find a lot of libraries below. Yes, quite a part of it is the C runtime library.

Okay, so, first of all, we used VC2005 to write a C or C + + program that does not use MFC, how to publish to the end user? There are two ways of doing this:
(1) static reference C Runtime Library: Open the "project", "XXX Properties", "Configuration Properties", "C + +" code generation, "Run-time library." Did you see that? There are a total of four options, where Mt begins with a static reference, MD starts with a dynamic reference, D ends with debug debug, and No D is released release, so there are four options. We choose/MT, and then compile the program (the generated program should not be small), the program sent to the user, and then the user can run directly.
(2) dynamic reference C Runtime Library: Similar to the above, but is compiled with the/MD option (the program should be just dozens of k), and then sent to the user. At this point, the user is unable to run the program, will be reported what the program boot failure, re-install the system may fix problems such as tips. This is what we are going to do with the C runtime library once more. Put ${vs Install dir}\vc\redist\x86\ All Files under Microsoft.VC80.CRT (note, all, including that. manifest file) are sent to the user, the user puts the files in the same directory as our program, and then runs again, when the program is up (VC2005, C The runtime library references have changed a lot, so Google is recommended.

Speaking of C Runtime Library, to say that the QT library, here I assume you are using the latest Qt4.4.3. When we compile QT, Configure.exe has a lot of parameters, we can configure.exe--help to see, which, the default generated QT library (the default meaning, is not add-share or-static parameters) is a dynamic reference, that is to say , after compiling, in the Qtdir Lib directory in addition to a large heap of LIB files, there are a lot of DLL files. When we release our EXE program, we need to send the DLL of the corresponding QT library to the user as well.

According to the QT installation manual and online a lot of Daniel, plus-static parameters, QT can be statically compiled, that is, the Lib directory after a lot of lib files, no DLL files. Whether? Let's do an experiment:

The first is to set the variable:
Set qtdir=%cd%
Set Path=%path%;%qtdir%\bin
Set qmakespec=win32-msvc2005
"C:\Program Files\Microsoft Visual Studio 8\vc\vcvarsall.bat" "x86
Configuration makefile:
Configure-release-static-fast-qt-sql-odbc-qt-sql-sqlite-no-webkit
(The parameters here do not explain, it is recommended that readers into the--help carefully check, special note, the reason is-no-webkit, because the new version of QT plus WebKit, and this thing compile time is very expensive, compiled is also very large, there are more than 100 m, And I basically don't use this stuff, so ignore it)
And then
CD src (I go directly to the SRC directory nmake because I don't want to make other irrelevant modules, saving time)
Nmake

After a long wait, we found that Lib is really only a large heap of Lib files, and each lib file size is above m, seems to have succeeded. Then we build a QT project in the VC2005 that installs the qt-vsintegration, and then compile a release version. When compiling, the problem comes up. We select the/MD option, and the link is available, but if we want to use the static C runtime Library with the/MT option, we will report a lot of errors such as the repetition of such function links. Experience tells us that it is not possible to compile with/MT because another library--QT library uses another way of referencing/md (in principle, all modules in a program should use the same reference method, specifically Google). Obviously, we compiled the so-called static QT program, the same to carry the C Runtime library of Microsoft run around, not enough "real" static.

How can the


be made completely static? Remember that before compiling wxwidgets, it has an option of runtime_libs = static or dynamic In addition to the option of shared=0 or 1, and obviously this runtime_libs option is what we want. But I looked through the QT installation manual and the online Daniel article, did not mention this question, I was in the heart felt strange, have no one encountered this problem? I carefully checked the help of Configure.exe, there is no similar option, the problem is frozen.

Recall that when we compile, the screen calls Cl.exe compile, there is a parameter: cl.exe .... xxx.cpp, a friend of the eye will find that this-MD is the C Runtime dynamic reference option. However, how to change this-md to-MT? We opened just now we compiled QT src directory, casually find a directory in, open makefile.release, we will see CFLAGS=-MD ..., yes, that's it. As long as we change the-MD to-mt here, we will compile QT using the static C runtime library. Of course we can't replace these makefile one by one, the key is to find the template file that generates these parameters. Obviously, it must be in the QT mkspecs directory, we went straight to win32-msvc2005 directory, sure enough to find a qmake.conf file, sure enough to find a qmake_cflags_release =-O2-MD, the-MD here to replace-MT, And then clean up the configuration information of the build just now (online also said with NMAKE Confclean to empty, but I did not succeed, it seems that the use of the-fast parameter, but it doesn't matter, delete this directory, re-unzip a source code on it, Then replace the qmake.conf-MD in the win32-msvc2005 directory with-MT) and re-
Configure-release-static-fast-qt-sql-odbc-qt-sql-sqlite- No-webkit
Then NMAKE
is a long wait. But let's not wait and see the Compile command, cl.exe ....-mt ... xxx.cpp, it turns out to be a static C run-time library.
After compiling, like just then, in VC2005 build a QT project, and then use/MT this option to compile, OK, compile successfully, the EXE file size is 4.95M, seems to have the C runtime library embedded in. Then put this program to the user to run, OK single EXE file run successfully.

At this point, compiling a truly static QT program test is complete. Summing up the whole process, first of all to be patient, because the compilation of QT at least two hours (of course, with some techniques, such as-fast,-no-qmake, only compile src and so on the skills can shorten a lot of time), I went back and forth to compile five Qt; second, familiar with some common compilation, link errors , for example, see the XXX library has been quoted such errors, immediately associated with the reference should be a different library caused; Finally, be good at discovering problems and finding problems.

Http://www.cnblogs.com/bingcaihuang/archive/2010/12/01/1892765.html

Compiling a true static QT program using VC2005

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.