How to use the VC2005 compiler "real" static Qt applications (rpm)

How to use the VC2005 compiler "real" static Qt applications (rpm)
2009-10-10 10:30

The original post Location: http://www.qtcn.org/bbs/read.php?tid=13825

How to use the VC2005 compiler "real" static Qt application

(I hate to quote or reprint someone else's article does not specify when the address or the author, I hope that readers be able to change this view :) - Hawaii Snowman)

(hereinafter involves only windows, and talk about linux, unix, etc. In addition, here said VC2005, because I use the 2005, in fact, other versions of the same VC can also reference)

first of all, you should know what the static reference compiler, what is the dynamic reference compiler. I am here simply mention specific you can google it.

Dynamic reference compiler, refers to the relevant library, a reference library in the form of dll. Exe dynamically compiled program size is relatively small, because the relevant libraries are not included. Of course, when the release of the program, but also the associated libraries together advertised.

Compile static reference, refers to the associated libraries also introduced together with Exe file. This is the size of the program will be enormous, however, release program will become much easier.

Second, you may have noticed I wrote "real" word on the title. Why do I really want to emphasize the word it? Because the use of VC compiled C or C ++ program, they are relevant to the needs of the C runtime library to run. If you are VC6, corresponding library is called MSVCR, if it is vc2005, that is MSVCR08, vc2008 is MSVCR09. I assumed that you are installing the VC2005, please go to the following directory: $ {VS Install Dir} / VC / redist / x86 and $ {System Driver}: / windows / WinSxS, you will find here are many, many libraries. Yes, there is a considerable portion of the C runtime library.

Well, get on, first of all, we wrote a program VC2005 does not use MFC deposit of C or C ++, how it distributed to end-users? There are two ways:
(1) static reference C runtime library: Open the "Project" -> "XXX Properties" -> "Configuration Properties" -> "C / C ++ " -> " Code Generation" -> "run-time library." See it? There are four options here, which is the beginning of MT static references are cited at the beginning of the dynamic MD, d is the end of the Debug debugging version, is no d Release release version, so a total of four options. We select / MT, then the compiler (the program generated should not be small), this program to users, and the user can directly run.
(2) dynamic reference C runtime library: almost like the above, but with the / MD compiler option (the program should only tens K), and then distributed to users. At this point, the user can not run this program, and will report what program fails to boot, reinstall the system may fix the problem prompted the like. This is what we have to put a concurrent C runtime library in the past. All files $ {VS Install Dir} /VC/redist/x86/Microsoft.VC80.CRT under (note that all, including the .manifest file) to users, user put these files on our program with a directory, then run again, this time, the program is up (VC2005 after the reference C runtime library has changed a lot, recommendations google it).

Having C runtime library, are concerned that the Qt library, and here I assume you are using the latest Qt4.4.3. When we compile Qt, configure.exe there are many parameters, we can configure. exe --help to see, which is generated by default Qt library (default meaning here that are not added -share or -static parameters) are dynamically referenced, that is, after the compiler, lib directory of QtDir in addition to file under lib a lot, there are a lot of dll file. We publish our Exe program when the need to appropriate Dll Qt libraries also distributed to users together.

Follow the installation manual online and a lot of Qt Daniel statement and, with the -static parameters, Qt can be statically compiled, that is to say, a lot of lib file after the lib directory, no dll file. whether? We do a test:

The first is to set variables:
the SET QTDIR =% CD%
the SET the PATH =% the PATH%;% QTDIR% / bin
the SET QMAKESPEC win32-msvc2005 =
"C: / Program Files / in the Microsoft Visual Studio 8 / VC / vcvarsall.bat" "x86
Configuration Makefile:
the configure -static -release the -fast -QT-SQL-ODBC SQL-SQLite--QT the -no-WebKit
(the parameters of not setting them out here, the reader is advised to seriously look into the --help, otherwise stated what, why -no-webkit, because the new version of qt added Webkit, and this stuff is very time-consuming compile time, the compiler is also great, there are more than 100 M, and I basically do not use this East East, so ignore it)
then
cd src (the reason I go directly to the src directory nmake, because do not want to make other unrelated modules, saving time)
nmake

After a long wait, we found a lot of really only a lib file under lib, lib and size of each file are more than M, it seems to have succeeded. Then we installed qt-vsintegration of a new VC2005 Qt project, and then compile a release version. Compile time, the problem came. We select / MD option, and then links you can, but if we want to use the / MT option to use a static C runtime library, it will report a lot of mistakes repeat of a certain function links the class. Experience tells us that he could not use the / MT to compile, because another library --Qt library uses another reference / MD (in principle, a program which all modules should use the same reference specific google it). Obviously, we have compiled the so-called static Qt program, as to carrying Microsoft C Runtime libraries everywhere, it is not enough "real" static.


How can it be made entirely of static? Before compiling wxWidgets remember when, in addition to its option SHARED = 0 or 1, there are a RUNTIME_LIBS = static or dynamic options, it is clear that this RUNTIME_LIBS option is what we want options. But I rummaged through the installation manual and on-line articles Qt Daniel, have not mentioned this, I was heart felt strange, is that no one encountered this problem? I carefully looked up the help configure.exe, there is no similar options, click on the question froze.

Recall that we have just compile time, the compiler calls cl.exe on the screen when there is such an argument: cl.exe .... -MD .... xxx.cpp, Lee eye friends what you will find, this -MD that option c runtime dynamically referenced. However, how this -MD -MT into it? We have just opened under the src directory we have compiled the qt, and go in just to find a directory, open Makefile.Release, we will see CFLAGS = -MD ........, yes, that is here. As long as we are here to -MD into -MT, you will use a static c runtime library compiled the Qt. We certainly can not replace these one by one makefile, the key is to identify these parameters to generate a template file. Obviously, it is certainly in the qt mkspecs directory, we went straight to the win32-msvc2005 directory, she found a qmake.conf file, she found a QMAKE_CFLAGS_RELEASE = -O2 -MD, here's the -MD replaced -MT, then clean up just generated configuration information (online said with nmake confclean to clear, but I did not succeed, seemingly using the -fast argument's sake, but that's okay, this directory deleted, re-extract a source code on it, then qmake.conf under win32-msvc2005 directory -MD replaced -MT), re-
configure -release -static -fast -qt-sql- odbc -qt-sql-sqlite -no-webkit
then nmake
another long wait. But we do not do so on to see out of the compile command, cl.exe .... -MT .... xxx.cpp, it really becomes a static c runtime.
After compiling, as I have just, in VC2005 build a Qt project, and then use / MT this option to compile, OK, compiled successfully, out of the Exe file size is 4.95M, it seems to have embedded itself in the C runtime library come. Then put this program into the user where to run, OK single Exe file to run successful.

At this point, the real static compile Qt completion of the test program. To sum up the whole process, the first is to be patient, because Qt are compiled once at least two hours (of course, with some skills, such as -fast, -no-qmake, and so only compile src techniques can shorten a lot of time), I back and forth five times to compile Qt; secondly familiar with some common compiler, broken links, such as a library to see the errors already cited XXX like, immediately think should be a reference different libraries caused; and finally, for the good questions, find the problem.

Published 15 original articles · won praise 2 · views 50000 +

Guess you like

Origin blog.csdn.net/imhikaru/article/details/5983853