简体   繁体   中英

How do you create a window in Linux with C++?

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?

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

It's fairly easy to work directly with Xlib and opengl or if you just want to learn opengl the glut provides the framework you need to display a window, handle mouse/keyboard events and so on.

For OpenGL, the easiest way to do it is by using GLUT or SDL . Here's an approximate example using 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.

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:

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.

Once installed, create a window with:

#include <GLFW/glfw3.h>
.
. //Entry and glfwInit()
.
GLFWwindow* window = glfwCreateWindow(1000, 1000, "MyWindow", NULL, NULL);
glfwMakeContextCurrent(window);

Ax Martin said, X11 (or its fork XOrg these days) is the windowing system, but you can actually write X11 applications (ie clients) without using a toolkit, just using the X libraries. See here for documentation.

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.

I know this is an old post. But for people that have found this recently like me here is a useful diagram. It is just a matter of how far down do you want/need to go in abstraction; if you arent careful you'll end up trying to code in binary. XLib is a dependency for SFML and XLib has it's dependencies.

Diagram of GUI Layers

Assuming by simple window you mean the drawing should appear on the screen: The Weston compositor uses EGLOutput/EGLDevice to display the composited Weston desktop or individual Wayland applications on a physical display device.

For drawing with other manufacturer's hardware studying GEM may give hints.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM