How to open a window in just C++ (g++ linux) ?

  • Context: C/C++ 
  • Thread starter Thread starter Buffu
  • Start date Start date
  • Tags Tags
    C++ Linux Window
Click For Summary

Discussion Overview

The discussion centers around the challenge of opening a window using only C++ without relying on any external libraries, specifically in the context of using g++ on Ubuntu 16.04. Participants explore the feasibility of this task and the underlying mechanisms involved in window creation.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant expresses a desire to open a window using plain C++ without any libraries, questioning how library developers manage to do so.
  • Several participants argue that it is not possible to open a window purely with C/C++ as it requires a windowing library or system calls to interact with the operating system.
  • Another participant suggests that while libraries are necessary, the foundational code for these libraries must have originated from lower-level programming, possibly in assembly.
  • One participant mentions that using Xlib can open a window, but this contradicts the original request for a solution without libraries.
  • Discussion includes the idea that C++ does not inherently provide GUI capabilities and that any interaction with the operating system for GUI tasks must involve libraries.
  • Some participants highlight the complexity of creating a GUI, noting that it involves event handling and system interrupts, which cannot be managed without a library.
  • There is a mention of the syscall function as a low-level interface to the Linux kernel, but this still implies the need for some form of library support.
  • One participant acknowledges the confusion regarding the nature of C++ as a language versus the requirements of the operating system for GUI operations.

Areas of Agreement / Disagreement

Participants generally disagree on the feasibility of opening a window without using any libraries. While some acknowledge the necessity of libraries for such tasks, others maintain that understanding the foundational aspects of window creation is important.

Contextual Notes

Participants note that opening a window involves interactions with the operating system, which are typically managed through libraries. There is also a recognition that the requirements for GUI programming extend beyond basic language constructs.

Buffu
Messages
851
Reaction score
147
I want to open a window in just c++. Now I can do this easily with SDL with just 2-3 lines of code, but I want to learn how to open a window in c++ without any library, I mean like plain c++ nothing else. I searched this on google but results yield nothing of my interest.

There has to be a way. People who write libraries are not aliens, they know how to do it so there must be a method to do it.

I think it is important to mention that I am using Ubuntu 16.04 and g++.
 
Technology news on Phys.org
You can't with plain C/C++ you need a windowing library.

The best you can do is spawn a command to open a window or to use ansi codes with count to paint the window.
 
jedishrfu said:
You can't with plain C/C++ you need a windowing library.

The best you can do is spawn a command to open a window or to use ansi codes with count to paint the window.

But people who made windowing libraries must have started from somewhere, right ?
In which language they programmed those libraries ? assembly ?
 
Buffu said:
But people who made windowing libraries must have started from somewhere, right ?
In which language they programmed those libraries ? assembly ?
They programmed them in C for the most part but you said you wanted to open a window not write a whole windowing library.
 
phinds said:
They programmed them in C for the most part but you said you wanted to open a window not write a whole windowing library.
Yes I want to open a window like a file explorer window not write a whole library. I don't want to paint the terminal white with count.

I just want to learn how a library opens a window. Since C++ can be used to run C code, then that means I can use C++ to open a window.

Can you tell me how can I do that ?
 
This will open a X11 window but only a window.

gcc junk.c $(pkg-config --cflags --libs x11) -o junk
./junk

junk.c
C:
/* Simple x client without any error checking
 *
 * gcc -o xlib-1 -Wall -O3 xlib-1.c -L/usr/X11R6/lib/ -lX11
 *
 * Written by cel98csn@mds.mdh.se, April 1999
 */
#include <X11/Xlib.h>int main(void)
{
  Display *d; int s; Window w; XEvent ev;
  int should_quit = 0;

  d = XOpenDisplay(NULL);
  s = XDefaultScreen(d);

  w = XCreateSimpleWindow(d, XRootWindow(d, s), 0, 0,
                         200, 200, 0,
                         XBlackPixel(d, s),
                         XWhitePixel(d, s));

  XSelectInput(d, w, ButtonPressMask);
  XMapWindow(d, w);

  while(!should_quit)
   {
   XNextEvent(d, &ev);
   switch(ev.type)
       {
     case ButtonPress:
       should_quit = 1;
       break;
     default:
   break;
       }
   }

  return 0;
}
 
That uses Xlib. He doesn't want to use a library.
 
  • #10
Buffu said:
Is there something similar for Linux ?
I have no idea but assume there must be.
 
  • #11
jtbell said:
That uses Xlib. He doesn't want to use a library.

That's what he says but it's a dubious requirement because everything other than basic language constructs is a library of some sort in C(++).
 
  • #12
Buffu said:
But people who made windowing libraries must have started from somewhere, right ?
In which language they programmed those libraries ? assembly ?

