解决未定义的参考XCB

resolving undefined references xcb

本文关键字:XCB 参考 未定义 解决      更新时间:2023-10-16

i可以包括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,它在libxcb-devel子包中包装XCB和XCB-Rand;但是您的Linux发行版可能需要安装单独的libxcb-randr-devel子弹。

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

我没有链接xcb-randr

解决方案:

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