Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link an X11 program

Tags:

c

linker

x11

I've got my first X11 program compiled, but cannot link it. I'm on 64-bit Xubuntu 13.10, and I use the command line gcc $(pkg-config x11) findXfonts.c -o findXfonts

It compiles okay, but every X* symbol I use shows up as undefined in the linker step. The pkg-config idiom expands to simply -lX11

/*
 * Copyright 2014 Kevin O'Gorman <[email protected]>.
 * 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;
}
like image 352
4dummies Avatar asked Mar 22 '26 17:03

4dummies


2 Answers

It is false that

The pkg-config idiom expands to simply -lX11

in fact if you try

echo $(pkg-config x11)

you obtain nothing. Instead

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

outputs (on my system)

-lX11

which is what you want and all you need being everything correctly set up on your system in order to compile and develop X11 codes.

So, it should be enough you add --cflags --libs inside your $(...).

like image 105
ShinTakezou Avatar answered Mar 24 '26 05:03

ShinTakezou


Try:

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

For more information, pkg-config has a manual page:

man pkgconf
like image 32
Nikos C. Avatar answered Mar 24 '26 06:03

Nikos C.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!