当我编译代码时,我有这个错误不能在c++中打开文件X11/Xlib.h

When i compile acode i have this error cannot open file X11/Xlib.h in c++

本文关键字:c++ 文件 X11 Xlib 不能 代码 编译 错误      更新时间:2023-10-16
/* Sequential Mandelbrot program mandelbrot.c */
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xos.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#define           X_RESN      800       /* x resolution */
#define           Y_RESN      800       /* y resolution */
typedef struct complextype
{ float real, imag;
} Compl;
int main (int argc, char *argv[])
{  Window     win;                 /* window initialization */
   unsigned 
   int        width, height,       /* window size */
              x, y,                /* window position */
              border_width,        /* border width in pixels */
              display_width, 
              display_height,      /* size of screen */
              screen;              /* which screen */
   char      *window_name = "Mandelbrot Set", *display_name = NULL;
   GC         gc;
   unsigned   long valuemask = 0;
   XGCValues  values;
   Display   *display;
   XSizeHints size_hints;
   Pixmap     bitmap;
   XPoint     points[800];
   FILE          *fp, *fopen ();
   char           str[100];
   XSetWindowAttributes attr[1];
/* Mandlebrot variables */
   int i, j, k;
   Compl    z, c;
   float    lengthsq, temp;
/* connect to Xserver */
   if (  (display = XOpenDisplay (display_name)) == NULL ) 
   {  fprintf (stderr, "drawon: cannot connect to X server %sn",
                        XDisplayName (display_name) );
      exit (-1);
   }
/* get screen size */
   screen = DefaultScreen (display);
   display_width = DisplayWidth (display, screen);
   display_height = DisplayHeight (display, screen);
/* set window size */
   width = X_RESN;
   height = Y_RESN;
/* set window position */
   x = 0;
   y = 0;
/* create opaque window */
   border_width = 4;
   win = XCreateSimpleWindow(display, RootWindow (display, screen),
        x, y, width, height, border_width, 
        BlackPixel (display, screen), WhitePixel (display, screen));
   size_hints.flags = USPosition|USSize;
   size_hints.x = x;
   size_hints.y = y;
   size_hints.width = width;
   size_hints.height = height;
   size_hints.min_width = 300;
   size_hints.min_height = 300;
   XSetNormalHints (display, win, &size_hints);
   XStoreName(display, win, window_name);
/* create graphics context */
   gc = XCreateGC (display, win, valuemask, &values);
   XSetBackground (display, gc, WhitePixel (display, screen));
   XSetForeground (display, gc, BlackPixel (display, screen));
   XSetLineAttributes (display,gc,1,LineSolid,CapRound,JoinRound);
   attr[0].backing_store = Always;
   attr[0].backing_planes = 1;
   attr[0].backing_pixel = BlackPixel(display, screen);
   XChangeWindowAttributes(display, win, 
        CWBackingStore | CWBackingPlanes | CWBackingPixel, attr);
   XMapWindow (display, win);
   XSync(display, 0);
/* Calculate and draw points */
   for(i=0; i < X_RESN; i++) 
   {  for(j=0; j < Y_RESN; j++) 
      {  z.real = z.imag = 0.0; /* 800x800 scale factors */
         c.real = ((float) j - 400.0)/200.0;  
         c.imag = ((float) i - 400.0)/200.0;
         k = 0;
         do
         {                      /* iterate for pixel color */
             temp = z.real*z.real - z.imag*z.imag + c.real;
             z.imag = 2.0*z.real*z.imag + c.imag;
             z.real = temp;
             lengthsq = z.real*z.real+z.imag*z.imag;
             k++;
         } while (lengthsq < 4.0 && k < 100);
         if (k == 100) XDrawPoint (display, win, gc, j, i);
   }  }
   XFlush (display);
   sleep (30);
/* Program Finished */
}

打开命令提示符,运行以下两个命令:

find / -type f - name Xlib.h
find / -type d - name X11

这将帮助您定位该文件和/或目录,以便您可以查看它是否可用。

如果可用(例如,它在/usr/include/X_stuff/X11/Xlib.h中找到),确保编译器命令引用它,如:

g++ -I/usr/include/X_stuff ...

如果是没有,安装它。

或者您忘记安装X开发包,或者包含头文件的目录没有添加到includedir列表中。