Gtk-WARNING **: cannot open display:

  • Post author:
  • Post last modified:May 27, 2023
  • Reading time:2 mins read

While running a GTK+ based C program under Cygwin, the following runtime error is observed,

Gtk-WARNING **: cannot open display:

SOLUTION

  1. Make sure that Cygwin/XServer is installed and is running. If it is not installed, download the relevant packages from Cygwin.

  2. Make sure that environment variable DISPLAY is defined in the shell's environment. If DISPLAY is not defined, it can be defined by giving the following commands from shell,

    DISPLAY=":0" 
    export DISPLAY 
    

    You can, alternatively, put the following code in the .bashrc file,

    if [[ -z "$DISPLAY" ]] 
    then
        DISPLAY=":0" 
        export DISPLAY 
    fi
    

  3. Better still, put the code for setting the environment variable, DISPLAY, in the source code. This can be done by putting the following lines at the beginning of the main function of the C program.

    #include <stdlib.h> 
    // introduce the environment variable 
    // DISPLAY, if not present  
    if (setenv ("DISPLAY", ":0", 0) == -1)   
        error ("setenv");  
    

    This will obviate the requirement of setting the environment variable and is particularly helpful if you wish to run the program directly under Windows.

SEE ALSO

C Programming Tutorials

Share

Karunesh Johri

Software developer, working with C and Linux.