1.背景
现在好多客户端程序都内嵌浏览器,有的用于实现界面,有的用于实现一些特殊功能,比如网易云音乐,QQ客户端,微信桌面客户端等。如果要内嵌浏览器,传统的方法是加入自带的IE webbrowser activex控件,但是IE对html5标准的支持不是很好,无法完成一些最新的功能。此时webkit就是最好的选择,可是webkit是一个很复杂的工程,编译也非常麻烦。好在有人替我们完成这个工作。有个叫libcef的库,实现了对webkit的封装,我们只需要直接调用就可以了,从而往我们的程序嵌入webkit浏览器,实现我们需要的功能。上面说到的那三个软件都用到了libcef这个库,在这些程序的安装目录下我们可以看到libcef.dll,libEGL.dll等dll文件。
2.生成VS工程文件
从https://cefbuilds.com/下载预编译好的二进制包,我下载的是cef_binary_3.2526.1346.g1f86d24_windows32.7z,2526分支的32位版本。然后解压到本地,比如我的是D:\SDK\cef。虽然需要的dll以及两个lib文件已经帮我们编译好了,但此时libcef还不能直接使用,因为我们还需要libcef_dll_wrapper.lib这个文件,而这个需要我们自己编译,如果没有这个的话,我们运行里面的cefsimple:
C++
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include
#include "cefsimple/simple_app.h"
#include "include/cef_sandbox_win.h"
// When generating projects with CMake the CEF_USE_SANDBOX value will be defined
// automatically if using the required compiler version. Pass -DUSE_SANDBOX=OFF
// to the CMake command-line to disable use of the sandbox.
// Uncomment this line to manually enable sandbox support.
// #define CEF_USE_SANDBOX 1
#if defined(CEF_USE_SANDBOX)
// The cef_sandbox.lib static library is currently built with VS2013. It may not
// link successfully with other VS versions.
#pragma comment(lib, "cef_sandbox.lib")
#endif
// Entry point function for all processes.
int APIENTRY wWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow) {
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// Enable High-DPI support on Windows 7 or newer.
CefEnableHighDPISupport();
void* sandbox_info = NULL;
#if defined(CEF_USE_SANDBOX)
// Manage the life span of the sandbox information object. This is necessary
// for sandbox support on Windows. See cef_sandbox_win.h for complete details.
CefScopedSandboxInfo scoped_sandbox;
sandbox_info = scoped_sandbox.sandbox_info();
#endif
// Provide CEF with command-line arguments.
CefMainArgs main_args(hInstance);
// SimpleApp implements application-level callbacks. It will create the first
// browser instance in OnContextInitialized() after CEF has initialized.
CefRefPtr app(new SimpleApp);
// CEF applications have multiple sub-processes (render, plugin, GPU, etc)
// that share the same executable. This function checks the command-line and,
// if this is a sub-process, executes the appropriate logic.
int exit_code = CefExecuteProcess(main_args, app.get(), sandbox_info);
if (exit_code >= 0) {
// The sub-process has completed so return here.
return exit_code;
}
// Specify CEF global settings here.
CefSettings settings;
#if !defined(CEF_USE_SANDBOX)
settings.no_sandbox = true;
#endif
// Initialize CEF.
CefInitialize(main_args, settings, app.get(), sandbox_info);
// Run the CEF message loop. This will block until CefQuitMessageLoop() is
// called.
CefRunMessageLoop();
// Shut down CEF.
CefShutdown();
return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include
#include "cefsimple/simple_app.h"
#include "include/cef_sandbox_win.h"
// When generating projects with CMake the CEF_USE_SANDBOX value will be defined
// automatically if using the required compiler version. Pass -DUSE_SANDBOX=OFF
// to the CMake command-line to disable use of the sandbox.
// Uncomment this line to manually enable sandbox support.
// #define CEF_USE_SANDBOX 1
#if defined(CEF_USE_SANDBOX)
// The cef_sandbox.lib static library is currently built with VS2013. It may not
// link successfully with other VS versions.
#pragma comment(lib, "cef_sandbox.lib")
#endif
// Entry point function for all processes.
intAPIENTRYwWinMain(HINSTANCEhInstance,
HINSTANCEhPrevInstance,
LPTSTRlpCmdLine,
intnCmdShow){
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// Enable High-DPI support on Windows 7 or newer.
CefEnableHighDPISupport();
void*sandbox_info=NULL;
#if defined(CEF_USE_SANDBOX)
// Manage the life span of the sandbox information object. This is necessary
// for sandbox support on Windows. See cef_sandbox_win.h for complete details.
CefScopedSandboxInfoscoped_sandbox;
sandbox_info=scoped_sandbox.sandbox_info();
#endif
// Provide CEF with command-line arguments.
CefMainArgsmain_args(hInstance);
// SimpleApp implements application-level callbacks. It will create the first
// browser instance in OnContextInitialized() after CEF has initialized.
CefRefPtrapp(newSimpleApp);
// CEF applications have multiple sub-processes (render, plugin, GPU, etc)
// that share the same executable. This function checks the command-line and,
// if this is a sub-process, executes the appropriate logic.
intexit_code=CefExecuteProcess(main_args,app.get(),sandbox_info);
if(exit_code>=0){
// The sub-process has completed so return here.
returnexit_code;
}
// Specify CEF global settings here.
CefSettingssettings;
#if !defined(CEF_USE_SANDBOX)
settings.no_sandbox=true;
#endif
// Initialize CEF.
CefInitialize(main_args,settings,app.get(),sandbox_info);
// Run the CEF message loop. This will block until CefQuitMessageLoop() is
// called.
CefRunMessageLoop();
// Shut down CEF.
CefShutdown();
return0;
}
会报如下错误:
SeverityCodeDescriptionProjectFileLine
ErrorLNK2019unresolved external symbol "int __cdecl CefExecuteProcess(class CefMainArgs const &,class CefRefPtr,void *)" (?CefExecuteProcess@@YAHABVCefMainArgs@@V?$CefRefPtr@VCefApp@@@@PAX@Z) referenced in function _wWinMain@16cefsampleD:\vs2015\cefsample\cefsample\Source.obj1
ErrorLNK2019unresolved external symbol "bool __cdecl CefInitialize(class CefMainArgs const &,class CefStructBase const &,class CefRefPtr,void *)" (?CefInitialize@@YA_NABVCefMainArgs@@ABV?$CefStructBase@UCefSettingsTraits@@@@V?$CefRefPtr@VCefApp@@@@PAX@Z) referenced in function _wWinMain@16cefsampleD:\vs2015\cefsample\cefsample\Source.obj1
ErrorLNK2019unresolved external symbol "void __cdecl CefShutdown(void)" (?CefShutdown@@YAXXZ) referenced in function _wWinMain@16cefsampleD:\vs2015\cefsample\cefsample\Source.obj1
ErrorLNK2019unresolved external symbol "void __cdecl CefRunMessageLoop(void)" (?CefRunMessageLoop@@YAXXZ) referenced in function _wWinMain@16cefsampleD:\vs2015\cefsample\cefsample\Source.obj1
ErrorLNK2019unresolved external symbol "void __cdecl CefEnableHighDPISupport(void)" (?CefEnableHighDPISupport@@YAXXZ) referenced in function _wWinMain@16cefsampleD:\vs2015\cefsample\cefsample\Source.obj1
ErrorLNK2019unresolved external symbol "public: __thiscall SimpleApp::SimpleApp(void)" (??0SimpleApp@@QAE@XZ) referenced in function _wWinMain@16cefsampleD:\vs2015\cefsample\cefsample\Source.obj1
ErrorLNK11206 unresolved externalscefsampleD:\vs2015\cefsample\Debug\cefsample.exe1
1
2
3
4
5
6
7
8
SeverityCodeDescriptionProjectFileLine
ErrorLNK2019unresolvedexternalsymbol"int __cdecl CefExecuteProcess(class CefMainArgs const &,class CefRefPtr,void *)"(?CefExecuteProcess@@YAHABVCefMainArgs@@V?$CefRefPtr@VCefApp@@@@PAX@Z)referencedinfunction_wWinMain@16cefsampleD:\vs2015\cefsample\cefsample\Source.obj1
ErrorLNK2019unresolvedexternalsymbol"bool __cdecl CefInitialize(class CefMainArgs const &,class CefStructBase const &,class CefRefPtr,void *)"(?CefInitialize@@YA_NABVCefMainArgs@@ABV?$CefStructBase@UCefSettingsTraits@@@@V?$CefRefPtr@VCefApp@@@@PAX@Z)referencedinfunction_wWinMain@16cefsampleD:\vs2015\cefsample\cefsample\Source.obj1
ErrorLNK2019unresolvedexternalsymbol"void __cdecl CefShutdown(void)"(?CefShutdown@@YAXXZ)referencedinfunction_wWinMain@16cefsampleD:\vs2015\cefsample\cefsample\Source.obj1
ErrorLNK2019unresolvedexternalsymbol"void __cdecl CefRunMessageLoop(void)"(?CefRunMessageLoop@@YAXXZ)referencedinfunction_wWinMain@16cefsampleD:\vs2015\cefsample\cefsample\Source.obj1
ErrorLNK2019unresolvedexternalsymbol"void __cdecl CefEnableHighDPISupport(void)"(?CefEnableHighDPISupport@@YAXXZ)referencedinfunction_wWinMain@16cefsampleD:\vs2015\cefsample\cefsample\Source.obj1
ErrorLNK2019unresolvedexternalsymbol"public: __thiscall SimpleApp::SimpleApp(void)"(??0SimpleApp@@QAE@XZ)referencedinfunction_wWinMain@16cefsampleD:\vs2015\cefsample\cefsample\Source.obj1
ErrorLNK11206unresolvedexternalscefsampleD:\vs2015\cefsample\Debug\cefsample.exe1
都是些 referenced in function _wWinMain@16的错误。
要编译libcef_dll_wrapper.lib文件,需要我们去生成VS工程文件,然后用VS打开编译。此时我们需要用到cmake软件。如下图所示打开cmake软件,设置代码目录以及工程文件生成目录:
接着点击Configure,选择编译器,我用的是默认VS2015自带的:
然后点击Generate即在cef目录下生成VS工程文件:
如下图,目录下已经生成了cef.sln解决方案文件,此时我们打开cef.sln文件
可以看到该解决方案下有5个工程:
编译libcef_dll_wrapper工程即可得到libcef_dll_wrapper.lib文件,然后我们编译运行示例工程cefclient,即可看到一个简单的浏览器,很简单吧。