開篇注:博客是為了更好的思考,希望能以此記錄自己的學習歷程。本文寫於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” 即可,然后構建、執行構建即可。
結束語
另外,這篇文章留下個坑,下篇文章再說。