当我尝试在V8 - JavaScript中编译c++时,出现了许多错误
Many erros when i try compile c++ in V8 - JavaScript
我试着使用:
git clone git://github.com/v8/v8.git v8 && cd v8 or svn checkout http://v8.googlecode.com/svn/trunk/ v8
使用填词:
make dependencies
sudo apt-get install apt-file;
sudo apt-get install libc6-dev-i368 lib32stdc++6;
当我尝试编译一个简单的文件为:
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; }
命令使用:
g++ test.cpp -Ideps/v8/include -Ldeps/v8/ -lv8 -lpthread
我得到以下错误输出:
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
有什么问题吗?有人能给我指个正确的方向吗?
根据@Sim的回答,以下是工作版本。
#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;
}
要编译和运行它,假设../include
中有v8头文件,../lib
中有v8库
g++ -L../lib -I../include hello.cc -o a.out -lv8_base.x64 -lv8_snapshot -lpthread && ./a.out
您尝试编译的c++代码是V8"入门"指南中的部分示例("伪代码")。它不能像现在这样工作。下面同一文档中说明了运行该程序所需的操作:
- 包含V8 .h头文件,并将项目链接到静态(或共享)V8库。
- 导入v8命名空间(
using namespace v8
); - 获取默认隔离(
Isolate::GetCurrent()
)的引用/指针。 - 为本地句柄创建一个HandleScope
- 创建并进入V8执行上下文。
- 只有这样你才能使用类似于上面的代码。
详细信息请参考V8入门指南
相关文章:
- Node.js v6.2.0类扩展不是函数错误
- Jquery菜单操作不稳定,定位不正确,存在一般错误
- document.open/document.write没有正确地清除chrome中的文档——这是chrome的错误吗
- 试图在引导模式内动态生成图表,得到offsetWidth错误
- 为什么会出现错误;未捕获的类型错误:undefined不是函数;
- 我如何修复包含在captcha的addthis中的错误
- 同样,同样的错误'ahorcado.js:26未捕获类型错误:无法读取属性'beginPath'
- 节点是否需要模块传递带有方括号的arg?这是个错误吗
- Webpack/Rect:遵循egghead.io教程,但出现错误:您可能需要一个合适的加载程序来处理此文件类型
- CKFinder 3为所选文件返回错误的URL
- 同位素库错误:未捕获错误无布局模式包装生产线8
- 铬:“;未捕获的语法错误:意外的标记:"
- 如何通过自己获得Chrome扩展的用户反馈/错误报告
- 相位器状态未捕获参考错误
- /undefined在我的404错误日志中多次出现
- Javascript未捕获语法错误意外的标识符错误
- javascript:如何在antlr生成的Lexer中进行错误处理
- Angularjs工厂注入错误
- 可以设置“;文件名"发生错误时显示的内联脚本标记的
- 为什么忽略了eval()代码中的语法错误