现在的位置: 首页 > 综合 > 正文

VS2010使用静态编译的qt库

2013年08月28日 ⁄ 综合 ⁄ 共 3005字 ⁄ 字号 评论关闭

       Qt开发界面很方便,但发布程序就不那么方便了,你的把引用到的dll一起发布才行,要是能静态编译就好了,发布的时候只有一个exe多方便。

       虽然以前为了方便,直接安装的qt-windows-opensource-5.0.2-msvc2010_32-x86-offline.exe, 省去了自己编译这一步,但官方提供的库是动态编译的,是以lib + dll形式存在的,所以没法在我的程序中静态编译。

       为了能够得到静态编译的效果,毅然选择自己编译源码,生成静态库,下面是我编译静态库的全部过程。

 

       1、下载qt源码,5.0.2 qt-everywhere-opensource-src-5.0.2.zip
       2、解压到F:\qt-src-5.0.2
       3、查看readme文件,其中有提到,需要下载3个安装工具
   Windows:
   --------

     Open a Windows SDK (7.0, 7.1 or later) command prompt. Ensure that the
     following tools can be found in the path:
     * Perl version 5.12 or later   [http://www.activestate.com/activeperl/]
     * Python version 2.7 or later  [http://www.activestate.com/activepython/]
     * Ruby version 1.9.3 or later  [http://rubyinstaller.org/]

你可以选择下面地址直接下载:
     1. Perl 5.8 or later
        x86 http://www.activestate.com/activeperl/downloads/thank-you?dl=http://downloads.activestate.com/ActivePerl/releases/5.16.3.1603/ActivePerl-5.16.3.1603-MSWin32-x86-296746.msi 
       amd64 http://www.activestate.com/activeperl/downloads/thank-you?dl=http://downloads.activestate.com/ActivePerl/releases/5.16.3.1603/ActivePerl-5.16.3.1603-MSWin32-x64-296746.msi 
       2. Python 2.7 or later
       x86 http://www.activestate.com/activepython/downloads/thank-you?dl=http://downloads.activestate.com/ActivePython/releases/2.7.2.5/ActivePython-2.7.2.5-win32-x86.msi 
       amd64 http://www.activestate.com/activepython/downloads/thank-you?dl=http://downloads.activestate.com/ActivePython/releases/2.7.2.5/ActivePython-2.7.2.5-win64-x64.msi
      3. Ruby
              amd64 http://rubyforge.org/frs/download.php/76806/rubyinstaller-2.0.0-p0-x64.exe 

 

 

     4、进入Open Visual Studio Command Prompt (2010), cd 到源文件目录下,执行以下命令

      F:\qt-src-5.0.1 > configure -prefix F:\qt-static -debug-and-release -static -platform win32-msvc2010 -no-c++11  -no-icu -opengl desktop -qt-sql-sqlite -qt-zlib -qt-style-windowsvista -qt-libpng -qt-libjpeg -nomake demos -nomake examples -nomake tests
-mp

     5、执行完后,直接运行nmake(漫长的等待中)

     6、打开VS2010, Qt5--> Qt Options    添加版本

          static5.0.2      F:\qt-src-5.0.2\qtbase

     7、创建qt工程后,Qt-->Qt Project Settings, 选择静态库版本:static5.0.2

    

      8、首先我们在Debug模式下运行,会有以下链接错误
      Qt5Cored.lib(qeventdispatcher_win.obj) : error LNK2019: unresolved external symbol_WSAAsyncSelect@16 referenced in function "public: void __thiscall            QEventDispatcherWin32Private::doWsaAsyncSelect(int)"
(?doWsaAsyncSelect@QEventDispatcherWin32Private@@QAEXH@Z)

      这个链接容易解决,属性页-->Link-->input  Additional Dependencies 加入Ws2_32.lib, 然后运行

      9、虽然链接错误解决了,但运行仍会报错

      

解决这个问题,需要花费一些时间了,下面就直接提供最终解决方案了。

对于只引用qt默认的三个库的程序,依赖lib列表如下:
imm32.lib
winmm.lib
Ws2_32.lib
qtmaind.lib
Qt5Cored.lib
Qt5Guid.lib
Qt5Widgetsd.lib
opengl32.lib
Qt5PlatformSupportd.lib
qwindowsd.lib

注意Debug模式静态库名字后面都是带d的,另外qwindows.lib单独在一个文件中,需要在属性页-->Link-->General Additional Library Directories加入lib的目录,
F:\qt-src-5.0.2\qtbase\plugins\platforms

     10、以上lib加好后,还有关键一步,在main函数文件中加入以下两行代码
     #include <QtPlugin>
    Q_IMPORT_PLUGIN (QWindowsIntegrationPlugin);

     11、运行程序,OK程序可以跑起来了, 查看以下程序,发布版exe接近7M(默认界面)

     12、以上流程只是实现了qt库的静态编译,如果你想你的程序独立于编译器的话,还得设置Runtime Library  为/MT 或  /MTd

 

 

 

 

 

 

 

 

抱歉!评论已关闭.