如何在 Linux 中使用 C++ 创建窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11190925/
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
How do you create a window in Linux with C++?
提问by SteveDeFacto
I was expecting a Linux API similar to the Windows API. All I see on Google is references to Qt and GTK. I really don't need anything more than a simple window to draw on with OpenGL, so these libraries appear bloated for my use. What do Qt and GTK use to create windows under Linux? Is there nothing more low-level?
我期待一个类似于 Windows API 的 Linux API。我在 Google 上看到的只是对 Qt 和 GTK 的引用。我真的只需要一个简单的窗口来使用 OpenGL 进行绘制,因此这些库对于我的使用来说显得有些臃肿。Linux下Qt和GTK用什么创建windows?没有更底层的东西了吗?
采纳答案by Martin Beckett
The X window system generally does the drawing - you then use a toolkit such as Qt or GTK on top of raw Xlib to provide event loops, drag and drop, starting apps on mouseclicks and all the other 'desktop' stuff
X 窗口系统通常进行绘图——然后您在原始 Xlib 之上使用 Qt 或 GTK 等工具包来提供事件循环、拖放、鼠标点击启动应用程序以及所有其他“桌面”内容
It's fairly easy to work directly with Xlib and openglor if you just want to learn opengl the glutprovides the framework you need to display a window, handle mouse/keyboard events and so on.
直接使用Xlib 和 opengl相当容易,或者如果您只想学习 opengl,glut提供了显示窗口、处理鼠标/键盘事件等所需的框架。
回答by C2H5OH
For OpenGL, the easiest way to do it is by using GLUTor SDL. Here's an approximate example using GLUT:
对于 OpenGL,最简单的方法是使用GLUT或SDL。这是一个使用 GLUT 的近似示例:
#include <GL/glut.h>
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("My new window");
/* ... */
}
You really want to avoid using Xlib directly as it's extremely tedious to use. Furthermore, GLUT and SDL make it easier to port your OpenGL application to different platforms.
你真的想避免直接使用 Xlib,因为它使用起来非常乏味。此外,GLUT 和 SDL 可以更轻松地将 OpenGL 应用程序移植到不同的平台。
回答by smocking
Ax Martin said, X11 (or its fork XOrg these days) is the windowing system, but you can actually write X11 applications (i.e. clients) without using a toolkit, just using the X libraries. See herefor documentation.
Ax Martin 说,X11(或现在的 XOrg 分支)是窗口系统,但实际上您可以在不使用工具包的情况下编写 X11 应用程序(即客户端),只需使用 X 库即可。有关文档,请参见 此处。
It is generally not the best idea to do so, as it is rather painful and will involve a lot of code for relatively simple applications to work as you expect them to.
这样做通常不是最好的主意,因为它相当痛苦,并且会涉及大量代码,使相对简单的应用程序按照您的预期工作。
回答by tomer zeitune
Updated answer for 2019. Unix like systems normally uses the X window system. You can work with it directly using Xlib this is the low level API. But you likely need a more welcoming and cross-platform solution. You can use:
更新了 2019 年的答案。类 Unix 系统通常使用 X 窗口系统。您可以使用 Xlib 直接使用它,这是低级 API。但您可能需要一个更受欢迎的跨平台解决方案。您可以使用:
- OpenGL Utility Toolkit- GLUT
- Simple and Fast Multimedia Library- SFML
- Simple DirectMedia Layer- SDL
- Graphics Library Framework- GLFW (my recommendation)
- OpenGL 实用工具包- GLUT
- 简单快速的多媒体库- SFML
- 简单直接媒体层- SDL
- 图形库框架- GLFW(我的推荐)
GLFW is written in C and has native support for Windows, macOS and many Unix-like systems using the X Window System, such as Linux and FreeBSD.
GLFW 是用 C 编写的,并且对 Windows、macOS 和许多使用 X Window 系统的类 Unix 系统(例如 Linux 和 FreeBSD)具有本机支持。
Once installed, create a window with :
安装后,创建一个窗口:
#include <GLFW/glfw3.h>
.
. //Entry and glfwInit()
.
GLFWwindow* window = glfwCreateWindow(1000, 1000, "MyWindow", NULL, NULL);
glfwMakeContextCurrent(window);