环境:CentOS7
内存:8g(内存过少编译osg时会出现内存不足导致编译失败的情况,内存设置为4G时失败了,我直接加到了8g,所以就以8g为准了)文章来源地址https://www.toymoban.com/news/detail-803448.html
- 安装和配置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位的
- 升级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
- 安装osg
- 安装编译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 的软链接,必须链接到'/usr/bin/python3',不能时python3.7或者其他的,一会编译osg的依赖时会寻找python3,且是根据名称寻找
ln -s /usr/local/python3/bin/python3.7 /usr/bin/python3
# 添加 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版本是否已更新
#请注意,激活仅对当前会话有效
- 安装osg
vcpkg install osg
- 测试是否安装成功
#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://www.toymoban.com/news/detail-803448.html
到了这里,关于Linux系统下安装Vcpkg,并使用Vcpkg安装、编译OpenSceneGraph的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!