user3488563
user3488563

Reputation: 35

Find the library that a specific function is defined in

Somebody sent me a piece of C++ code, and it includes some headers and functions that I'm unfamiliar with. I want to find which header file defined a particular function. For example, I have these headers:

#include <iostream>
#include <ncurses.h>
#include <stdlib.h>
#include <stdio.h>
#include <pthread.h>
#include <time.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>

and I want to know witch of them defined this function:

tcgetattr

or which of them defined this struct:

termios

Can anyone help me?

Upvotes: 1

Views: 246

Answers (2)

paxdiablo
paxdiablo

Reputation: 882806

The tcgetattr is a "get attribute" function from termios.h. The termios structure is also from that header as I would have thought was self-evident by virtue of the fact it shares the name.

See, for example, the Linux man page here.

If you're looking as to which headers contain arbitrary functions, I usually (in UNIX-like operating systems) just grep in known locations, something like:

grep tcgetattr /usr/include/*.h /usr/include/*/*.h

(or using the recursive option of grep, or find to search everywhere).

Upvotes: 1

R Sahu
R Sahu

Reputation: 206747

A general method to find out where a function is declared, if you don't know where it's supposed to be:

  • Create a C++ source file that contains just those lines. Let's call it test.cc.

  • Compile with file with the -E option.

    g++ -E -o test.out test.cc
    
  • Look for the function you want to track in test.out. From there, you can track down which .h file it came from.

Upvotes: 0

Related Questions