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

In summary: The best you can do is spawn a command to open a window or to use ansi codes with cout to paint the window.
  • #1
Buffu
849
146
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
  • #2
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 cout to paint the window.
 
  • #3
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 cout to paint the window.

But people who made windowing libraries must have started from somewhere, right ?
In which language they programmed those libraries ? assembly ?
 
  • #4
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.
 
  • #5
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 cout.

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 ?
 
  • #6
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;
}
 
  • #7
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 Buffu
  • #13
Last edited:
  • Like
Likes 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 enviroment. 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 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 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 nsaspook

1. How do I open a window in C++ using g++ on Linux?

To open a window in C++ using g++ on Linux, you will need to use a library such as Xlib or Qt. These libraries provide functions and classes for creating and managing windows, as well as handling user input and graphics. You will need to include the appropriate header files and link the libraries when compiling your code.

2. Can I open multiple windows in C++ using g++ on Linux?

Yes, you can open multiple windows in C++ using g++ on Linux. You will need to create separate windows using the library's functions or classes, and then manage each window individually. Keep in mind that opening multiple windows may impact the performance of your program, so it is important to optimize and efficiently manage your code.

3. How do I handle user input in a window created with C++ and g++ on Linux?

To handle user input in a window created with C++ and g++ on Linux, you will need to use the library's functions or classes for detecting and responding to events such as mouse clicks and keyboard presses. These events will typically be handled in a loop while the window is open, allowing your program to react to user input in real-time.

4. Is there a way to create a graphical user interface (GUI) using C++ and g++ on Linux?

Yes, you can create a GUI using C++ and g++ on Linux. Libraries such as Qt and GTK+ provide tools for building GUI applications in C++, including windows, buttons, menus, and other graphical elements. These libraries also offer support for different themes and styles, allowing you to customize the appearance of your GUI.

5. Are there any alternatives to using libraries for opening windows in C++ with g++ on Linux?

While libraries are the most common and recommended way to open windows in C++ with g++ on Linux, there are other options available. For example, you could use a framework like SDL or SFML, which provide more advanced features and cross-platform compatibility. Alternatively, you could directly access the underlying operating system functions, but this approach may be more complex and less portable.

Similar threads

  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
15
Views
5K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
2
Replies
45
Views
3K
  • Programming and Computer Science
Replies
19
Views
3K
  • Programming and Computer Science
Replies
14
Views
1K
  • Programming and Computer Science
4
Replies
107
Views
5K
  • Programming and Computer Science
Replies
1
Views
351
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
9
Views
2K
Back
Top