"‘string’ undeclared" in code from "Basic Graphics Programming With The XCB Library"

198 views Asked by At

Here was the code that caused the problem:

#include <string.h>
#include <xcb/xcb.h>
#include <xcb/xcb_atom.h>

int main ()
{
  xcb_connection_t *c;
  xcb_screen_t     *screen;
  xcb_window_t      win;
  char             *title = "Hello World !";
  char             *title_icon = "Hello World ! (iconified)";



  /* Open the connection to the X server */
  c = xcb_connect (NULL, NULL);

  /* Get the first screen */
  screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;

  /* Ask for our window's Id */
  win = xcb_generate_id (c);

  /* Create the window */
  xcb_create_window (c,                             /* Connection          */
                     0,                             /* depth               */
                     win,                           /* window Id           */
                     screen->root,                  /* parent window       */
                     0, 0,                          /* x, y                */
                     250, 150,                      /* width, height       */
                     10,                            /* border_width        */
                     XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class               */
                     screen->root_visual,           /* visual              */
                     0, NULL);                      /* masks, not used     */

  /* Set the title of the window */
  xcb_change_property (c, XCB_PROP_MODE_REPLACE, win,
                       WM_NAME, STRING, 8,
                       strlen (title), title);

  /* fixed it by replacing WM_NAME with XCB_ATOM_WM_NAME
  and replacing STRING with XCB_ATOM_STRING */

  /* Set the title of the window icon */
  xcb_change_property (c, XCB_PROP_MODE_REPLACE, win,
                       WM_ICON_NAME, STRING, 8,
                       strlen(title_icon), title_icon);

  /* fixed this by replacing WM_ICON_NAME with XCB_ATOM_ICON_NAME
  and replacing STRING with XCB_ATOM_STRING */

  /* Map the window on the screen */
  xcb_map_window (c, win);

  xcb_flush (c);

  while (1) {}

  return 0;
}

gcc generated an error because WM_NAME and WM_ICON_NAME weren't defined anywhere; XCB_ATOM_ should have been prepended. I found that solution from reading forum posts online, and from reading xproto.h

However, STRING doesn't seem to be defined anywhere. I searched string.h. The only cases in which STRING was found were in comments. I tried changing STRING to string, but it still wouldn't compile.

$ gcc -Wall -o win-icon-name win-icon-name.c -lxcb
win-icon-name.c: In function ‘main’:
win-icon-name.c:40:42: error: ‘string’ undeclared (first use in this function)
                        XCB_ATOM_WM_NAME, string, 8,
                                          ^
win-icon-name.c:40:42: note: each undeclared identifier is reported only once for each function it appears in

I solved the problem by putting "XCB_ATOM_" before "STRING", and it compiled just fine.

1

There are 1 answers

0
s0s On BEST ANSWER

As Clifford suggested, here is a separate post with the fixed code.

#include <string.h>
#include <xcb/xcb.h>
#include <xcb/xcb_atom.h>

int main ()
{
  xcb_connection_t *c;
  xcb_screen_t     *screen;
  xcb_window_t      win;
  char             *title = "Hello World !";
  char             *title_icon = "Hello World ! (iconified)";



  /* Open the connection to the X server */
  c = xcb_connect (NULL, NULL);

  /* Get the first screen */
  screen = xcb_setup_roots_iterator (xcb_get_setup (c)).data;

  /* Ask for our window's Id */
  win = xcb_generate_id (c);

  /* Create the window */
  xcb_create_window (c,                             /* Connection          */
                     0,                             /* depth               */
                     win,                           /* window Id           */
                     screen->root,                  /* parent window       */
                     0, 0,                          /* x, y                */
                     250, 150,                      /* width, height       */
                     10,                            /* border_width        */
                     XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class               */
                     screen->root_visual,           /* visual              */
                     0, NULL);                      /* masks, not used     */

  /* Set the title of the window */
  xcb_change_property (c, XCB_PROP_MODE_REPLACE, win,
                       XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8,
                       strlen (title), title);

  /* Set the title of the window icon */
  xcb_change_property (c, XCB_PROP_MODE_REPLACE, win,
                       XCB_ATOM_WM_ICON_NAME, XCB_ATOM_STRING, 8,
                       strlen(title_icon), title_icon);

  /* Map the window on the screen */
  xcb_map_window (c, win);

  xcb_flush (c);

  while (1) {}

  return 0;
}