Linux系统下安装Vcpkg,并使用Vcpkg安装、编译OpenSceneGraph

发布时间:2024年01月18日

环境:CentOS7
内存:8g(内存过少编译osg时会出现内存不足导致编译失败的情况,内存设置为4G时失败了,我直接加到了8g,所以就以8g为准了)

  1. 安装和配置vcpkg
cd ~/
git clone https://www.github.com/microsoft/vcpkg
cd vcpkg/
./bootstrap-vcpkg.sh # 这一步将生成二进制文件 `/vcpkg/vcpkg
sudo ln -s ~/vcpkg/vcpkg /usr/bin/vcpkg # 这一步是创建软连接,让你在shell中能直接使用vcpkg命令
export VCPKG_DEFAULT_TRIPLET=x64-linux #设置环境变量:vcpkg默认的三元组,保证安装的包是64位的
  1. 升级git
git --version #git 版本至少为2.7.4
sudo yum -y remove git
sudo yum -y install https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm
sudo yum -y install git 
git --verison
  1. 安装osg
  1. 安装编译osg时需要的依赖
#安装python3

yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make libffi-devel #安装编译 Python3的相关包
# 添加 epel 扩展源 
yum -y install epel-release 
# 安装 pip 
yum install python-pip
# 安装pip3,也可以不安装
yum install -y python3-pip
pip install wget
# 从华为云镜像站下载
wget https://mirrors.huaweicloud.com/python/3.7.2/Python-3.7.2.tgz
# 解压缩
tar -zxvf Python-3.7.2.tgz
# 进入解压后的目录
cd Python-3.7.2
# 依次执行下面命令进行手动编译
./configure prefix=/usr/local/python3 
make && make install
# 删除原来的 python 软链接
rm /usr/bin/python
# 添加 python 的软链接 
ln -s /usr/local/python3/bin/python3.7 /usr/bin/python
# 添加 python3 的软链接 
ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3.7
# 添加 pip3 的软链接 
ln -s /usr/local/python3/bin/pip3.7 /usr/bin/pip3.7
# 测试是否安装成功了 
python -V
vi /usr/bin/yum 
# 把 #! /usr/bin/python 修改为 #! /usr/bin/python2
vi /usr/libexec/urlgrabber-ext-down
# 把 #! /usr/bin/python 修改为 #! /usr/bin/python2

sudo yum -y install perl-IPC-Cmd # 编译osg时,某个依赖编译时所需要的包

export CFLAGS="-std=c99" #编译fontconfig、pcre2要c99标准

sudo yum install -y libxi-devel mesa-libGLU-devel #编译freeglut要安装libxi-devel和mesa-libGLU-devel
#编译gdal需要高版本的gcc
sudo yum install -y centos-release-scl #安装CentOS SCL仓库
sudo yum install -y devtoolset-8 #安装所需的devtoolset包(例如,devtoolset-8)
scl enable devtoolset-8 bash #激活devtoolset以使用新的GCC版本
gcc --version #检查GCC版本是否已更新
#请注意,激活仅对当前会话有效
  1. 安装osg
vcpkg install osg
  1. 测试是否安装成功
#include <osg/Geode>
#include <osg/Geometry>
#include <osgViewer/Viewer>
#include <iostream>
int main()
{
    // Create a root node
    osg::ref_ptr<osg::Group> root = new osg::Group;

    // Create a Geode containing a simple cube
    osg::ref_ptr<osg::Geode> geode = new osg::Geode;
    osg::ref_ptr<osg::Geometry> geometry = new osg::Geometry;

    osg::ref_ptr<osg::Vec3Array> vertices = new osg::Vec3Array;
    vertices->push_back(osg::Vec3(-0.5, -0.5, 0));
    vertices->push_back(osg::Vec3(0.5, -0.5, 0));
    vertices->push_back(osg::Vec3(0.5, 0.5, 0));
    vertices->push_back(osg::Vec3(-0.5, 0.5, 0));

    geometry->setVertexArray(vertices);

    osg::ref_ptr<osg::DrawElementsUInt> quad =
        new osg::DrawElementsUInt(osg::PrimitiveSet::QUADS, 0);
    quad->push_back(0);
    quad->push_back(1);
    quad->push_back(2);
    quad->push_back(3);

    geometry->addPrimitiveSet(quad);

    geode->addDrawable(geometry);
    root->addChild(geode);
    std::cout<<"successed!"<<std::endl;
    // Create a viewer and set the scene data
    osgViewer::Viewer viewer;
    viewer.setSceneData(root);

    // Run the viewer loop
    return viewer.run();
}
g++ -o osg_test osg_test.cpp -I/root/vcpkg/installed/x64-linux/include -Wl,--no-as-needed -L/root/vcpkg/installed/x64-linux/lib -losgViewer -losgUtil -losgGA -losgDB -losg -lOpenThreads -ldl -lGL -lpthread -lm -lstdc++
./osg_test
# 执行结果,打印出successed!就说明安装成功了,至于后面的报错应该与显卡驱动等有关,但是在linux中安装osg也不是为了渲染
# successed!
# Warning: GraphicsContext::WindowingSystemInterfaces::getWindowingSystemInterface() failed, no interfaces available.
# AcrossAllScreens::configure() : Error, no WindowSystemInterface available, cannot create windows.
# Viewer::realize() - failed to set up any windows

文章来源:https://blog.csdn.net/weixin_41364246/article/details/135682760
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。