How can I find out what linker flags are needed to use a given C library function?

Anko

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.

Faheem Mitha

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I find out what linker flags are needed to use a given C library function?

From Dev

How to find out what type to use for OpenCV .at function in C++?

From Dev

How can I find out the size of the group a given element exists in?

From Dev

How can I find out the version of given / installed Boost headers?

From Dev

How can I find out what jQuery functions I use in my scripts?

From Dev

How can I find out which library is including libcmt?

From Dev

How can I find out what calls the push-mark function in emacs?

From Dev

How can I use the ls command to find out a folder's owner and group, and what rights they each have?

From Dev

How can I find out what type of c# project I'm using in Visual Studio 2017?

From Dev

How can I find out what distro I am running?

From Dev

How can I find out what plugins I installed for Nautilus?

From Dev

How can I find out what version of cgroups I have?

From Dev

How to find out what function I am calling

From Dev

How can i use if needed the FileInfo[] or if needed the string[]?

From Dev

Can I find out what Perl IO Layer(s) a given filehandle has?

From Dev

How can I find out what this ffmpeg error code means?

From Dev

How can I find what is sending out a confirm?

From Dev

How can I find out what type the template<typename type> is?

From Dev

How can I find out what .desktop file is being launched?

From Dev

How can I find out what .desktop file is being launched?

From Dev

How can I find out what kind of file this is?

From Dev

How can I find out what motherboard is in my computer?

From Dev

How can I find out what options sudo is configured with?

From Dev

How can I find what is sending out a confirm?

From Dev

How can I find out what part of the GMSPolyline was tapped?

From Dev

How can I find out what plugin is missing in nmcli?

From Dev

How can I find out what package that a python module belongs to?

From Dev

How can I find out what my default gateway should be?

From Dev

How can I find out what's wrong with my RAM?

Related Related

  1. 1

    How can I find out what linker flags are needed to use a given C library function?

  2. 2

    How to find out what type to use for OpenCV .at function in C++?

  3. 3

    How can I find out the size of the group a given element exists in?

  4. 4

    How can I find out the version of given / installed Boost headers?

  5. 5

    How can I find out what jQuery functions I use in my scripts?

  6. 6

    How can I find out which library is including libcmt?

  7. 7

    How can I find out what calls the push-mark function in emacs?

  8. 8

    How can I use the ls command to find out a folder's owner and group, and what rights they each have?

  9. 9

    How can I find out what type of c# project I'm using in Visual Studio 2017?

  10. 10

    How can I find out what distro I am running?

  11. 11

    How can I find out what plugins I installed for Nautilus?

  12. 12

    How can I find out what version of cgroups I have?

  13. 13

    How to find out what function I am calling

  14. 14

    How can i use if needed the FileInfo[] or if needed the string[]?

  15. 15

    Can I find out what Perl IO Layer(s) a given filehandle has?

  16. 16

    How can I find out what this ffmpeg error code means?

  17. 17

    How can I find what is sending out a confirm?

  18. 18

    How can I find out what type the template<typename type> is?

  19. 19

    How can I find out what .desktop file is being launched?

  20. 20

    How can I find out what .desktop file is being launched?

  21. 21

    How can I find out what kind of file this is?

  22. 22

    How can I find out what motherboard is in my computer?

  23. 23

    How can I find out what options sudo is configured with?

  24. 24

    How can I find what is sending out a confirm?

  25. 25

    How can I find out what part of the GMSPolyline was tapped?

  26. 26

    How can I find out what plugin is missing in nmcli?

  27. 27

    How can I find out what package that a python module belongs to?

  28. 28

    How can I find out what my default gateway should be?

  29. 29

    How can I find out what's wrong with my RAM?

HotTag

Archive