Running example C code is a painful exercise unless it comes with a makefile.

I often find myself with a C file containing code that supposedly does something very cool, but for which a first basic attempt at compilation (gcc main.c) fails with—

main.c:(.text+0x1f): undefined reference to `XListInputDevices'
clang-3.7: error: linker command failed with exit code 1 (use -v to see invocation)

—or similar.

I know this means I'm missing the right linker flags, like -lX11, -lXext or -lpthread.

But which ones?

The way I currently deal with this is to find the library header that a function was included from, use Github's search to find some other program that imports that same header, open its makefile, find the linker flags, copy them onto my compilation command, and keep deleting flags until I find a minimal set that still compiles.

This is inefficient, boring, and makes me feel like there must be a better way.

  • Can you provide an example file? A reasonable way is to look up which package the header file belongs to. From there, it's a hop, skip and jump to finding the correct library flag. But to provide an illustration, an example file would help. – Faheem Mitha Apr 20 '16 at 15:48
  • @FaheemMitha I don't see how an example file helps the general question. – Anko Apr 20 '16 at 15:50
  • Well, would you like an illustration of a technique, or general remarks? Your choice. – Faheem Mitha Apr 20 '16 at 15:51
  • @FaheemMitha Whatever answers the question. Examples are trivial to construct: just pick any library function that requires a linker flag, like XListInputDevices used in the example compiler error here. – Anko Apr 20 '16 at 16:04
  • 1
    @FaheemMitha Unless I misunderstand what you mean, the header a function was defined in can easily be found by checking man, as mentioned. For XListInputDevices, the appropriate include-line is #include <X11/extensions/XInput.h>. I'm wondering how I can tell based on that what -lSomething flag I need. – Anko Apr 20 '16 at 16:12

The question is how to determine what linker flag to use from inspection of the source file. The example below will work for Debian. The header files are the relevant items to note here.

So, suppose one has a C source file containing the header

#include <X11/extensions/XInput.h>.

We can do a search for XInput.h using, say apt-file. If you know this header file is contained in an installed package, dpkg -S or dlocate will also work. E.g.

apt-file search XInput.h
libxi-dev: /usr/include/X11/extensions/XInput.h

That tells you that this header file belongs to the development package for libxi (for C libraries, the development packages (normally of the form libname-dev or libname-devel) contain the header files), and therefore you should use the -lxi linker flag.

Similar methods should work for any distribution with a package management system.


Here's a community wiki to collect other distros' equivalent tools for Faheem's method. Feel free to edit, but keep the sort alphabetical for searching.


Use pkgfile from the extra repository, passing the header file name as a parameter.


$ pkgfile XInput.h


(and anything Debian-based using dpkg)

apt-file search for the header file name, as covered.


As covered in another question by warl0ck, use the pfl package's e-file program, or alternatively, the web-based Portage File List search.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.