如何构建 &安装 GLFW 3 并在 Linux 项目中使用它 [英] How to build & install GLFW 3 and use it in a Linux project

查看:57
本文介绍了如何构建 &安装 GLFW 3 并在 Linux 项目中使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨晚我工作到很晚,试图从源代码构建适用于 Linux 的 GLFW 3 包.这个过程花了我很长时间,总共大约 3 个小时,部分是因为我不熟悉 CMake,部分是因为我不熟悉 GLFW.

Last night I was working late trying to build the GLFW 3 packages for Linux from source. This process took me a very long time, about 3 hours in total, partly because I am unfamiliar with CMake, and partly because I am was unfamiliar with GLFW.

我希望这篇文章能让你从我昨天遇到的困难中解脱出来!我想我应该写一篇简短的文章,希望能为你节省几个小时的生命......

I hope that this post will save you from the difficulty I had yesterday! I thought I should make a short write-up, and hopefully save you several hours of your life...

感谢urraka",b6"和尼克拉斯"在#glfw IRC 频道上,我已经能够让 glfw 3.0.1 版正常工作.

Thanks to "urraka", "b6" and "niklas" on the #glfw IRC channel, I have been able to get glfw version 3.0.1 working.

事实证明这不是一个微不足道的过程(当然不适合我,我不是专家)因为网络上没有太多关于 glfw3 的文档,特别是关于使用 CMake 设置它的文档.

It turns out this is not a trivial process (certainly not for me, I am no expert) as there is not much documentation on the web about glfw3, particularly about setting it up with CMake.

我被要求把它分成一个问答部分,所以我这样做了,现在答案部分在下面.

I was asked to split this into a question and answer section, and so I have done that, and the answer parts are now below.

如果 GLFW3 的任何维护者看到这一点,那么我给他们的信息是请添加在 Windows、Mac OS X 和 Linux 上设置 GLFW3";部分到您的网站!使用 GLFW 编写程序非常容易,因为在线文档非常好,快速浏览所有可用的类和模块,您就可以开始了.这里 的测试项目示例也非常好.我发现的两个主要问题是,首先如何在我的系统上设置 GLFW3,其次如何构建 GLFW3 项目?对于非专家来说,这两件事可能还不够清楚.

If any of the maintainers of GLFW3 see this, then my message to them is please add a "setting up GLFW3 on Windows, Mac OS X and Linux" section to your website! It is quite easy to write programs with GLFW, since the online documentation is quite good, a quick scan of all the available classes and modules and you'll be ready to go. The example of a test project featured here is also very good. The two main problems I found were, firstly how do I set up GLFW3 on my system, and secondly how to I build a GLFW3 project? These two things perhaps aren't quite clear enough for a non-expert.

今天简单看了一下(日期:2014-01-14)好像自从我上次看GLFW网站已经发生了很大的变化,现在有一个关于编译GLFW和用GLFW构建程序的部分,我认为是新的.

Had a brief look today (Date: 2014-01-14) it looks as if the GLFW website has undergone heavy changes since I last looked and there is now a section on compiling GLFW and buliding programs with GLFW, which I think are new.

推荐答案

2020 更新答案

现在是 2020 年(7 年后),这段时间我对 Linux 有了更多的了解.特别是在安装库时运行 sudo make install 可能不是一个好主意,因为它们可能会干扰包管理系统.(在这种情况下 apt,因为我使用的是 Debian 10.)

2020 Updated Answer

It is 2020 (7 years later) and I have learned more about Linux during this time. Specifically that it might not be a good idea to run sudo make install when installing libraries, as these may interfere with the package management system. (In this case apt as I am using Debian 10.)

如有不对之处,请在评论中指正.

If this is not correct, please correct me in the comments.

此信息取自 GLFW 文档,但我已扩展/简化了与 Linux 用户相关的信息.

This information is taken from the GLFW docs, however I have expanded/streamlined the information which is relevant to Linux users.

  • 转到主目录并从 github 克隆 glfw 存储库
cd ~
git clone https://github.com/glfw/glfw.git
cd glfw

  • 此时您可以创建一个构建目录并按照此处的说明进行操作 (glfw构建说明),但我选择不这样做.以下命令似乎在 2020 年仍然有效,但 glfw 在线说明中没有明确说明.
    • You can at this point create a build directory and follow the instructions here (glfw build instructions), however I chose not to do that. The following command still seems to work in 2020 however it is not explicitly stated by the glfw online instructions.
    • cmake -G "Unix Makefiles"
      

      • 您可能需要在 (?) 之前运行 sudo apt-get build-dep glfw3.我按照说明运行了这个命令和 sudo apt install xorg-dev.

        • You may need to run sudo apt-get build-dep glfw3 before (?). I ran both this command and sudo apt install xorg-dev as per the instructions.

          最后运行make

          现在在您的项目目录中,执行以下操作.(转到使用 glfw 库的项目)

          Now in your project directory, do the following. (Go to your project which uses the glfw libs)

          创建一个CMakeLists.txt,我的看起来像这样

          Create a CMakeLists.txt, mine looks like this

          CMAKE_MINIMUM_REQUIRED(VERSION 3.7)
          PROJECT(project)
          
          SET(CMAKE_CXX_STANDARD 14)
          SET(CMAKE_BUILD_TYPE DEBUG)
          
          set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
          set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
          set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
          
          add_subdirectory(/home/<user>/glfw /home/<user>/glfw/src)
          
          
          FIND_PACKAGE(OpenGL REQUIRED)
          
          SET(SOURCE_FILES main.cpp)
          
          ADD_EXECUTABLE(project ${SOURCE_FILES})
          TARGET_LINK_LIBRARIES(project glfw)
          TARGET_LINK_LIBRARIES(project OpenGL::GL)
          

          • 如果您不喜欢 CMake,那么我很抱歉,但在我看来,这是让您的项目快速运行的最简单方法.我建议学习使用它,至少在基本水平上.遗憾的是我不知道任何好的 CMake 教程

            • If you don't like CMake then I appologize but in my opinion it is the easiest way to get your project working quickly. I would recommend learning to use it, at least to a basic level. Regretably I do not know of any good CMake tutorial

              然后执行 cmake .make,你的项目应该被构建并链接到 glfw3 共享库

              Then do cmake . and make, your project should be built and linked against glfw3 shared lib

              有一些方法可以创建动态链接库.我相信我在这里使用了静态方法.如果您知道的比我多,请在下面的这个答案中评论/添加一个部分

              There is some way of creating a dynamic linked lib. I believe I have used the static method here. Please comment / add a section in this answer below if you know more than I do

              这应该适用于其他系统,如果不能,请告诉我,如果可以,我会提供帮助

              This should work on other systems, if not let me know and I will help if I am able to

              这篇关于如何构建 &amp;安装 GLFW 3 并在 Linux 项目中使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