libcef(二)基于VS2019编写CEF简单样例

本文详细指导如何在VS2019的Win32空白项目中集成libcef库,包括设置环境变量、添加文件和配置项目属性,适用于非MFC项目开发者。

前提

本文以VS2019中的Win32空白项目为例。非MFC项目,请大家注意,不要到后面才发现不是自己想要找的。

我会在Debug模式下完成代码的开发工作。在Release下是同样的步骤,但是需要注意的是你所选择的目标是Debug或Release都需要和libcef库以及libcef_dll_wrapper完全一致。

库文件选择

  1. 在想要位置新建一个文件夹,命名为libcef(可自己命名,不一定非要一样),在该文件夹下新建manifest文件夹及lib文件夹。
  2. 将Debug文件夹、Release文件夹、include文件夹、Resources文件夹复制到步骤一中新建的libcef文件夹下
    在这里插入图片描述
  3. 将cefsimple文件夹下cefsimple.exe.manifest和compatibility.manifest两个文件复制到\libcef\manifest文件夹下。将cefsimple.exe.manifest名称修改为cef.exe.manifest(此处不一定要修改,但是后文中配置项目属性生成后事件的命令行中的文件名一定要与此处的文件名一致。)
    在这里插入图片描述
  4. 将libcef_dll_wrapper静态库文件(来自于你编译出来的静态库,见下图所示)分别复制到文件夹libcef\lib\Debug和文件夹libcef\lib\Release下
    在这里插入图片描述
    在这里插入图片描述

文件夹截图

准备完成后的文件夹如下图所示

1.libcef

在这里插入图片描述

2.Debug

在这里插入图片描述

3.Release

在这里插入图片描述

4.lib

在这里插入图片描述

5.manifest

在这里插入图片描述

6.Resources

在这里插入图片描述

7.include

在这里插入图片描述

配置环境变量

将上述libcef文件夹配置系统变量如下图所示
在这里插入图片描述

新建项目

打开VS2019新建一个空白项目

  • 新建项目
    在这里插入图片描述
  • 选择Windows桌面向导,点击下一步
    在这里插入图片描述
  • 填入项目名称(此处命名为TestCef)、选择项目所在位置,点击创建
    在这里插入图片描述
  • 选择桌面应用程序、勾选空项目,点击确定
    在这里插入图片描述
    至此,Win32空项目建立完成

配置工程

添加文件

添加头文件源文件

  1. 在上面新建的工程目录下新建一个src文件夹
    在这里插入图片描述
  2. 将cefsimple文件夹下.h.cc文件(除了resource.h文件,该文件不需要)复制到src文件夹中
    在这里插入图片描述
    在这里插入图片描述
  3. 在VS中添加头文件,操作如下所示
    1. 右击头文件
    2. 选择添加,选择现有项
    3. 在跳出来的对话框中点击进入src文件夹,选中所有.h文件,点击添加
      头文件添加完毕
  4. 在VS中添加源文件,操作如下所示
    1. 右击源文件
    2. 选择添加,选择现有项
    3. 在跳出来的对话框中点击进入src文件夹,选中所有.cc文件,点击添加
      源文件添加完毕

如下图所示
在这里插入图片描述

配置项目属性

注意:下图中一定要一一对应。尤其一定要选择Win32(或x86)。因为之前的文章libcef(一)编译CEF中下载的是32位的CEF。
在这里插入图片描述

  1. 右击项目,点击属性

  2. 点击C/C++->常规->附加包含目录,输入库文件夹位置(如果你还记得的话,上面已经配置过系统变量)。输入黄框中的环境变量,会自动匹配路径(即为绿框中的地址)
    注意:红框中一定要修改成Debug(如果选的是Release模式则修改成Release)和Win32。因为之前下载的CEF是32位的。
    在这里插入图片描述

  3. 点击代码生成->运行库,切换成MTd
    在这里插入图片描述

  4. 点击预处理器->预处理器定义,输入以下内容

%(PreprocessorDefinitions)
WIN32
_WINDOWS
__STDC_CONSTANT_MACROS
__STDC_FORMAT_MACROS
_WIN32
UNICODE
_UNICODE
WINVER=0x0601
_WIN32_WINNT=0x601
NOMINMAX
WIN32_LEAN_AND_MEAN
_HAS_EXCEPTIONS=0
PSAPI_VERSION=1
CEF_USE_SANDBOX
CEF_USE_ATL
_HAS_ITERATOR_DEBUGGING=0
CMAKE_INTDIR="Debug"

若是Release模式则输入以下内容(Release模式下该处可不改动,即该步骤可跳过)

%(PreprocessorDefinitions)
WIN32
_WINDOWS
NDEBUG
__STDC_CONSTANT_MACROS
__STDC_FORMAT_MACROS
_WIN32
UNICODE
_UNICODE
WINVER=0x0601
_WIN32_WINNT=0x601
NOMINMAX
WIN32_LEAN_AND_MEAN
_HAS_EXCEPTIONS=0
PSAPI_VERSION=1
CEF_USE_SANDBOX
CEF_USE_ATL
_NDEBUG
CMAKE_INTDIR="Release"

在这里插入图片描述

  1. 点击链接器->常规->附加库目录,输入lib文件所在文件夹(若选择的是Release模式则选择相应Release文件夹,后续皆如此,不再提示)
    在这里插入图片描述

  2. 点击输入->附加依赖项,输入所需lib文件

