【问题标题】:How to link an X11 program如何链接 X11 程序
【发布时间】: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 (或其他任何名称)。我得看看该怎么办。

标签: c linker x11


【解决方案1】:

试试:

gcc $(pkg-config x11 --cflags) findXfonts.c -o findXfonts $(pkg-config x11 --libs)

然后阅读 pgk-config 的手册页:

人 pkg 配置

【讨论】:

    【解决方案2】:

    这是错误的

    pkg-config 成语扩展为简单的 -lX11

    事实上,如果你尝试

    echo $(pkg-config x11)
    

    你一无所获。而是

    echo $(pkg-config x11  --cflags --libs)
    

    输出(在我的系统上)

    -lX11
    

    这就是你想要的,你只需要在你的系统上正确设置一切,以便编译和开发 X11 代码。

    所以,在 $(...) 中添加 --cflags --libs 就足够了。

    【讨论】:

    • 应该,但不是,由于其他问题。请参阅对先前答案的评论。
    • @4dummies LD_LIBRARY_PATH 也可以是“void”,因为有一些默认的库系统路径,例如/usr/lib 和其他:LD_LIBRARY_PATH 它也是我系统上的“”,我可以在其中无缝地编译它。然后,猜测是:您的 x11 库位于非标准位置(/opt/lib?)。试试找!或者,ldd executable,其中 Executable 是肯定链接到 x11 的程序(比如 xfontsel);然后尝试LD_LIBRARY_PATH=/path/to/x11 gcc ...。这只是一个快速的测试,没有我确定的解决方案。我觉得很奇怪 pkg-config 不会给你所有正确的需要的标志。
    【解决方案3】:

    有三个问题。

    首先,我将 --libs 切换到 pkg-config。 (在这种情况下, --cflags 开关没有任何作用)。我也把它放在命令行的错误位置。

    其次,链接器找不到 X11 库。我必须告诉它要查看哪个目录。有些人为此使用 LD_LIBRARY_PATH,但由于我同时拥有 64 位和 32 位库,因此我不想要单一方法。

    最后,我做了一个Makefile,最终得到了

    gcc -Wall -ansi -g -m64 -c findXfonts.c
    gcc findXfonts.o -m64 -L/usr/lib/x86_64-linux-gnu -lX11 -o findXfonts
    

    最后,一旦编译和链接,我发现“模式”字符串的末尾有一个额外的连字符(“-”)。我删除了它,得到了一个包含 1400 多种字体的列表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-31
      • 1970-01-01
      • 2019-01-20
      • 2011-11-10
      • 2011-04-13
      • 2020-04-03
      • 1970-01-01
      相关资源
      最近更新 更多