如何在C ++应用程序中使用Google的v8? [英] How to use Google's v8 in C ++ application?

查看:187
本文介绍了如何在C ++应用程序中使用Google的v8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了GYP的Google的V8源代码,在all.sln生成动态库之后构建和编译,在Debug& amp;中生成一个lib文件。发布模式。 (我使用的是谷歌的V8源5.4.0版本。)



我只是使用v8,它会输出javascript的运行值。



当你尝试使用Google的V8时,我在visual studio 2015中创建了一个新的win32应用程序控制台项目。



我是知道,使用v8.lib文件和v8_base.lib文件以及v8_nosnapshot.lib文件就足够了。



1.我生成了v8_base_0.lib,v8_base_1。 lib,v8_base_2.lib,v8_base_3.lib。我使用的是什么v8_base.lib?

2.我是否需要其他lib文件?



我被用作整个复制包含目录以便使用h文件。我也复制了dll文件(icu18n.dll,icuuc.dll,v8.dll)。



我习惯了复制hello-world.cc源码在.cpp文件中。



但是,然后编译ac ++应用程序源,发生错误。



 1> ----- Build build:Project:v8Application2,Configuration:Debug Win32 ----- 
1> v8Application2.cpp
1> v8Application2.obj:错误LNK2019:函数class v8 :: Platform * __cdecl v8 :: platform :: CreateDefaultPlatform(int)中引用了未解析的外部符号_main(?CreateDefaultPlatform @ platform @ v8 @ @ YAPAVPlatform @ 2 @ H @ Z)1> c:\ users \ kito \documents\visual studio 2015 \Projects\v8Application2 \Debug\v8Application2.exe:致命错误LNK1120:1未解析的外部
==========构建:0成功,1失败,0最新,0跳过==========





3.我该怎么办?



我上传了.vcxproj和.cpp文件的c ++应用程序。请查看 visual studio 2015'c ++ win32应用程序控制台'项目文件



另请参阅我的另一个问题链接

我在这个问题上花了三个星期。根本没有进展到这里。



我需要帮助,不管你有什么不对。或者询问与此相对应的相关链接。



这是该计划的主要来源。

我必须复制主要来源来自示例源。

我不是单独更改的一部分。



  #include   <   stdio .h  >  
#include < stdlib.h > ;
#include < ; string.h >

#include include / libplatform / libplatform.h
#include include / v8.h

使用 命名空间 v8;

int main( int argc, char * argv []){

V8 :: InitializeICUDefaultLocation(argv [ 0 ]);
V8 :: InitializeExternalStartupData(argv [ 0 ]);
Platform * platform = platform :: CreateDefaultPlatform();
V8 :: InitializePlatform(平台);
V8 :: Initialize();

// 创建一个新的隔离并使其成为当前隔离。
Isolate :: CreateParams create_params;
create_params.array_buffer_allocator =
v8 :: ArrayBuffer :: Allocator :: NewDefaultAllocator();
Isolate * isolate = Isolate :: New(create_params);
{
Isolate :: Scope isolate_scope(isolate);

// 创建堆栈分配的句柄范围。
HandleScope handle_scope(隔离);

// 创建新的上下文。
Local< Context> ; context = Context :: New(isolate);

// 输入编译和运行hello world脚本的上下文。
Context :: Scope context_scope(context);

// 创建一个包含JavaScript源代码的字符串。
Local< String> source =
String :: NewFromUtf8(isolate, 'test'+',test2!'
NewStringType :: kNormal)。ToLocalChecked();

// 编译源代码。
Local< Script> ; script = Script :: Compile(context,source).ToLocalChecked();

// 运行脚本以获得结果。
本地及LT;值> result = script->运行(context).ToLocalChecked();

// 将结果转换为UTF8字符串并打印出来。
String :: Utf8Value utf8(result);
printf( %s \ n,* utf8);
}

// 处理隔离并拆除V8。
isolate-> Dispose();
V8 :: Dispose();
V8 :: ShutdownPlatform();
// 删除平台;
删除 create_params.array_buffer_allocator;
return 0 ;
}





我的尝试:



1.sln文件,在其他选项中重新生成。