libcef.lib
libcef_dll_wrapper.lib
cef_sandbox.lib
comctl32.lib
gdi32.lib
rpcrt4.lib
shlwapi.lib
ws2_32.lib
Advapi32.lib
dbghelp.lib
Delayimp.lib
OleAut32.lib
PowrProf.lib
Propsys.lib
psapi.lib
SetupAPI.lib
Shell32.lib
version.lib
wbemuuid.lib
winmm.lib
kernel32.lib
user32.lib
winspool.lib
shell32.lib
ole32.lib
oleaut32.lib
uuid.lib
comdlg32.lib
advapi32.lib

在这里插入图片描述

  1. 7.17.2两个操作任选其一,都可成功(7.2更简便,操作更少)
    7.1 点击清单工具->输入和输出->附加清单文件,输入compatibility.manifest文件的路径
    在这里插入图片描述
    7.2点击生成事件->生成后事件->命令行, 输入以下内容(若是Release模式则将以下内容的Debug全部替换成Release即可)
setlocal
mt.exe -nologo -manifest "D:/project/test/libcef/manifest/cef.exe.manifest" "D:/project/test/libcef/manifest/compatibility.manifest" -outputresource:"D:/project/VisualStudioProject/study/CEF/TestCef/Debug/TestCef.exe";#1
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_if_different D:/project/test/libcef/Debug/chrome_elf.dll D:/project/VisualStudioProject/study/CEF/TestCef/Debug/chrome_elf.dll
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_if_different D:/project/test/libcef/Debug/libcef.dll D:/project/VisualStudioProject/study/CEF/TestCef/Debug/libcef.dll
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_if_different D:/project/test/libcef/Debug/libEGL.dll D:/project/VisualStudioProject/study/CEF/TestCef/Debug/libEGL.dll
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_if_different D:/project/test/libcef/Debug/libGLESv2.dll D:/project/VisualStudioProject/study/CEF/TestCef/Debug/libGLESv2.dll
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_if_different D:/project/test/libcef/Debug/snapshot_blob.bin D:/project/VisualStudioProject/study/CEF/TestCef/Debug/snapshot_blob.bin
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_if_different D:/project/test/libcef/Debug/v8_context_snapshot.bin D:/project/VisualStudioProject/study/CEF/TestCef/Debug/v8_context_snapshot.bin
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_if_different D:/project/test/libcef/Debug/vk_swiftshader.dll D:/project/VisualStudioProject/study/CEF/TestCef/Debug/vk_swiftshader.dll
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_if_different D:/project/test/libcef/Debug/vk_swiftshader_icd.json D:/project/VisualStudioProject/study/CEF/TestCef/Debug/vk_swiftshader_icd.json
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_if_different D:/project/test/libcef/Debug/vulkan-1.dll D:/project/VisualStudioProject/study/CEF/TestCef/Debug/vulkan-1.dll
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_directory D:/project/test/libcef/Debug/swiftshader D:/project/VisualStudioProject/study/CEF/TestCef/Debug/swiftshader
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_if_different D:/project/test/libcef/Debug/d3dcompiler_47.dll D:/project/VisualStudioProject/study/CEF/TestCef/Debug/d3dcompiler_47.dll
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_if_different D:/project/test/libcef/Resources/chrome_100_percent.pak D:/project/VisualStudioProject/study/CEF/TestCef/Debug/chrome_100_percent.pak
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_if_different D:/project/test/libcef/Resources/chrome_200_percent.pak D:/project/VisualStudioProject/study/CEF/TestCef/Debug/chrome_200_percent.pak
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_if_different D:/project/test/libcef/Resources/resources.pak D:/project/VisualStudioProject/study/CEF/TestCef/Debug/resources.pak
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_if_different D:/project/test/libcef/Resources/icudtl.dat D:/project/VisualStudioProject/study/CEF/TestCef/Debug/icudtl.dat
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd
setlocal
D:\Environment\CMake\bin\cmake.exe -E copy_directory D:/project/test/libcef/Resources/locales D:/project/VisualStudioProject/study/CEF/TestCef/Debug/locales
if %errorlevel% neq 0 goto :cmEnd
:cmEnd
endlocal & call :cmErrorLevel %errorlevel% & goto :cmDone
:cmErrorLevel
exit /b %1
:cmDone
if %errorlevel% neq 0 goto :VCEnd

在这里插入图片描述

  1. 若第7步选择7.1,则还要将需要的文件和资源(libcef文件夹下)复制到运行目录。主要有以下两个地方需要拷贝
    8.1 Resources文件夹。把Resources文件夹里面的所有文件和子文件夹复制到运行目录下。
    在这里插入图片描述
    8.2 Debug文件夹。将下图中除了划掉的两个lib库文件之外的文件和子文件夹拷贝到运行目录下。
    在这里插入图片描述

生成项目

右击项目->生成,编译出来的运行目录如下
在这里插入图片描述

运行项目

此时运行.exe文件,肯定是空白窗口,如下图所示
在这里插入图片描述
前一篇文章libcef(一)编译CEF已经解释过原因,只要将默认url修改为国内任意网站或者自己写的网页,重新编译即可,此处不再说明。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值