基于开源PDFium,编译动态库--Windows平台(上)

 

开篇注:博客是为了更好的思考,希望能以此记录自己的学习历程。本文写于2018年09月11日,修改于2018年09月12日。随着时间流逝,可能有些内容已经失效,望读者望文观义,get到关键点。假如对文中有啥有疑问、有想法、感觉不太对的地方欢迎留言交流~。

引言

因为用到过别人编译的PDFium.dll,但是有点问题,于是官方方式编译一下PDFium库。这里只说Windows平台的编译。

开始

1、访问外网

PDFium源码托管在https://pdfium.googlesource.com/pdfium/
所以先得有个访问谷歌的工具吧。这个得自己找。

2、获取depot工具

关于depot工具,就知道它是个编译工具就好,如同vistual studio一样,知道怎么用,什么效果即。如官方介绍,http://www.chromium.org/developers/how-tos/install-depot-tools这里有关于depot工具的 介绍。别忘了将depot工具添加到环境变量中,这样我们可以很方便的在cmd中使用它。

3、准备好合适版本Vistaul Studio及相关环境

正如官方所说:

As of September, 2017 (R503915) Chromium requires Visual Studio 2017 (15.7.2) to build. The clang-cl compiler is used but Visual Studio’s header files, libraries, and some tools are required. Visual Studio Community Edition should work if its license is appropriate for you. You must install the “Desktop development with C++” component and the “MFC and ATL support” sub-component. This can be done from the command line by passing these arguments to the Visual Studio installer that you download:
–add Microsoft.VisualStudio.Workload.NativeDesktop
–add Microsoft.VisualStudio.Component.VC.ATLMFC --includeRecommended
You must have the version 10.0.17134 Windows 10 SDK installed. This can be installed separately or by checking the appropriate box in the Visual Studio Installer.
The SDK Debugging Tools must also be installed. If the Windows 10 SDK was installed via the Visual Studio installer, then they can be installed by going to: Control Panel → Programs → Programs and Features → Select the “Windows Software Development Kit” → Change → Change → Check “Debugging Tools For Windows” → Change. Or, you can download the standalone SDK installer and use it to install the Debugging Tools.

pdfium2017年9月以后版本需要我们至少Vistual Studio 2017(15.7.2)版本,Win10 SDK。并且官方提到,用命令行安装确保一下内容存在。

You must install the “Desktop development with C++” component and the “MFC and ATL support” sub-component.

文档很详细,参照安装即可。(注:关于这里,恰好电脑上有Vistual Studio 2017,就没继续搞,有一些网友改了配置然后用的是Vistual Studio 2015,闲着没事可以搞一搞。 因为较新代码里面有c++11特性,就不要用Vistual Studio 2010、2013编译了)

4、获取源码

如官网所述:

mkdir repo
cd repo
gclient config --unmanaged https://pdfium.googlesource.com/pdfium.git
gclient sync

 

这样就可以获取源码。

5、开始编译

(1)生成gn构建文件

# 进入pdfium根目录
cd pdfium
# 使用VistulStudio编译必要项
set DEPOT_TOOLS_WIN_TOOLCHAIN=0
# 利用GN来生成构建文件  gn args <directory>
gn args out/Release64

此时会自动在pdfium根目录下创建"out\Release64"目录,并且在"out\Release64"目录下自动生成args.gn文件(构建文件),然后"gn args "这条命令会使用系统默认编辑器打开args.gn文件,这时候需要我们编辑来设置编译目标:

use_goma = false#Googlers。确保goma已安装并首先运行。
is_debug = false#要生成release库,所以关闭调试功能。
#设置为启用实验性Skia后端。
pdf_use_skia = false
#设置为启用实验性Skia后端(仅限路径)。
pdf_use_skia_paths = false
pdf_enable_xfa = true#设置false以删除XFA支持(隐含JS支持)。
pdf_enable_v8 = true#设置false以删除Javascript支持。
pdf_is_standalone = true#设置非嵌入式构建。
is_component_build = false#禁用组件构建(必须为false)
clang_use_chrome_plugins = false#当前必须为false。
target_cpu=“x64”#默认就是编译x64平台,编x86平台就需要修改了

将以上内容拷贝进args.gn文件后,保存并退出编辑。

(2)ninja执行构建动作

常见几个构建动作:

# 执行构建示例程序动作
ninja -C <directory> pdfium_test
# 执行构建整个 产品动作
ninja -C <directory> pdfium_all
# 执行构建pdfium库动作(本文便是执行构建pdfium动态库动作)
ninja -C <directory> pdfium

这里,单单使用:

ninja -C out\Release32 pdfium

来执行构建pdfium.dll的动作即可。

6、测试生成的pdfium.dll

自行根据文档,写测试程序测试即可。
另外,假如要生成32位的动态库,修改target_cpu的值为 “x86” 即可,然后构建、执行构建即可。

结束语

另外,这篇文章留下个坑,下篇文章再说。

 

posted @ 2018-11-09 17:53  purehol  阅读(4309)  评论(0编辑  收藏  举报