Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

Tags:
  1. Mar 16, 2017 #1
    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++.
     
  2. jcsd
  3. Mar 16, 2017 #2

    jedishrfu

    Staff: Mentor

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

    phinds

    User Avatar
    Gold Member
    2016 Award

    They programmed them in C for the most part but you said you wanted to open a window not write a whole windowing library.
     
  6. Mar 16, 2017 #5
    Yes I want to open a window like a file explorer window not write a whole library. I dont 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 ?
     
  7. Mar 16, 2017 #6

    nsaspook

    User Avatar
    Science Advisor

    This will open a X11 window but only a window.

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

    junk.c
    Code (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;
    }
     
     
  8. Mar 16, 2017 #7

    jtbell

    User Avatar

    Staff: Mentor

    That uses Xlib. He doesn't want to use a library.
     
  9. Mar 16, 2017 #8

    phinds

    User Avatar
    Gold Member
    2016 Award

  10. Mar 16, 2017 #9
  11. Mar 16, 2017 #10

    phinds

    User Avatar
    Gold Member
    2016 Award

    I have no idea but assume there must be.
     
  12. Mar 16, 2017 #11

    nsaspook

    User Avatar
    Science Advisor

    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(++).
     
  13. Mar 16, 2017 #12

    wle

    User Avatar

    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 lets 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 lets 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: Mar 16, 2017
  14. Mar 16, 2017 #13
    Last edited: Mar 17, 2017
  15. Mar 17, 2017 #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.
     
  16. Mar 20, 2017 #15
    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.
     
  17. Mar 20, 2017 #16
    Sorry if I may have said that unintentionally rather I want to do this.
     
  18. Mar 20, 2017 #17

    phinds

    User Avatar
    Gold Member
    2016 Award

    In an earlier post you said you did NOT want to write a whole windowing library. Make up your mind.
     
  19. Mar 20, 2017 #18
    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.
     
  20. Mar 20, 2017 #19

    phinds

    User Avatar
    Gold Member
    2016 Award

    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?
     
  21. Mar 20, 2017 #20
    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 ?
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted