如何构建和安装 GLFW 3 并在 Linux 项目中使用它

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17768008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:19:27  来源:igfitidea点击:

How to build & install GLFW 3 and use it in a Linux project

linuxbuildinstallcmakeglfw

提问by user3728501

GLFW3

GLFW3

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.

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

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

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

感谢#glfw IRC 频道上的“urraka”、“b6”和“niklas”,我已经能够让glfw 3.0.1 版正常工作。

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.

事实证明,这不是一个微不足道的过程(当然不适合我,我不是专家)因为网络上没有太多关于 glfw3 的文档,特别是关于使用 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.

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

Are you a maintainer of GLFW, or a member of the GLFW team?

您是 GLFW 的维护者,还是 GLFW 团队的成员?

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 hereis 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.

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

Edit

编辑

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.

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

采纳答案by user3728501

Step 1: Installing GLFW 3 on your system with CMAKE

第 1 步:使用 CMAKE 在您的系统上安装 GLFW 3

For this install, I was using KUbuntu 13.04, 64bit.

对于这次安装,我使用的是 KUbuntu 13.04,64 位。

The first step is to download the latest version (assuming versions in the future work in a similar way) from www.glfw.org, probably using this link.

第一步是从 www.glfw.org 下载最新版本(假设将来的版本以类似方式工作),可能使用此链接

The next step is to extract the archive, and open a terminal. cdinto the glfw-3.X.X directory and run cmake -G "Unix Makefiles"you may need elevated privileges, and you may also need to install build dependencies first. To do this, try sudo apt-get build-dep glfworsudo apt-get build-dep glfw3ordo it manually, as I did using sudo apt-get install cmake xorg-dev libglu1-mesa-dev... There may be other libs you require such as the pthread libraries... Apparently I had them already. (See the -l options given to the g++ linker stage, below.)

下一步是提取存档,并打开一个终端。cd进入 glfw-3.XX 目录并运行cmake -G "Unix Makefiles"您可能需要提升权限,您可能还需要先安装构建依赖项。要做到这一点,尝试sudo apt-get build-dep glfwsudo apt-get build-dep glfw3做手工,像我一样用sudo apt-get install cmake xorg-dev libglu1-mesa-dev......有可能是你需要其他库,如并行线程库......显然,我有他们了。(请参阅下面为 g++ 链接器阶段提供的 -l 选项。)

Now you can type makeand then make install, which will probably require you to sudofirst.

现在您可以输入make然后make install,这可能需要您sudo先输入。

Okay, you should get some verbose output on the last three CMake stages, telling you what has been built or where it has been placed. (In /usr/include, for example.)

好的,您应该在最后三个 CMake 阶段获得一些详细的输出,告诉您已构建的内容或放置的位置。(/usr/include例如,在 中。)

Step 2: Create a test program and compile

第二步:创建测试程序并编译

The next step is to fire up vim ("what?! vim?!" you say) or your preferred IDE / text editor... I didn't use vim, I used Kate, because I am on KUbuntu 13.04... Anyway, download or copy the test program from here(at the bottom of the page) and save, exit.

下一步是启动vim(“什么?!vim?!”你说)或你喜欢的IDE/文本编辑器......我没有使用vim,我使用了Kate,因为我在KUbuntu 13.04上......无论如何,从这里(在页面底部)下载或复制测试程序并保存,退出。

Now compile using g++ -std=c++11 -c main.cpp- not sure if c++11 is requiredbut I used nullptrso, I needed it... You may need to upgrade your gcc to version 4.7, or the upcoming version 4.8... Info on that here.

现在,编译使用g++ -std=c++11 -c main.cpp-不知道C ++ 11的要求,但我用nullptr的话,我需要它。你可能需要将GCC升级到4.7版本,或即将推出的4.8版...信息上这里

Then fix your errors if you typed the program by hand or tried to be "too clever" and something didn't work... Then link it using this monster! g++ main.o -o main.exec -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXiSo you see, in the "install build dependencies" part, you may also want to check you have the GL, GLU, X11 Xxf86vm (whatever that is) Xrandr posix-thread and Xi (whatever that is) developmentlibraries installed also. Maybe update your graphics drivers too, I think GLFW 3 may require OpenGL version 3 or higher? Perhaps someone can confirm that? You may also need to add the linker options -ldl -lXinerama -lXcursorto get it to work correctly if you are getting undefined references to dlclose(credit to @user2255242).

