来自 "Basic Graphics Programming With The XCB Library" 的代码中的“'string' undeclared”

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

这是导致问题的代码:

#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 生成错误,因为 WM_NAME 和 WM_ICON_NAME 未在任何地方定义; XCB_ATOM_ 应该放在前面。我通过在线阅读论坛帖子和阅读 xproto.h

找到了解决方案

但是,似乎没有在任何地方定义 STRING。我搜索了 string.h。发现 STRING 的唯一情况是在评论中。我尝试将 STRING 更改为字符串,但它仍然无法编译。

$ 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

我通过将 "XCB_ATOM_" 放在 "STRING" 之前解决了这个问题,它编译得很好。

正如克利福德所建议的,这里有一个单独的post固定代码。

#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;
}