本V8引擎静态库分为x86版本和x64版本
V8引擎静态库下载地址
编译工具:VS2019
v8开头的即为V8引擎静态库(附送其它开源静态库libuv.lib、zlib.lib等_)


如何调用V8引擎
1、下载v8静态库到本地
2、配置开发环境
将上述v8的include目录配置到“附加包含目录”配置项中

根据你的开发环境配置不同版本的lib库(注意debug和release),如下:

3、调用代码如下,需要创建一个控制台程序
#include <iostream>
#include "v8.h"
#include "libplatform/libplatform.h"
#include <assert.h>
#include <windows.h>
#pragma comment(lib, "winmm.lib")
#pragma comment(lib, "dbghelp.lib")
#pragma comment(lib, "shlwapi.lib")
#pragma comment(lib, "v8_compiler.lib")
#pragma comment(lib, "v8_libbase.lib")
#pragma comment(lib, "v8_snapshot.lib")
#pragma comment(lib, "v8_libplatform.lib")
#pragma comment(lib, "v8_libsampler.lib")
#pragma comment(lib, "v8_init.lib")
#pragma comment(lib, "v8_initializers.lib")
#pragma comment(lib, "v8_zlib.lib")
#pragma comment(lib, "icudata.lib")
#pragma comment(lib, "icutools.lib")
#pragma comment(lib, "icuucx.lib")
#pragma comment(lib, "icui18n.lib")
#pragma comment(lib, "v8_base_without_compiler.lib")
using namespace v8;
using namespace std;
wstring Utf8ToUnicode(const string& strSrc)
{
/*!< 分配目标空间 */
int iAllocSize = static_cast<int>(strSrc.size() + 10);
WCHAR* pwszBuffer = new WCHAR[iAllocSize];
if (NULL == pwszBuffer)
{
return L"";
}
int iCharsRet = MultiByteToWideChar(CP_UTF8, 0, strSrc.c_str(),
static_cast<int>(strSrc.size()),
pwszBuffer, iAllocSize);
/*!< 成功 */
wstring wstrRet;
if (0 < iCharsRet)
{
wstrRet.assign(pwszBuffer, static_cast<size_t>(iCharsRet));
}
/*!< 释放内存 */
delete[] pwszBuffer;
return wstrRet;
}
string Utf8ToAnsi(const string& strSrc)
{
wstring wstrTemp = Utf8ToUnicode(strSrc);
/*!< 分配目标空间, 长度为 Ansi 编码的两倍 */
int iAllocSize = static_cast<int>(strSrc.size() * 2 + 10);
char* pszBuffer = new char[iAllocSize];
if (NULL == pszBuffer)
{
return "";
}
int iCharsRet = WideCharToMultiByte(CP_ACP, 0, wstrTemp.c_str(),
static_cast<int>(wstrTemp.size()),
pszBuffer, iAllocSize, NULL, NULL);
/*!< 成功 */
string strRet;
if (0 < iCharsRet)
{
strRet.assign(pszBuffer, static_cast<size_t>(iCharsRet));
}
/*!< 释放内存 */
delete[] pszBuffer;
return strRet;
}
string UnicodeToUtf8(const wstring& wstrSrc)
{
/*!< 分配目标空间, 一个16位Unicode字符最多可以转为4个字节 */
int iAllocSize = static_cast<int>(wstrSrc.size() * 4 + 10);
char* pszBuffer = new char[iAllocSize];
if (NULL == pszBuffer)
{
return "";
}
int iCharsRet = WideCharToMultiByte(CP_UTF8, 0, wstrSrc.c_str(),
static_cast<int>(wstrSrc.size()),
pszBuffer, iAllocSize, NULL, NULL);
/*!< 成功 */
string strRet;
if (0 < iCharsRet)
{
strRet.assign(pszBuffer, static_cast<size_t>(iCharsRet));
}
/*!< 释放内存 */
delete[] pszBuffer;
return strRet;
}
string AnsiToUtf8(const string& strSrc)
{
/*!< 分配目

本文介绍了如何在VS2019环境下下载、配置和使用V8引擎静态库,包括必要的编译链接步骤,并展示了如何通过C++调用V8中的JavaScript函数和对象实例化。
482

被折叠的 条评论
为什么被折叠?