It's impossible to open a window purely in C or C++ without calling a library because most resources on the computer (display, memory, hard disk, etc.) are ultimately managed by the operating system. So you can't normally just "open a window"; you instead (somehow) tell the operating system that you want a window drawn or (perhaps) want lower-level access to the display in some way that the OS allows. Normally there would be a low-level library supplied with your OS that let's you tell the OS these kinds of things that you can call from C code.

I don't know the details but I think the lowest-level interface that you can use from C to the Linux kernel is via the syscall function. The Linux kernel provides a set of low-level services (to do I/O, request memory, start or communicate with other processes, etc.) and there are special codes you can use in assembly language to invoke them. I think syscall is just a simple function (written at least partially in assembly language) that let's you supply these codes from C, and system libraries like POSIX are built on top of this.

I think X Windows uses the networking facilites to communicate with applications, so you could presumably open a window in Linux by opening a network connection to yourself (localhost or 127.0.0.1) on whatever port the X client listens to, and sending the X client whatever message is needed to tell it to open a window. But this would still require library support to access the OS's networking facilities.
 
Last edited:
  • Like
Likes   Reactions: Buffu
  • #13
Last edited:
  • Like
Likes   Reactions: Buffu
  • #14
I think you are very confused. C++ is not an environment, it's a language. That's it. It doesn't give a crap about your operating system, hardware... as far as it's concerned, you don't have a GUI, you don't have threads, hell, you don't even have a hard drive. If you want then, you have include boost, or stdio, or pthread...

Any interaction with an environment must be handled through the environment. What if you decide to write an entire program using the X windowing system, then load it up on a Linux district that doesn't even have X? Best thing to do is use a library that is agnostic to the system (wxwidgets, qt, glut...)

Qt would probably be easiest to learn, and will introduce you to event driven programming. Programming with windows that can have lots of events is very different than writing procedural code. Glut is more procedural, but its ancient and mostly only useful for OpenGL context areas, which is useless if you want to use native widgets like a file explorer (which would require a window, frame, icons, scroll bars, probably at least one layout device, and mouse handlers... all of which you have to set the event handlers for.). You said you don't want to handle paint events... then how do you expect the window to draw itself?

Windows do not require assembly, they require system interrupts.
 
  • #15
newjerseyrunner said:
I think you are very confused. C++ is not an environment, it's a language. That's it. It doesn't give a crap about your operating system, hardware... as far as it's concerned, you don't have a GUI, you don't have threads, hell, you don't even have a hard drive. If you want then, you have include boost, or stdio, or pthread...
No I know what C++ is. I do know C++ don't give a crap about my OS as far as it does not need to interact with my OS on low level.
I am pretty sure I need to know my OS to open a window because I think that stuff is very low level.
Ofcourse I could use a library that is cross platfrom but that is not the point of this thread, I wanted to know how to create a window from scratch. I think people who create these libraries must have to start from somewhere, they also would have written "main" function somewhere.
 
  • #16
newjerseyrunner said:
You said you don't want to handle paint events
Sorry if I may have said that unintentionally rather I want to do this.
 
  • #17
Buffu said:
No I know what C++ is. I do know C++ don't give a crap about my OS as far as it does not need to interact with my OS on low level.
I am pretty sure I need to know my OS to open a window because I think that stuff is very low level.
Ofcourse I could use a library that is cross platfrom but that is not the point of this thread, I wanted to know how to create a window from scratch. I think people who create these libraries must have to start from somewhere, they also would have written "main" function somewhere.
In an earlier post you said you did NOT want to write a whole windowing library. Make up your mind.
 
  • #18
phinds said:
In an earlier post you said you did NOT want to write a whole windowing library. Make up your mind.
Sorry. Yes I don't want to create a library because a library would have many things other than just opening a windowing like it would have functions to display images, function to draw primitive shapes etc ... . By windowing library I meant this kind of library.

If windowing library is something that only opens a window and nothing more/less, then yes I want to write a crude one.
If windowing library mean something like what described above, then no.
 
  • #19
What would be the point of opening a window that has nothing in it? What would be the point of opening a window that does DO anything and cannot do anything? It seems like you just want to draw a picture of a window. I don't get the point. It WOULD be a lot more simple than writing a windowing library that could actually DO something, but again, I just don't get the point. What am I missing?
 
  • #20
phinds said:
What would be the point of opening a window that has nothing in it? What would be the point of opening a window that does DO anything and cannot do anything? It seems like you just want to draw a picture of a window. I don't get the point. It WOULD be a lot more simple than writing a windowing library that could actually DO something, but again, I just don't get the point. What am I missing?

I thought I get to learn how people make windows, also I get to learn some low level C++ etc...
If a complete windowing library is easier than just a dumb window then I am fine with it. I thought a dumb window is easier to make.

Anyway I do need to create a dumb window first, right ?
 
  • #21
