v8环境搭建采坑记录

项目组有把js接入C++服务求的需求,故开始了v8接入的工作,用了一天多时间,v8才在centos环境上成功安装,过程中踩了很多坑,下面将采坑过程记录如下:

centos下编译安装v8:
 
查看centos版本号:
#cat /etc/redhat-release
 
在环境上 gcc版本(需要支持C++14)、glibc库(GLIBC-2.18)版本都OK,GN编译选项配置好,并且环境变量都配置成功的前提下,下面的V8编译、安装的脚本,功能是OK的:
 
 
#How to compile and install v8 and v8js on CentOS 7
#https://velenux.wordpress.com/2018/04/17/how-to-compile-and-install-v8-and-v8js-on-centos-7/

#v8在centos下的编译安装脚本
#!/bin/bash
 
set -x  # debug
set -e  # exit on all errors
 
# update and install basic packages
sudo yum -y upgrade #right
sudo yum -y --enablerepo=epel --enablerepo=remi-php71 install git subversion make gcc-c++ chrpath redhat-lsb-core php php-devel php-pear #right
 
mkdir -p local/src #right
 
# install the depot tools from google
git clone https://chromium.googlesource.com/chromium/tools/depot_tools.git local/depot_tools #需要vpn啊,没有vpn的话,该命令不必执行,可以使用github上的镜像(最终是用github镜像成功安装了v8)
export PATH=$PATH:$PWD/local/depot_tools   #right
 
# install v8
cd local/src  #right
fetch v8 #需要vpn啊,没有vpn的话,该命令不必执行,可以直接使用github镜像(最终是用github镜像成功安装了v8)
cd v8  #right
//检查下载v8的依赖包
gclient sync --with_branch_heads --with_tags -Rv --disable-syntax-validation  #right
#在上面这个命令执行时,可能会提示 
client not configured; see 'gclient config' 
那么需要我们对gclient进行配置,方法如下,执行如下命令 
gclient config https://chromium.googlesource.com/v8/v8   #right

./tools/dev/v8gen.py -vv x64.release -- 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_gold=false  #right,非常关键(尤其是use_custom_libcxx、is_clang、v8_use_external_startup_data这三个编译选项的配置),非常重要,这里的编译选项配置如果不正确,在下面的编译和代码执行过程中会出现各种问题,具体出现的问题都罗列在了下面的”问题解决描述“中

time ninja -C out.gn/x64.release #编译  #right
time ./tools/run-tests.py --gn   #right

  

 编译成功后的二进制文件安装:
#v8 install
mkdir -p /opt/v8/{lib,include}
 
cd /home/ldx/software/v8-instal/v8
/bin/cp -v out.gn/x64.release/lib*.so out.gn/x64.release/*_blob.bin out.gn/x64.release/icudtl.dat /opt/v8/lib/
/bin/cp -v out.gn/x64.release/lib*.so out.gn/x64.release/*_blob.bin out.gn/x64.release/icudtl.dat /usr/lib64
/bin/cp -v out.gn/x64.release/lib*.so out.gn/x64.release/*_blob.bin out.gn/x64.release/icudtl.dat /usr/local/lib
/bin/cp -vR include/* /opt/v8/include/

#并配置环境变量(PATH、LIB、LD_LIB、INCLUDE_PATH)

 

#使用了v8库的c++代码的编译方法
g++ -o main xx.cpp -std=c++11 -lpthread -lv8 -lv8_libplatform

#生成可执行文件后,即可运行:
./main

源代码hello-world.cc(v8/sample下面的示例文件)内容如下:  

  

// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#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();

      </
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值