当前位置: 首页 > 工具软件 > GLFW > 使用案例 >

OpenGL学习001-GLFW环境搭建

孙熠彤
2023-12-01

      GLFW是一个专门针对OpenGL的C语言库,它提供了一些渲染物体所需的最低限度的接口。它允许用户创建OpenGL上下文,定义窗口参数以及处理用户输入,下面来记述下GLFW在Ubuntu环境下的构建。

1.源代码下载

     在ubuntu下创建glfw工作目录,然后下载glfw源代码

mkdir glfw
cd glfw
wget https://github.com/glfw/glfw/releases/download/3.3.4/glfw-3.3.4.zip
unzip glfw-3.3.4.zip

2. 检查依赖

    glfw构建时需要特定的依赖包,libx11-dev libxrandr-dev libxinerama-dev libxcursor-dev libxi-dev,使用dpkg命令检查上述包是否已经安装。

 2.1检查libx11-dev

ubuntu@ubuntu-VirtualBox:~/glfw$ dpkg -s libx11-dev
Package: libx11-dev
Status: install ok installed
Priority: optional
Section: libdevel
Installed-Size: 2528
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Multi-Arch: same
Source: libx11
Version: 2:1.6.9-2ubuntu1.2
Replaces: x11proto-dev (<< 2019.2)
Depends: libx11-6 (= 2:1.6.9-2ubuntu1.2), libxau-dev (>= 1:1.0.0-1), libxdmcp-dev (>= 1:1.0.0-1), x11proto-dev (>= 2019.2-1), xtrans-dev, libxcb1-dev
Suggests: libx11-doc
Description: X11 client-side library (development headers)
 This package provides a client interface to the X Window System, otherwise
 known as 'Xlib'.  It provides a complete API for the basic functions of the
 window system.
 .
 This package contains the development headers for the library found in
 libx11-6. Non-developers likely have little use for this package.
 .
 More information about X.Org can be found at:
 <URL:https://www.X.org>
 .
 This module can be found at
 git://anongit.freedesktop.org/git/xorg/lib/libX11
Original-Maintainer: Debian X Strike Force <debian-x@lists.debian.org>

Status: install ok installed  表明libx11-dev已经安装

2.2检查libxrandr-dev

ubuntu@ubuntu-VirtualBox:~/glfw$ dpkg -s libxrandr-dev
dpkg-query: package 'libxrandr-dev' is not installed and no information is available
Use dpkg --info (= dpkg-deb --info) to examine archive files.
ubuntu@ubuntu-VirtualBox:~/glfw$

 查询结果显示libxrandr-dev未安装,则将其安装。

ubuntu@ubuntu-VirtualBox:~/glfw$ sudo apt-get -y install libxrandr-dev

再次检查libxrandr-dev安装情况

ubuntu@ubuntu-VirtualBox:~/glfw$ dpkg -s libxrandr-dev
Package: libxrandr-dev
Status: install ok installed
Priority: optional
Section: libdevel
Installed-Size: 131
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: amd64
Multi-Arch: same
Source: libxrandr
Version: 2:1.5.2-0ubuntu1
Depends: libxrandr2 (= 2:1.5.2-0ubuntu1), libx11-dev, libxext-dev, x11proto-randr-dev (>= 1.4), libxrender-dev
Description: X11 RandR extension library (development headers)
 libXrandr provides an X Window System client interface to the RandR
 extension to the X protocol.
 .
 The RandR extension allows for run-time configuration of display attributes
 such as resolution, rotation, and reflection.
 .
 This package contains the development headers for the library found in
 libxrandr2.  Non-developers likely have little use for this package.
 .
 More information about X.Org can be found at:
 <URL:https://www.X.org>
 .
 This module can be found at
 git://anongit.freedesktop.org/git/xorg/lib/libXrandr
Original-Maintainer: Debian X Strike Force <debian-x@lists.debian.org>
ubuntu@ubuntu-VirtualBox:~/glfw$

显示librandr-dev已安装

2.3 检查libxinerama-dev

     按照2.1 libx11-dev, 2.2libxrandr-dev操作步骤,未安装则安装,已安装则忽略。

2.4 检查libxcursor-dev

         按照2.1 libx11-dev, 2.2libxrandr-dev操作步骤,未安装则安装,已安装则忽略。

2.5 检查 libxi-dev

      按照2.1 libx11-dev, 2.2libxrandr-dev操作步骤,未安装则安装,已安装则忽略。

3 编译 & 安装

    

ubuntu@ubuntu-VirtualBox:~/glfw/glfw-3.3.4$ cd   #切入glfw源代码目录
ubuntu@ubuntu-VirtualBox:~$ cd glfw/glfw-3.3.4/
ubuntu@ubuntu-VirtualBox:~/glfw/glfw-3.3.4$ pwd
/home/ubuntu/glfw/glfw-3.3.4
ubuntu@ubuntu-VirtualBox:~/glfw/glfw-3.3.4$ mkdir build-dir  #创建编译目录
ubuntu@ubuntu-VirtualBox:~/glfw/glfw-3.3.4$ cd build-dir/
ubuntu@ubuntu-VirtualBox:~/glfw/glfw-3.3.4/build-dir$ cmake .. -DCMAKE_INSTALL_PREFIX=/home/ubuntu/glfw/Install -DCMAKE_BUILD_TYPE=RELEASE  #编译预处理
ubuntu@ubuntu-VirtualBox:~/glfw/glfw-3.3.4/build-dir$ make -j3 #编译
ubuntu@ubuntu-VirtualBox:~/glfw/glfw-3.3.4/build-dir$ make install  #安装


4. 配置PKG_CONFIG_PATH

     需要源码编译高版本的glfw库,但又不想安装到默认路径下,这样可能可能会覆盖默认安装glfw,则需要将自定义安装路径。默认的 CMAKE_INSTALL_PREFIX 为 /home/ubuntu/OpenGL/glfw/Install/通过以下参数来自定义安装路径。

DCMAKE_INSTALL_PREFIX=/home/ubuntu/glfw/Install

     安装完成后需要将/home/ubuntu/OpenGL/glfw/Install/lib/pkgconfig/glfw3.pc 添加到环境变量PKG_CONFIG_PATH中,编辑~/.bash_profile,添加如下:

export PKG_CONFIG_PATH=/home/ubuntu/OpenGL/glfw/Install/lib/pkgconfig:$PKG_CONFIG_PATH

添加完毕后保存,执行source ~/.bash_profile让环境变量生效

source ~/.bash_profile

 查看PKG_CONFIG_PATH


ubuntu@ubuntu-VirtualBox:~/OpenGL/glfw/Install/lib/pkgconfig$ echo $PKG_CONFIG_PATH
/home/ubuntu/OpenGL/glfw/Install/lib/pkgconfig:
ubuntu@ubuntu-VirtualBox:~/OpenGL/glfw/Install/lib/pkgconfig$ pkg-config --modversion glfw3.pc
3.3.4

若通过PKG_CONFIG_PATH, pkg-config --modversion glfw3 查看到值 则表明GLFW安装成功。

 类似资料: