V8引擎静态库及其调用方法

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

本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)
{
   
   
	/*!< 分配目
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值