【问题标题】:GCC gives undefined reference when using pkg-config to link with the XCB libraryGCC 在使用 pkg-config 链接 XCB 库时给出未定义的参考
【发布时间】:2021-02-04 01:30:02
【问题描述】:

我正在学习来自here 的 X11 编程教程。该页面提供了 2 个可用于编译基于 XCB 的程序的命令:

gcc -Wall prog.c -o prog `pkg-config --cflags --libs xcb`

gcc -Wall prog.c -lxcb

现在,我都试过了。第一个说gcc: error: unrecognized command-line option ‘--cflags’。显然这是一个与 shell 相关的问题(正如我所见 here)。所以我尝试了bash。这给出了一个不同的错误:

/usr/bin/ld: /tmp/ccnURTF3.o: in function `useXlib':
example.c:(.text+0xd6): undefined reference to `XInternAtom'
/usr/bin/ld: /tmp/ccnURTF3.o: in function `useXlibProperly':
example.c:(.text+0x163): undefined reference to `XInternAtoms'
/usr/bin/ld: /tmp/ccnURTF3.o: in function `main':
example.c:(.text+0x4b1): undefined reference to `XOpenDisplay'
/usr/bin/ld: example.c:(.text+0x559): undefined reference to `XCloseDisplay'
collect2: error: ld returned 1 exit status

这与我使用gcc -Wall prog.c -lxcb 得到的相同。所以我猜bash 解决了这个问题,但有 2 个。在 Atom 中,当你将鼠标悬停在一个函数上时,它会显示它来自哪个标题。但是在这个我什么都没得到。

提前致谢

【问题讨论】:

  • pkg-config --cflags --libs xcb 的结果是什么?
  • @KamilCuk 对此很抱歉
  • @Gerhardh 我以前试过。它说-lxcb。我认为它的意思是添加一个 -lxcb 标志而不是整个东西。但这也行不通。顺便说一句,这是一个错字

标签: c gcc x11 ld xcb


【解决方案1】:

看起来这些年库被拆分并且函数声明被移动到不同的库。现在xcb 取决于X11 或类似的东西,或者pkg-config --libs xcb 应该输出-lX11,不知道。无论如何,以下工作:

gcc -Wall 1.c -lxcb -lX11

或者以下也可以:

gcc -Wall 1.c $(pkg-config --cflags --libs xcb) $(pkg-config --cflags --libs x11)    

如果能 ping 那个介绍的维护者,让他知道他应该更新页面,那就太好了。

【讨论】:

  • 谢谢。这正是我正在寻找的。我会让他知道的。
猜你喜欢
  • 2020-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-14
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
  • 2011-03-31
相关资源
最近更新 更多