如果您手动输入程序或试图“太聪明”并且某些东西不起作用,然后修复您的错误......然后使用这个怪物链接它!g++ main.o -o main.exec -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi所以你看,在“安装构建依赖项”部分,你可能还想检查你是否还安装了 GL、GLU、X11 Xxf86vm(无论是什么)Xrandr posix-thread 和 Xi(无论是什么)开发库。也许也更新您的图形驱动程序,我认为 GLFW 3 可能需要 OpenGL 版本 3 或更高版本?也许有人可以证实这一点?-ldl -lXinerama -lXcursor如果您收到未定义的引用dlclose(归功于@user2255242),您可能还需要添加链接器选项以使其正常工作。

And, yes, I really did need that many -ls!

而且,是的,我确实需要那么多-ls!

Step 3: You are finished, have a nice day!

第 3 步:您已完成,祝您有美好的一天!

Hopefully this information was correct and everything worked for you, and you enjoyed writing the GLFW test program. Also hopefully this guide has helped, or will help, a few people in the future who were struggling as I was todayyesterday!

希望这些信息是正确的,一切都对您有用,并且您喜欢编写 GLFW 测试程序。也希望本指南能够帮助或将帮助未来一些像我昨天今天一样挣扎的人!

By the way, all the tags are the things I searched for on stackoverflow looking for an answer that didn't exist. (Until now.) Hopefully they are what you searched for if you were in a similar position to myself.

顺便说一句,所有标签都是我在 stackoverflow 上搜索的东西,寻找不存在的答案。(直到现在。)如果您与我处于类似的位置,希望它们是您搜索的内容。

回答by CastleDefender

Note that you do not need that many -ls if you install glfw with the BUILD_SHARED_LIBSoption. (You can enable this option by running ccmakefirst).

请注意,-l如果您使用该BUILD_SHARED_LIBS选项安装 glfw,则不需要那么多s 。(您可以通过ccmake先运行来启用此选项)。

This way sudo make installwill install the shared library in /usr/local/lib/libglfw.so. You can then compile the example file with a simple:

这种方式sudo make install将在/usr/local/lib/libglfw.so. 然后,您可以使用简单的命令编译示例文件:

g++ main.cpp -L /usr/local/lib/ -lglfw

Then do not forget to add /usr/local/lib/ to the search path for shared libraries before running your program. This can be done using:

然后不要忘记在运行程序之前将 /usr/local/lib/ 添加到共享库的搜索路径中。这可以使用:

export LD_LIBRARY_PATH=/usr/local/lib:${LD_LIBRARY_PATH}

And you can put that in your ~/.bashrcso you don't have to type it all the time.

你可以把它放在你的里面,~/.bashrc这样你就不必一直输入它。

回答by Oleh Pomazan

I solved it in this way

我是这样解决的

A pkg-config file describes all necessary compile-time and link-time flags and dependencies needed to use a library.

pkg-config 文件描述了使用库所需的所有必要的编译时和链接时标志和依赖项。

pkg-config --static --libs glfw3

shows me that

告诉我

-L/usr/local/lib -lglfw3 -lrt -lXrandr -lXinerama -lXi -lXcursor -lGL -lm -ldl -lXrender -ldrm -lXdamage -lX11-xcb -lxcb-glx -lxcb-dri2 -lxcb-dri3 -lxcb-present -lxcb-sync -lxshmfence -lXxf86vm -lXfixes -lXext -lX11 -lpthread -lxcb -lXau -lXdmcp  

I don't know if all these libs are actually necessary for compiling but for me it works...

我不知道所有这些库是否实际上都是编译所必需的,但对我来说它有效......

回答by user2941878

Great guide, thank you. Given most instructions here, it almostbuilt for me but I did have one remaining error.

很好的指导,谢谢。鉴于此处的大多数说明,它几乎是为我构建的,但我确实有一个剩余的错误。

/usr/bin/ld: //usr/local/lib/libglfw3.a(glx_context.c.o): undefined reference to symbol 'dlclose@@GLIBC_2.2.5'
//lib/x86_64-linux-gnu/libdl.so.2: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

After searching for this error, I had to add -ldlto the command line.

搜索此错误后,我不得不添加-ldl到命令行。