@Buffu: so,... on Unix/Linux variants,... do you understand that there's something called a "X server" daemon process which is in charge of on-screen display, and that application processes talk to the X server via interprocess messaging? (This is so applications can be either on the same machine as the X server + display, or on a remote machine.)

Applications typically talk to the X server with the help of a "Window Manager" which implements a particular style for the windows. (E.g., "Gnome", "KDE", etc.)

Since you mentioned Linux, I presume you already have a machine running Linux, and one of these common window managers or desktop environments? Which one is running on your machine?

Then, if (say) your machine is running KDE, try googling the phrase "writing C++ applications with kde". That turns up quite a few informative resources and tutorials.

See also the answers to this question about native gui api's on Linux.
 
Last edited:
  • Like
Likes   Reactions: Buffu
  • #22
Buffu said:
I thought I get to learn how people make windows, also I get to learn some low level C++ etc...
If a complete windowing library is easier than just a dumb window then I am fine with it. I thought a dumb window is easier to make.

Anyway I do need to create a dumb window first, right ?
Sure, if your final goal is to create a windowing library, then writing the code that creates a bare window is not a bad start, and maybe even just as a stand-alone learning experience it could be worthwhile. Certainly is is much easier than writing a useful windowing library. My point is more that if it's just a learning exercise then I think you time would be better spent learning things for which there are NOT massive libraries already available.
 
  • Like
Likes   Reactions: Buffu
  • #23
strangerep said:
@Buffu: so,... on Unix/Linux variants,... do you understand that there's something called a "X server" daemon process which is in charge of on-screen display, and that application processes talk to the X server via interprocess messaging? (This is so applications can be either on the same machine as the X server + display, or on a remote machine.)

Applications typically talk to the X server with the help of a "Window Manager" which implements a particular style for the windows. (E.g., "Gnome", "KDE", etc.)

Since you mentioned Linux, I presume you already have a machine running Linux, and one of these common window managers or desktop environments? Which one is running on your machine?

Then, if (say) your machine is running KDE, try googling the phrase "writing C++ applications with kde". That turns quite a few informative resources and tutorials.

See also the answers to this question about native gui api's on Linux.

I will look into it. Thanks.

phinds said:
Sure, if your final goal is to create a windowing library, then writing the code that creates a bare window is not a bad start, and maybe even just as a stand-alone learning experience it could be worthwhile. Certainly is is much easier than writing a useful windowing library. My point is more that if it's just a learning exercise then I think you time would be better spent learning things for which there are NOT massive libraries already available.

Yes you are right but just for the sake of knowledge I wanted to know how to make a window.
 
  • #24
Why do you think something as complicated as a window will have an easy way to just create one? Linux doesn't know what a window is, you have to tell it what it is. You have to tell it where the skin is, you have to tell it how to display that skin, you have to paint it. Then, you need something to manage those windows. How does the OS know if a mouse was clicked at position x that that overlaps with one of your windows? If you type the keyboard, which window is active? How do you stack windows inside of each other? How do you manage scrollbars? Hell, what's a scrollbar? Where's the event handler? Is it threaded? What do the events look like? All of that is in the window library.

On Microsoft Windows, that library is called kernel.lib. On Linux, it can be a variety of things depending on the distro and how the individual user has it set up.

If you REALLY want to make your own window, you'll need to use OpenGL to talk directly to the graphics card as well as manage all of those things that I mentioned.
 
  • #25
newjerseyrunner said:
Why do you think something as complicated as a window will have an easy way to just create one? Linux doesn't know what a window is, you have to tell it what it is. You have to tell it where the skin is, you have to tell it how to display that skin, you have to paint it. Then, you need something to manage those windows. How does the OS know if a mouse was clicked at position x that that overlaps with one of your windows? If you type the keyboard, which window is active? How do you stack windows inside of each other? How do you manage scrollbars? Hell, what's a scrollbar? Where's the event handler? Is it threaded? What do the events look like? All of that is in the window library.

On Microsoft Windows, that library is called kernel.lib. On Linux, it can be a variety of things depending on the distro and how the individual user has it set up.

If you REALLY want to make your own window, you'll need to use OpenGL to talk directly to the graphics card as well as manage all of those things that I mentioned.
I have abandoned the plan to make my own window for now, it is too difficult. I will pursue it later sometime.
 
  • #26
Buffu said:
I have abandoned the plan to make my own window for now, it is too difficult. I will pursue it later sometime.
Smart move.
 
  • Like
Likes   Reactions: nsaspook

Similar threads

Replies
13
Views
4K
  • · Replies 15 ·
Replies
15
Views
8K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 14 ·
Replies
14
Views
7K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 14 ·
Replies
14
Views
2K
Replies
19
Views
4K
  • · Replies 45 ·
2
Replies
45
Views
6K