Many erros when i try compile c++ in V8 - JavaScript

泄露秘密 提交于 2019-12-11 23:18:06

问题


I try using:

git clone git://github.com/v8/v8.git v8 && cd v8 or svn checkout http://v8.googlecode.com/svn/trunk/ v8 

Use libs:

make dependencies
sudo apt-get install apt-file;
sudo apt-get install libc6-dev-i368 lib32stdc++6;

When i try compile a simple file as:

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

  // Create a string containing the JavaScript source code.
  String source = String::New("'Hello' + ', World'");

  // Compile the source code.
  Script script = Script::Compile(source);

  // Run the script to get the result.
  Value result = script->Run();

  // Convert the result to an ASCII string and print it.
  String::AsciiValue ascii(result);
  printf("%s\n", *ascii);
  return 0;
}

the command use :

g++ test.cpp -Ideps/v8/include -Ldeps/v8/ -lv8 -lpthread

I get the follow error output:

v8/src/

test.cpp: Na função ‘int main(int, char**)’:
test.cpp:4:3: error: ‘String’ was not declared in this scope
test.cpp:4:10: error: expected ‘;’ before ‘source’
test.cpp:7:3: error: ‘Script’ was not declared in this scope
test.cpp:7:10: error: expected ‘;’ before ‘script’
test.cpp:10:3: error: ‘Value’ was not declared in this scope
test.cpp:10:9: error: expected ‘;’ before ‘result’
test.cpp:13:3: error: ‘String’ is not a class or namespace
test.cpp:13:22: error: expected ‘;’ before ‘ascii’
test.cpp:14:19: error: ‘ascii’ was not declared in this scope
test.cpp:14:24: error: ‘printf’ was not declared in this scope

What is the issue? Can someone point me in the right direction?


回答1:


Based on the answer from @Sim, here is the working version.

#include "v8.h"
using namespace v8;


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

  // Get the default Isolate created at startup.
  Isolate* isolate = Isolate::GetCurrent();

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

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

  // Here's how you could create a Persistent handle to the context, if needed.
  Persistent<Context> persistent_context(isolate, context);

  // Enter the created 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::New("'Hello' + ', World'");

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

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

  // Convert the result to an ASCII string and print it.
  String::AsciiValue ascii(result);
  printf("%s\n", *ascii);
  return 0;
}

To compile and run it, assume you have v8 headers files in ../include and v8 libraries in ../lib

g++ -L../lib -I../include hello.cc -o a.out -lv8_base.x64 -lv8_snapshot -lpthread && ./a.out



回答2:


The C++ code you are trying to compile is partial example ("pseudo code") taken from the V8 "Getting Started" guide. It can't work as it is. What you need to get this running is illustrated in the same document below:

  1. Include v8.h header and link your project to the static (or shared) V8 library.
  2. Import v8 namespace (using namespace v8);
  3. Get a reference/pointer to the default Isolate (Isolate::GetCurrent()).
  4. Create a HandleScope for local handles.
  5. Create and enter the V8 execution context.
  6. And only then you can use something like the code above.

Refer to the V8 Getting Started Guide for details.



来源:https://stackoverflow.com/questions/16949690/many-erros-when-i-try-compile-c-in-v8-javascript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!