g++ main.cpp -lglfw3 -lX11 -lXrandr -lXinerama -lXi -lXxf86vm -lXcursor -lGL -lpthread -ldl

Then the "hello GLFW" sample app compiled and linked.

然后编译并链接“hello GLFW”示例应用程序。

I am pretty new to linux so I am not completely certain what exactly this extra library does... other than fix my linking error. I do see that cmd line switch in the post above, however.

我对 linux 还很陌生,所以我不完全确定这个额外的库到底做了什么……除了修复我的链接错误。但是,我确实在上面的帖子中看到了 cmd 行切换。

回答by Adrian

Since the accepted answer does not allow more edits, I'm going to summarize it with a single copy-paste command (Replace 3.2.1 with the latest version available in the first line):

由于接受的答案不允许更多编辑,我将用一个复制粘贴命令对其进行总结(用第一行中可用的最新版本替换 3.2.1):

version="3.2.1" && \
wget "https://github.com/glfw/glfw/releases/download/${version}/glfw-${version}.zip" && \
unzip glfw-${version}.zip && \
cd glfw-${version} && \
sudo apt-get install cmake xorg-dev libglu1-mesa-dev && \
sudo cmake -G "Unix Makefiles" && \
sudo make && \
sudo make install

If you want to compile a program use the following commands:

如果要编译程序,请使用以下命令:

g++ -std=c++11 -c main.cpp && \
g++ main.o -o main.exec -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXinerama -lXcursor

If you are following the learnopengl.com tutorial you may have to set up GLAD as well. In such case click on this link

如果您正在学习 learnopengl.com 教程,您可能还需要设置 GLAD。在这种情况下,请单击此链接

http://glad.dav1d.de/#profile=core&specification=gl&api=gl%3D3.3&api=gles1%3Dnone&api=gles2%3Dnone&api=glsc2%3Dnone&language=c&loader=on

http://glad.dav1d.de/#profile=core&specification=gl&api=gl%3D3.3&api=gles1%3Dnone&api=gles2%3Dnone&api=glsc2%3Dnone&language=c&loader=on

and then click on the "Generate" button at the bottom right corner of the website and download the zip file. Extract it and compile the sources with the following command:

然后点击网站右下角的“生成”按钮,下载zip文件。提取它并使用以下命令编译源代码:

g++ glad/src/glad.c -c -Iglad/include

Now, the commands to compile your program become like this:

现在,编译程序的命令变成这样:

g++ -std=c++11 -c main.cpp -Iglad/include && \
g++ main.o glad.o -o main.exec -lGL -lGLU -lglfw3 -lX11 -lXxf86vm -lXrandr -lpthread -lXi -ldl -lXinerama -lXcursor

回答by mathmaniage

If anyone's getting lazy and maybe perhaps doesn't know how to configure shell for all those libraries and -ls, then I created a python script(you have to have python3, most linux users have it.) that allows you to easily compile scripts and run them without worrying much, it just has regular system calls, just arranged neatly, I created it for my self but maybe it'd be useful: Here it is

如果有人变得懒惰并且可能不知道如何为所有这些库和-l配置 shell ,那么我创建了一个 python 脚本(你必须有 python3,大多数 linux 用户都有它。)它允许你轻松编译脚本并运行它们而不必担心,它只有常规系统调用,排列整齐,我为自己创建了它,但也许它会有用:这是

回答by Shayan Amani

The well-described answer is already there, but I went through this SHORTERrecipe:

详细描述的答案已经存在,但我经历了这个更短的食谱:

  1. Install Linuxbrew
  2. $ brew install glfw
  3. cd /home/linuxbrew/.linuxbrew/Cellar/glfw/X.X/include
  4. sudo cp -R GLFW /usr/include
  1. 安装Linuxbrew
  2. $ brew install glfw
  3. cd /home/linuxbrew/.linuxbrew/Cellar/glfw/X.X/include
  4. sudo cp -R GLFW /usr/include

Explanation: We manage to build GLFW by CMAKE which is done by Linuxbrew (Linux port of beloved Homebrew). Then copy the header files to where Linux reads from (/usr/include).

说明:我们设法通过 CMAKE 构建 GLFW,这是由 Linuxbrew(心爱的 Homebrew 的 Linux 端口)完成的。然后将头文件复制到 Linux 从 ( /usr/include)读取的位置。