0

Trying to compile V8 samples/hello-world.cc on ubuntu

#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;
}

with following command:

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

And getting an Error:

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> >)

Note that I built V8 with GN args:

"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"

Am I doing something wrong with V8 built or compilation of samples/hello-world.cc

1 Answers1

0

In your compiler command line, try removing the \ after out.gn/x64.release/libv8_libplatform.so. \ is an "escape" character, so it changes how the following space is interpreted -- specifically, it makes it part of the file name rather than a file name separator, resulting in a filename that does not exist.

Does that help?

jmrk
  • 34,271
  • 7
  • 59
  • 74