2. demangling。



3.添加和删除lib文件。

解决方案

您必须在项目设置。它是一个链接器选项卡,其中包含要使用的库的字段。它们必须以某种方式在项目的搜索路径中或使用目录的条目。



它也可以是命名空间问题,但为此您必须参考Google文档。


I used a GYP the Google's V8 source, to build and compile after all.sln generated dynamic library, to generate a lib file in the Debug & Release mode. (I've used Google's V8 source 5.4.0 version.)

I simply use the v8, and it outputs a run value of javascript.

I created a new win32 application console project in visual studio 2015 when you try to use the Google's V8.

That I know, it is sufficient to use a v8.lib file and v8_base.lib files and v8_nosnapshot.lib file.

1. I was generated v8_base_0.lib, v8_base_1.lib, v8_base_2.lib, the v8_base_3.lib. Do I use what v8_base.lib?
2. Do I need other lib file?

And I was used as whole copy the include directory in order to use the h file. I also, have copied the dll file(icu18n.dll, icuuc.dll, v8.dll).

I was used to copy the hello-world.cc source in the .cpp file.

However, Then compile a c ++ application source, an error occurs.

1>----- Build started: Project: v8Application2, Configuration: Debug Win32 -----
1> v8Application2.cpp
1>v8Application2.obj : error LNK2019: unresolved external symbol _main referenced in function"class v8::Platform * __cdecl v8::platform::CreateDefaultPlatform(int)" (?CreateDefaultPlatform@platform@v8@@YAPAVPlatform@2@H@Z) 1>c:\users\kito\documents\visual studio 2015\Projects\v8Application2\Debug\v8Application2.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



3. how do i do this problem?

I was upload the .vcxproj and .cpp files of c ++ application. Please check the visual studio 2015 'c++ win32 application console' project file.

And see also my other question Link.
I spent three weeks on this issue. Not going progress at all stuck here.

I there is a need for help that you are wrong in any way. Or ask the relevant link that corresponds to this.

This is the main source of the program.
I had to copy the main source from the example source.
I'm not part of the change separately.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "include/libplatform/libplatform.h"
#include "include/v8.h"

using namespace v8;

int main(int argc, char* argv[]) {

	V8::InitializeICUDefaultLocation(argv[0]);
	V8::InitializeExternalStartupData(argv[0]);
	Platform* platform = platform::CreateDefaultPlatform();
	V8::InitializePlatform(platform);
	V8::Initialize();

	// Create a new Isolate and make it the current one.
	Isolate::CreateParams create_params;
	create_params.array_buffer_allocator =
		v8::ArrayBuffer::Allocator::NewDefaultAllocator();
	Isolate* isolate = Isolate::New(create_params);
	{
		Isolate::Scope isolate_scope(isolate);

		// Create a stack-allocated handle scope.
		HandleScope handle_scope(isolate);

		// Create a new context.
		Local<Context> context = Context::New(isolate);

		// Enter the context for compiling and running the hello world script.
		Context::Scope context_scope(context);

		// Create a string containing the JavaScript source code.
		Local<String> source =
			String::NewFromUtf8(isolate, "'test' + ', test2!'",
				NewStringType::kNormal).ToLocalChecked();

		// Compile the source code.
		Local<Script> script = Script::Compile(context, source).ToLocalChecked();

		// Run the script to get the result.
		Local<Value> result = script->Run(context).ToLocalChecked();

		// Convert the result to an UTF8 string and print it.
		String::Utf8Value utf8(result);
		printf("%s\n", *utf8);
	}

	// Dispose the isolate and tear down V8.
	isolate->Dispose();
	V8::Dispose();
	V8::ShutdownPlatform();
	//delete platform;
	delete create_params.array_buffer_allocator;
	return 0;
}



What I have tried:

1. The "sln file", re-generate at other options.

2. demangling.

3. Add and remove the "lib file".

解决方案

You must make an entry in the project settings. It is a linker tab with a field for the libs to use. They must be somehow in the search path of the project or use an entry for the directory.

It can also be an namespace issue, but that for that you must consult the Google documentation.


这篇关于如何在C ++应用程序中使用Google的v8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