尝试在ubuntu上编译V8样本/ hello-world.cc
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/libplatform/libplatform.h"
#include "include/v8.h"
int main(int argc, char* argv[]) {
// Initialize V8.
v8::V8::InitializeICUDefaultLocation(argv[0]);
v8::V8::InitializeExternalStartupData(argv[0]);
std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
// Create a new Isolate and make it the current one.
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
v8::Isolate* isolate = v8::Isolate::New(create_params);
{
v8::Isolate::Scope isolate_scope(isolate);
// Create a stack-allocated handle scope.
v8::HandleScope handle_scope(isolate);
// Create a new context.
v8::Local<v8::Context> context = v8::Context::New(isolate);
// Enter the context for compiling and running the hello world script.
v8::Context::Scope context_scope(context);
// Create a string containing the JavaScript source code.
v8::Local<v8::String> source =
v8::String::NewFromUtf8(isolate, "'Hello' + ', World!'",
v8::NewStringType::kNormal)
.ToLocalChecked();
// Compile the source code.
v8::Local<v8::Script> script =
v8::Script::Compile(context, source).ToLocalChecked();
// Run the script to get the result.
v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
// Convert the result to an UTF8 string and print it.
v8::String::Utf8Value utf8(isolate, result);
printf("%s\n", *utf8);
}
// Dispose the isolate and tear down V8.
isolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete create_params.array_buffer_allocator;
return 0;
}
使用以下命令:
g++ -I. -Iinclude samples/hello-world.cc -o hello-world -Wl,--start-group \
out.gn/x64.release/libicui18n.so out.gn/x64.release/libicuuc.so\
out.gn/x64.release/libv8_libbase.so\
out.gn/x64.release/libv8_libplatform.so\ out.gn/x64.release/libv8.so\
out.gn/x64.release/libc++.so\
-Wl,--end-group -lrt -ldl -pthread -std=c++0x
收到错误:
hello-world.cc:(.text+0x75): undefined reference to
v8::platform::NewDefaultPlatform(int, v8::platform::IdleTaskSupport,
v8::platform::InProcessStackDumping,
std::unique_ptr<v8::TracingController,
std::default_delete<v8::TracingController> >)
请注意,我用GN args构建了V8:
&#34; is_debug = false,is_official_build = true,is_component_build = true,is_cfi = false,is_clang = false,v8_use_external_startup_data = false,treat_warnings_as_errors = false,use_custom_libcxx = false,use_sysroot = false,use_gold = false&#34; < / p>
我是否在构建V8或编译样本/ hello-world.cc
时出错答案 0 :(得分:0)
在编译器命令行中,尝试在\
之后删除out.gn/x64.release/libv8_libplatform.so
。 \
是一个“转义”字符,因此它会更改后续空格的解释方式 - 具体而言,它会使其成为文件名的一部分而不是文件名分隔符,从而导致文件名不存在。 / p>
这有帮助吗?