【发布时间】:2014-01-30 21:00:55
【问题描述】:
我已经编译了我的第一个 X11 程序,但无法链接它。我在 64 位 Xubuntu 13.10 上,我使用命令行 gcc $(pkg-config x11) findXfonts.c -o findXfonts
它可以编译,但是我使用的每个 X* 符号在链接器步骤中都显示为未定义。 pkg-config 习惯用法简单地扩展为 -lX11
/*
* Copyright 2014 Kevin O'Gorman <kogorman@gmail.com>.
* Distributed under the GNU General Public License.
*
* This is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <unistd.h>
#include <X11/Xlib.h>
#include <stdio.h>
int
main(int argc, char *argv[])
{
char **fontlist;
XFontStruct *returned_info;
char *pattern="-*-*-*-*-*-*-*-*-*-*-*-*-*-*-";
int nFonts;
char *displayName;
Display *display;
FILE *ostream = stdout;
int i, j, k;
displayName = getenv("DISPLAY"); /* expect ":0.0", but YMMV */
display = XOpenDisplay(displayName);
fontlist = XListFontsWithInfo(display, pattern, 10000, &nFonts, &returned_info);
for (i = 0; i < nFonts; i++) {
fprintf(ostream, "\n%s\n", fontlist[i]);
fprintf(ostream, " first: %u/%u, last: %u/%u\n",
returned_info[i].min_byte1, returned_info[i].min_char_or_byte2,
returned_info[i].max_byte1, returned_info[i].max_char_or_byte2);
for (j = 0; j < returned_info[i].n_properties; j++) {
fprintf(ostream, " %s: %ld\n",
XGetAtomName(display, returned_info[i].properties[j].name),
returned_info[i].properties[j].card32);
}
}
XFreeFontInfo(fontlist, returned_info, nFonts);
return EXIT_SUCCESS;
}
【问题讨论】:
-
您实际上是否在 X .so 文件中进行链接?仅仅因为您包含 X 标头对链接器来说意义不大。
-
gcc $(pkg-config x11 --cflags --libs) source.c -o whatever应该可以正常工作:您只是错过了从pkg-config指定您想要什么样的“信息” -
当。我知道这一点,并认为我已经做到了。幸运的是,我在回答讽刺之前检查过。但是,当我修复它时,我收到链接器错误“/usr/bin/ld: 找不到 -lX11”。这看起来很奇怪,但我也注意到在我的环境中绝对没有 LD_LIBRARY_PATH (或其他任何名称)。我得看看该怎么办。