在 C++、Windows 7 中包含 OpenGL 库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12376225/
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-27 16:13:11  来源:igfitidea点击:

Include OpenGL Libraries in C++, Windows 7

c++windowsopenglcompilationc-preprocessor

提问by imkendal

I am reading through a "Getting Started with OpenGL" tutorial and I came across this text:

我正在阅读“OpenGL 入门”教程,并看到了以下文字:

"If you are using C/C++, then you must first set up a build environment (Visual Studio project, GNU makefile, CMake file, etc) that can link to OpenGL. Under Windows, you need to statically link to a library called OpenGL32.lib (note that you still link to OpenGL32.lib if you're building a 64-bit executable. The "32" part is meaningless). Visual Studio, and most Windows compilers, come with this library."

“如果你使用的是C/C++,那么你首先要设置一个可以链接到OpenGL的构建环境(Visual Studio项目、GNU makefile、CMake文件等)。在Windows下,你需要静态链接到一个叫做OpenGL32的库.lib(请注意,如果您正在构建 64 位可执行文件,您仍会链接到 OpenGL32.lib。“32”部分没有意义。Visual Studio 和大多数 Windows 编译器都带有此库。”

I am just trying to write my source files using vim, I don't want to use an IDE like VS, and from my understanding the OpenGL libraries come with Windows 7 (correct me if I'm wrong). After this point the article doesn't really go into any more detail about how to include the OpenGL libraries, and my real question is, how do I include and use the OpenGL libraries in my source files?

我只是想使用 vim 编写我的源文件,我不想使用像 VS 这样的 IDE,并且根据我的理解,Windows 7 附带了 OpenGL 库(如果我错了,请纠正我)。在这一点之后,文章并没有真正详细介绍如何包含 OpenGL 库,我真正的问题是,如何在我的源文件中包含和使用 OpenGL 库?

Is it as simple as writing #include <name of lib>or do I need to do something else with my Programming environment like editing my path variables?

它像编写一样简单#include <name of lib>还是我需要在我的编程环境中做一些其他的事情,比如编辑我的路径变量?

Edit: I'm using the MinGW g++/gcc compiler

编辑:我正在使用 MinGW g++/gcc 编译器

采纳答案by Lars Pensj?

To get access to the include files, you need a special support library. There are more than one of these, but I would recommend GLEW. You can find the GLEW interface at http://glew.sourceforge.net/.

要访问包含文件,您需要一个特殊的支持库。其中不止一种,但我会推荐 GLEW。您可以在http://glew.sourceforge.net/找到 GLEW 界面。

The reason for this is that only a very old version of OpenGL headers are available as default on Windows. The newer interface is available indirectly; you have to ask for function addresses. This is however done by GLEW for you.

这样做的原因是 Windows 上只有非常旧版本的 OpenGL 标头可用作默认值。较新的界面可间接使用;你必须要求函数地址。然而,这是由 GLEW 为您完成的。

So you only have to include < GL/glew.h>, and do some initialization with glewInit();

所以你只需要包含<GL/glew.h>,并用glewInit()做一些初始化;

This is compatible with both Linux and Windows, especially as you use MinGW. When linking, I use the following:

这与 Linux 和 Windows 兼容,尤其是当您使用 MinGW 时。链接时,我使用以下内容:

MY_LIBS = -lglew32 -lopengl32 -lWs2_32 -lole32 -lcomctl32 -lgdi32 -lcomdlg32 -luuid

MY_LIBS = -lglew32 -lopengl32 -lWs2_32 -lole32 -lcomctl32 -lgdi32 -lcomdlg32 -luuid

I can recommend the use of a generic makefile, see http://sourceforge.net/projects/gcmakefile/

我可以推荐使用通用的 makefile,请参阅http://sourceforge.net/projects/gcmakefile/

Notice that you also have to setup a contextfor OpenGL, before initializing GLEW. This is done differently depending on the environment. It is done when you open a window. For portable libraries, I can recommend the glfwlibrary or freeglut.

请注意,在初始化 GLEW 之前,您还必须为 OpenGL设置一个上下文。这取决于环境,以不同的方式完成。当你打开一个窗口时它就完成了。对于便携式库,我可以推荐glfw库或freeglut