v8环境搭建采坑记录

本文详细记录了在CentOS环境中搭建V8时遇到的各种问题及解决方案,包括GCC升级、动态链接库配置、依赖库缺失等问题,为类似工作提供了参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目组有把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();

      // Compile the source code.
      v8::Local<v8::Script> script =
          v8::Script
YOLO (You Only Look Once) 是一种实时物体检测算法,V8 版本通常指YOLO的最新版本之一。要在Python环境搭建YOLO V8,你需要按照以下步骤操作: 1. **安装依赖库**: - 首先,确保已经安装了基本的Python环境和必要的科学计算库如TensorFlow或PyTorch。对于YOLO V8,由于它可能基于Darknet框架,所以需要安装`torch`和`torchvision`。 ```bash pip install torch torchvision ``` 2. **下载预训练模型**: YOLO V8的预训练权重通常可以从GitHub或其他官方仓库获取。你需要下载对应的权重文件和配置文件,例如`yolov8.cfg`和`yolov8.weights`。 3. **下载Darknet源码**: 如果YOLO V8是基于Darknet开发的,从Darknet官网(https://github.com/AlexeyAB/darknet)克隆或下载源码。 4. **构建Darknet**: 使用Darknet的Makefile系统构建暗网工具链,这将包含YOLO的推理工具。在Darknet目录下运行`make darknet` 或 `make yolo`。 5. **配置路径**: 将模型文件的路径添加到环境变量中,以便于在命令行中调用。 6. **测试模型**: 在Python中,你可以通过`darknet.detect()`函数加载模型并进行物体检测。但是,为了直接使用YOLO V8,你可能需要使用专门的Python库如`pydarknet`,或者自定义接口来加载模型。 ```python from pydarknet import Detector # 初始化模型 detector = Detector("yolov8.cfg", "yolov8.weights", 0.5, 0.4) # 加载图片或视频进行检测 image_path = 'path_to_your_image.jpg' predictions = detector(image_path) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值