解决未定义的引用 xcb

resolving undefined references xcb

我可以包括 xcb/xcb.h 中的项目,但不能包括 /usr/include/xcb/randr.h 中概述的项目。

我更喜欢使用 C++,但为了帮助调试,我也尝试了 C,它产生了相同错误的变体。

我确定我做错了什么,但我不确定从哪里着手解决这个问题。非常感谢您的阅读,有什么建议吗?

例子

main.cpp

#include <xcb/xcb.h>
#include <xcb/randr.h>

int main()
{
    const xcb_setup_t                       * xsetup;
    xcb_connection_t                        * conn;
    xcb_screen_t                            * screen;
    xcb_window_t                              root_win;
    xcb_screen_iterator_t                     screen_iterator;
    xcb_randr_get_screen_resources_cookie_t   resources;

    // connect to Xserver
    conn   = xcb_connect(NULL, NULL);
    xsetup = xcb_get_setup(conn);

    // get the root window
    screen_iterator = xcb_setup_roots_iterator(xsetup);
    screen          = screen_iterator.data;
    root_win        = screen->root;

    // any function from xcb/randr.h fails with undefined reference.
    resources = xcb_randr_get_screen_resources(conn, root_win);
}

编译

# gcc tries
gcc -Wall  main.cpp -o main `pkg-config --cflags --libs xcb`
g++ -Wall  main.cpp -o main `pkg-config --cflags --libs xcb`

# clang tries
clang++    main.cpp -o main `pkg-config --cflags --libs xcb`
clang      main.cpp -o main `pkg-config --cflags --libs xcb`

结果

gcc

/usr/bin/ld: /tmp/ccWR2GQL.o: in function `main':
main.cpp:(.text+0x6c): undefined reference to `xcb_randr_get_screen_resources'
collect2: error: ld returned 1 exit status

clang

/usr/bin/ld: /tmp/main-d114b5.o: in function `main':
main.cpp:(.text+0x67): undefined reference to `xcb_randr_get_screen_resources'
clang-7: error: linker command failed with exit code 1 (use -v to see invocation)

xcb 库分为几个不同的包;所以你需要同时引入 xcbxcb-randr 库,明确地:

... `pkg-config --cflags --libs xcb xcb-randr`

您的 Linux 发行版可能单独打包了 randr 库。检查Fedora,它将xcb和xcb-rand都打包在libxcb-devel子包中;但是您的 Linux 发行版可能有一个您需要安装的单独的 libxcb-randr-devel 子包。

非常感谢n.m.G.M.

我没有链接 xcb-randr .

解决方案:

clang++ main.cpp -o main `pkg-config --cflags --libs xcb` -lxcb-randr