2

I'm wondering if there is an existing tool that lists the external dependencies for a set of C++ source files (e.g. one directory in a project with many directories). Something similar in concept to finding the external symbols of a binary, but it would across a set of source files.

A sample use case would be for working with an entire code base for purposes of understanding dependencies within it. For example, the developer could ask the question what other parts of the code base does the code in src/module1 depend on?

For example, if this tool was run on the following car.cc file it would list Vehicle and std::cout. If it were run on both source files together it would just list std::cout (since Vehicle is now defined within the source file set).

// vehicle.h
class Vehicle {
};
// car.cc
#include "vehicle.h"

class Car: public Vehicle {
  public:
    void honk() {
      cout << "Honk! \n" ;
    }
};

Optimally the tool would be available on Linux and support filtering out some references (like std::*).

studgeek
  • 14,272
  • 6
  • 84
  • 96
  • The compiler obviously identifies all identifiers, but AFAIK few compilers have an option to output such a list directly However, if the compiler is used to create an object file, `objdump` can be used to get a list of identifiers (referred to as symbols) from each object file. Those identifiers may be mangled though. The linker can also produce a map file, which can list the identifiers used. – Peter Nov 04 '19 at 05:16
  • 2
    https://tio.run/##bY1Bi8IwFITv@RVDF8RFUvdsQy/iec8e0/TRjWxeSvICwq6/vRoR8eA7PJiZb5jB5p9lcVZgTIt/TM5Bn91mAw39naFvMmIrYd7aNhbBaoU4nMYSZmjZvyTqw7P7LSPB@JglkQ29KtnzBLaB8mwdIcvYKeVZEKzn9af6w@1UfdXkEgZK3V27umYMmgMLJViuBE2UdmgehGf0/fvSMRZQLdKIpjoP6jmXSEpifHXqotpluQI – jxh Nov 04 '19 at 07:18

1 Answers1

3

Yes, there is. It's called nm and lists all the symbols exported/required by the given compiled object file. To see all the symbols that the object requires, you can run

nm <object-file> | grep ' U '

where the grep command filters out only the undefined symbols. Take a look at the man page to see what the other type characters mean.

Note, however, that C++ uses name-mangling: A function like

int foo(int a);

may be represented by a symbol like

_Z3fooi

to encode the type(s) of its argument(s) into the symbol name. This is important to make function overloading work, but it makes symbol names virtually impossible to read.

cmaster - reinstate monica
  • 38,891
  • 9
  • 62
  • 106
  • `nm -C` to unmangle, as per [this answer](https://stackoverflow.com/a/6036088/633093). – Hami Aug 09 '23 at 09:57