Poppler 及 Poppler-Qt4 在Windows下基于MinGW的静态编译 - Poppler Qt4

http://strayingworm.blog.163.com/blog/static/197234037201363113154429/


Poppler 是功能强大的 PDF 渲染编辑库。Poppler-Qt4 项目开发了 Poppler 的 Qt4 API,非常实用。
[http://poppler.freedesktop.org/] Poppler Project
[http://people.freedesktop.org/~aacid/docs/qt4/] The Poppler Qt4 interface library 

这里我们在 Windows 下,利用静态编译的 Qt 4.8.5 静态编译 Poppler, Poppler-Qt4 及所依赖的各个库。

(1) 使用 MinGW,没有使用 Visual C++。
比如安装路径 C:\MinGW,bin 路径 C:\MinGW\bin。MSYS 终端启动文件 C:\MinGW\msys\1.0\msys.bat

(2) 设置Qt路径
这里是静态编译poppler,故需要静态编译了的Qt库,比如在 C:\Qt\QtStatic\4.8.5 。在 MSYS 终端环境下的路径为 /C/Qt/QtStatic/4.8.5

(3) 运行进入 MSYS 终端
运行 C:\MinGW\msys\1.0\msys.bat 

比如 Win+R 运行 C:\MinGW\msys\1.0\msys.bat



(4) 设置环境变量
假设我们准备在 C:\Qt\Poppler-Static-Rebuild 目录及子目录下编译 Poppler,则

    export RebuildDir=/C/Qt/Poppler-Static-Rebuild


并且准备好 Qt 如

    export QtDir=/C/Qt/QtStatic/4.8.5



(5) 下载各个库源码
下载各个库源码并解压至我们准备编译的目录,一个库一个子文件夹。
 Lib  Download From  Extract Into  Header Path Header Dependency 
 Lib Dependency
zlib http://www.zlib.net/ zlib-1.2.8  zlib-1.2.8    
libpng http://www.libpng.org/pub/png/libpng.html png-1.6.2  png-1.6.2  zlib -lz
freetype http://download.savannah.gnu.org/releases/freetype/ freetype-2.5.0  freetype-2.5.0/include  png -lpng16 -lz
libiconv MinGW/include MinGW/lib iconv-mingw32  iconv-mingw32    
libxml2 ftp://xmlsoft.org/libxml2/ xml2-2.9.1  xml2-2.9.1/include    
fontconfig http://www.freedesktop.org/software/fontconfig/release/ fontconfig-2.10.93 fontconfig-2.10.93 xml2,freetype -lxml2 -liconv -lfreetype -lpng16 -lz
pixman http://www.cairographics.org/releases/ pixman-0.30.0 pixman-0.30.0/pximan png -lpng16 -lz
cairo http://www.cairographics.org/releases/ cairo-1.12.14 cairo-1.12.14/src pixman,fontconfig,
freetype,png,zlib
-lpixman-1 -lfontconfig -lxml2 -liconv -lfreetype -lpng16 -lz
libjpeg http://www.ijg.org/files/ jpeg-8d jpeg-8d    
libtiff ftp://ftp.remotesensing.org/pub/libtiff/ tiff-4.0.3 tiff-4.0.3/libtiff    
lcms2 http://sourceforge.net/projects/lcms lcms2-2.5 lcms2-2.5/include  tiff,jpeg,zlib -ltiff -ljpeg -lz
openjpeg http://code.google.com/p/openjpeg/downloads/list openjpeg-1.5.1 openjpeg-1.5.1/libopenjpeg  lcms2,tiff,png,zlib -llcms2 -ljpeg -ltiff -lpng16 -lz
poppler http://poppler.freedesktop.org/ poppler-0.23.4 poppler-0.23.4/poppler

  最后一行 poppler 需要 include 的库为
      cairo,fontconfig,freetype,openjpeg,lcms2,tiff,jpeg,png,zlib
  最后一行 poppler 及 poppler-qt4 需要引用的 lib 为  
      -lQtTest -lQtGui -lQtXml -lQtCore -lcairo -lpixman-1 -lfontconfig -lxml2 -liconv -lfreetype -ljpeg -lopenjpeg -llcms2 -lpng16 -lz -lGdi32 -lOle32 -lOleAut32 -lImm32 -lWinSpool -lWinMM -lComDlg32 -lWS2_32 -lUuid


(6) 编译pkg-config
Cairo库一定需要pkg-config,其它库对于pkg-config的依赖可有可无。到时候我们把所有生成的库文件都放到$RebuildDir目录下,就不make install了。

    export CFLAGS="-march=i486 -mms-bitfields -mthreads"     export CPPFLAGS="-DG_ATOMIC_OP_USE_GCC_BUILTINS=1"     cd ${RebuildDir}/pkg-config-0.28     ./configure --enable-static --with-internal-glib     make     成功之后拷贝 pkg-config.exe 到 ${RebuildDir} 备用。     cp .libs/pkg-config.exe ../     cd ../



(7) 编译 zlib

cd ${RebuildDir}/zlib-1.2.8/ make -fwin32/Makefile.gcc cp libz.a ../ cd ../



(8) 编译 libpng

cd   ${RebuildDir}/png-1.6.2/ export CPPFLAGS="-I${RebuildDir}/zlib-1.2.8" export LDFLAGS="-L${RebuildDir}" ./configure --enable-static make cp .libs/libpng16.a ../ cd ../



(9) 编译 freetype

cd ${RebuildDir}/freetype-2.5.0 export CPPFLAGS= export LDFLAGS= export LIBPNG_CFLAGS="-I${RebuildDir}/png-1.6.2" export LIBPNG_LDFLAGS="-L${RebuildDir} -lpng16 -lz" ./configure --enable-static --with-png --without-zlib   (use internal zlib) make cp objs/.libs/libfreetype.a ../ cd ../



(10) 拷贝 libiconv 自 MinGW 目录

mkdir ${RebuildDir}/iconv-mingw32 cp /C/MinGW/lib/libiconv.a  ${RebuildDir}/ cp /C/MinGW/include/iconv.h ${RebuildDir}/iconv-mingw32/



(11) 编译 libxml2

cd ${RebuildDir}/xml2-2.9.1 export CFLAGS="-DLIBICONV_STATIC -DLIBXML_STATIC" export CPPFLAGS= export LDFLAGS="-L${RebuildDir}" export LIBS="-liconv" ./configure --enable-static --with-sax1 --with-iconv --without-ftp --without-http make cp .libs/libxml2.a  ../ cd ../



(12) 编译 fontconfig

cd ${RebuildDir}/fontconfig-2.10.93 export CFLAGS="-DLIBICONV_STATIC -DLIBXML_STATIC" export CPPFLAGS= export LDFLAGS="-L${RebuildDir}" export LIBS="-lfreetype -lxml2 -liconv -lpng16 -lz" export FREETYPE_CFLAGS="-I${RebuildDir}/freetype-2.5.0/include" export FREETYPE_LIBS="-lfreetype" export LIBXML2_CFLAGS="-I${RebuildDir}/xml2-2.9.1/include" export LIBXML2_LIBS="-lxml2" ./configure --enable-static --enable-libxml2 make cp src/.libs/libfontconfig.a ../ cd ../



(13) 编译 cairo-pixman

cd ${RebuildDir}/pixman-0.30.0 export CFLAGS= export CPPFLAGS= export LDFLAGS= export LIBS= export PNG_CFLAGS="-I${RebuildDir}/png-1.6.2" export PNG_LIBS="-L${RebuildDir} -lpng16 -lz" ./configure --enable-static --enable-libpng make cp pixman/.libs/libpixman-1.a ../ cd ../



(14) 编译 cairo

export RebuildDir=/C/Gnu/Qt/QtProgramme/CrabPDF-Poppler-Static-Rebuild-Final cd ${RebuildDir}/cairo-1.12.12 export CFLAGS="-DLIBICONV_STATIC -DLIBXML_STATIC -D_SSIZE_T_DEFINED" export CPPFLAGS="-I${RebuildDir}/zlib-1.2.8" export LDFLAGS="-L${RebuildDir}" export LIBS= export png_CFLAGS="-I${RebuildDir}/png-1.6.2" export png_LIBS="-lpng16 -lz" export png_REQUIRES="nothing" export FREETYPE_CFLAGS="-I${RebuildDir}/freetype-2.5.0/include" export FREETYPE_LIBS="-lfreetype" export FONTCONFIG_CFLAGS="-I${RebuildDir}/fontconfig-2.10.93" export FONTCONFIG_LIBS="-lfontconfig -lxml2 -liconv" export pixman_CFLAGS="-I${RebuildDir}/pixman-0.30.0/pixman" export pixman_LIBS="-lpixman-1" export PKG_CONFIG="${RebuildDir}/pkg-config.exe" ./configure --enable-static --disable-qt --disable-win32 --disable-win32-font --enable-fc --enable-ft --enable-png --enable-ps --enable-pdf --disable-xlib --disable-xcb --disable-xlib-xrender --disable-pthread --host=i686-pc-mingw32 --enable-test-surfaces make cp src/.libs/libcairo.a ../ cd ../



(15) 编译 libjpeg
cd ${RebuildDir}/jpeg-8d
export CFLAGS="-DOPJ_STATIC"
export CPPFLAGS=
export LDFLAGS=
export LIBS=
./configure --enable-static
make
cp .libs/libjpeg.a ../
cd ../

(16) 编译 libtiff
cd ${RebuildDir}/tiff-4.0.3
export CFLAGS=
export CPPFLAGS=
export LDFLAGS=
export LIBS=
./configure --enable-static --enable-cxx
make
cp libtiff/.libs/libtiff.a ../
cd ../

(17) 编译 lcms2
cd ${RebuildDir}/lcms2-2.5
export CFLAGS="-DOPJ_STATIC"
export CPPFLAGS="-I${RebuildDir}/jpeg-8d -I${RebuildDir}/tiff-4.0.3/libtiff -I${RebuildDir}/zlib-1.2.8"
export LDFLAGS="-L${RebuildDir}"
export LIBS="-ljpeg -ltiff -lz"
./configure --enable-static --with-jpeg --with-tiff --with-zlib
make
cp src/.libs/liblcms2.a ../
cd ../

(18) 编译 openjpeg
cd ${RebuildDir}/openjpeg-1.5.1
export CFLAGS="-DOPJ_STATIC"
export CPPFLAGS="-I${RebuildDir}/zlib-1.2.8"
export LDFLAGS="-L${RebuildDir}"
export LIBS="-ltiff -llcms2 -lpng16 -lz"
export LCMS2_CFLAGS="-I${RebuildDir}/lcms2-2.5/include"
export LCMS2_LIBS="-llcms2 -ljpeg -ltiff -lz"
export TIFF_CFLAGS="-I${RebuildDir}/tiff-4.0.3/libtiff"
export TIFF_LIBS="-ltiff"
export PNG_CFLAGS="-I${RebuildDir}/png-1.6.2"
export PNG_LIBS="-lpng16 -lz"
export Z_CFLAGS="-I${RebuildDir}/zlib-1.2.8"
export Z_LIBS="-lz"
# patch! edit "libopenjpeg/t1_generate_luts.c"! it provides a main() function!
         line 185 - int main(){
                  + int t1_init_main(){
./configure --enable-static
make
cp libopenjpeg/.libs/libopenjpeg.a ../
cd ../

(19) 编译 poppler
首先,要确保上面生成的.a都放在 ${RebuildDir} 目录下了。
其次,可以下载 Poppler Test 文件放在 poppler-0.23.4/test 目录下,也可以不做这一步,不做的话 configure 时就不要 --with-testdatadir=test 就行了。
再次,编译make时poppler会自动进行test,并且依赖Windows SDKs,因此需要Windows SDKs的静态库*.Lib。比如,需要Gdi32.Lib, Ole32.Lib, OleAut32.Lib, Imm32.Lib, WinSpool.Lib, WinMM.Lib, ComDlg32.Lib, WS2_32.Lib, Uuid.Lib。这些Lib可以在安装后的Windows SDKs路径下找到,如 "C:\Program Files\Microsoft SDKs\Windows\v7.1\Lib\" 目录下。拷贝这些Lib至 ${RebuildDir} 目录下即可。

cd ${RebuildDir}/poppler-0.23.4 export CFLAGS="-DOPJ_STATIC -DLIBICONV_STATIC -DLIBXML_STATIC" export CXXFLAGS="-DOPJ_STATIC -DLIBICONV_STATIC -DLIBXML_STATIC" export CPPFLAGS="-I${RebuildDir}/zlib-1.2.8" export LDFLAGS="-L${RebuildDir}" export LIBS= export MOCQT4="${QtDir}/bin/moc.exe" export LIBPNG_CFLAGS="-I${RebuildDir}/png-1.6.2" export LIBPNG_LIBS="-lpng16 -lz" export FREETYPE_CFLAGS="-I${RebuildDir}/freetype-2.5.0/include" export FREETYPE_LIBS="-lfreetype -lpng16 -lz" export FONTCONFIG_CFLAGS="-I${RebuildDir}/fontconfig-2.10.93" export FONTCONFIG_LIBS="-lfontconfig -lfreetype -lxml2 -liconv -lpng16 -lz" export CAIRO_CFLAGS="-I${RebuildDir}/cairo-1.12.12/src -I${RebuildDir}/freetype-2.5.0/include" export CAIRO_LIBS="-lcairo -lpixman-1 -lfontconfig -lxml2 -liconv -lfreetype -ljpeg -lpng16 -lz" export LIBJPEG_CFLAGS="-I${RebuildDir}/jpeg-8d -I/C/MinGW/include -L${RebuildDir} -L/C/MinGW/lib" export LIBJPEG_LIBS="-ljpeg -L/C/MinGW/lib -lm" export LIBTIFF_CFLAGS="-I${RebuildDir}/tiff-4.0.3/libtiff" export LIBTIFF_LIBS="-ltiff" export LCMS_CFLAGS="-I${RebuildDir}/lcms2-2.5/include" export LCMS_LIBS="-llcms2 -ljpeg -ltiff -lz" export LIBOPENJPEG_CFLAGS="-I${RebuildDir}/openjpeg-1.5.1/libopenjpeg" export LIBOPENJPEG_LIBS="-lopenjpeg -llcms2 -ltiff -lpng16 -lz" export PDFTOCAIRO_CFLAGS="-I${RebuildDir}/cairo-1.12.12/src -I${RebuildDir}/freetype-2.5.0/include -I${RebuildDir}/fontconfig-2.10.93" export PDFTOCAIRO_LIBS="-lcairo -lpixman-1 -lfontconfig -lxml2 -liconv -lfreetype -ljpeg -lpng16 -lz" export POPPLER_QT4_CFLAGS="-I${QtDir}/include" export POPPLER_QT4_LIBS="-L${QtDir}/lib -lQtTest -lQtGui -lQtXml -lQtCore -lcairo -lpixman-1 -lfontconfig -lxml2 -liconv -lfreetype -ljpeg -lopenjpeg -llcms2 -lpng16 -lz -lGdi32 -lOle32 -lOleAut32 -lImm32 -lWinSpool -lWinMM -lComDlg32 -lWS2_32 -lUuid" export POPPLER_QT4_TEST_CFLAGS="-I${QtDir}/include" export POPPLER_QT4_TEST_LIBS="-L${QtDir}/lib -lQtTest -lQtGui -lQtXml -lQtCore -lcairo -lpixman-1 -lfontconfig -lxml2 -liconv -lfreetype -ljpeg -lopenjpeg -llcms2 -lpng16 -lz -lGdi32 -lOle32 -lOleAut32 -lImm32 -lWinSpool -lWinMM -lComDlg32 -lWS2_32 -lUuid" export PKG_CONFIG="${RebuildDir}/pkg-config.exe" ./configure --enable-static --enable-libtiff --enable-poppler-qt4 --disable-poppler-qt5 --disable-poppler-cpp --disable-gtk-test --with-testdatadir=test make cp poppler/.libs/libpoppler.a     ../ cp qt4/src/.libs/libpoppler-qt4.a ../ cd ../



(20) 附注
命令行查看库所包含的object
    ar -t ../../../libpoppler.a
    ar -t ../../../libpoppler-qt4.a

(21) Poppler-Qt4 的静态编译 Bug !
    遇到test-poppler-qt4编译通不过。 

    试着进入该文件所在目录手动编译     cd qt4/src/     gcc test-poppler-qt4.cpp -I${QtDir}/include -I${RebuildDir}/poppler-0.23.4/poppler -I${RebuildDir}/poppler-0.23.4/qt4/src -I${RebuildDir}/zlib-1.2.8 -fno-exceptions -fno-check-new -fno-common -DOPJ_STATIC -D_WIN32_IE=0x0500 -L${RebuildDir} -lpoppler-qt4 -lpoppler -lopenjpeg -llcms2 -ltiff -lcairo -lpixman-1 -lfontconfig -lxml2 -liconv -lfreetype -lpng16 -lz -L${QtDir}/lib -lQtTest -lQtGui -lQtXml -lQtCore -lGdi32 -lOle32 -lOleAut32 -lImm32 -lWinSpool -lWinMM -lComDlg32 -lWS2_32 -lUuid     还是通不过。     看错误提示,显示Undefined reference to operator new(unsigned int)大量报错,问题在于需要用g++而非gcc,白痴了……     g++ test-poppler-qt4.cpp -I${QtDir}/include -I${RebuildDir}/poppler-0.23.4/poppler -I${RebuildDir}/poppler-0.23.4/qt4/src -I${RebuildDir}/zlib-1.2.8 -fno-exceptions -fno-check-new -fno-common -DOPJ_STATIC -D_WIN32_IE=0x0500 -L${RebuildDir} -lpoppler-qt4 -lpoppler -lopenjpeg -llcms2 -ltiff -lcairo -lpixman-1 -lfontconfig -lxml2 -liconv -lfreetype -lpng16 -lz -L${QtDir}/lib -lQtTest -lQtGui -lQtXml -lQtCore -lGdi32 -lOle32 -lOleAut32 -lImm32 -lWinSpool -lWinMM -lComDlg32 -lWS2_32 -lUuid     还是通不过。     看错误提示,发现Poppler的函数是有_imp_前缀的,这意味着这些函数头文件中一定定义了__declspec(dllexport)或__declspec(dllimport)。     查看库文件包含的函数名称,是静态函数名称     nm ../src/libpoppler_qt4_la-poppler-document.o | grep Poppler8Document16setRenderBackend     nm ../../../libpoppler-qt4.a | grep Poppler8Document16setRenderBackend     看来还是要找到在静态编译中还是会强制定义 dllexport 和 dllimport 的头文件。发现qt4\src\poppler-export.h 和 cpp\poppler-global.h 是这样的,它们包含了dllexport和dllimport的定义。手动编辑删除相应内容 "__declspec(dllexport)", "__declspec(dllimport)" 即可。


    另外,发现QtGui包含的tiff与自己编译的tiff冲突

    ${QtDir}/src/3rdparty/libtiff/libtiff/tif_strip.c 与 ${RebuildDir}/tiff-4.0.3/libtiff/tif_strip.c     ${QtDir}/src/3rdparty/libtiff/libtiff/tiffio.h 与 ${RebuildDir}/tiff-4.0.3/libtiff/tiffio.h     edit ${RebuildDir}/poppler-0.23.4/qt4/src/libpoppler-qt4.la     replace "${RebuildDir}/poppler-0.23.4/poppler/libpoppler.la" with "-L"${RebuildDir}/poppler-0.23.4/poppler/.libs -lpoppler"


    另外,遇到stress-threads-qt4编译通不过

    ${RebuildDir}/poppler-0.23.4/qt4/tests/stress-threads-qt4.cpp     edit ${RebuildDir}/poppler-0.23.4/qt4/tests/stress-threads-qt4.cpp         add line #include <QtTest/QTest>         replace "sleep(duration);" with "QTest::qSleep(duration);"     edit ${RebuildDir}/poppler-0.23.4/qt4/tests/Makefile         add "-lQtTest" in this var "POPPLER_QT4_LIBS" exactly before "-lQtGui"


另外,如果想要在程序运行时搜索某个路径的库,

    则在 LDFLAGS 变量中,可以不用 -L/path/to/lib/dir 而用 -Wl,R/path/to/lib/dir



(22) 基本完成了吧……

你可能感兴趣的:(qt5)