简体   繁体   中英

How can I find out the references to a C++ symbol

I am working on an existing big C++ code base (more than 1 million line of code). I need to remove some part of the code deemed not useful. However, when I just exclude that part of code from the build process (ie not to compile them), eventually I got "undefined references" error in linking for some symbols (class function names) I removed.

A problem rose when I tried to find out where in other code have the references. Using Cscope or OpenGrok, I can find out a few explicit references but does not really help after removing such references. There are lots of other cases indirectly referring to the symbol I removed, for example:

virtual functions overridden in child class
"typedef" defined other symbol to refer to this missing symbol.

My question is: is there any gcc/g++ option I can turn on to have a output of all references (that gcc/g++ is aware of) direct or indirect to the symbol I removed?

If no such gcc/g++ option, is there any other tool that can produce such output?

Thanks.

Removing the compilation units (c or cpp files) from your project does not completely remove them. Those are typically just the definitions of functions and classes. The declarations of those functions and classes still exist in headers which are likely still being included in other compilation units.

Track down where these things are declared (typically in header files) and either comment them out in the headers or stop including the headers entirely if you don't need anything within them for your project.

For example:

If you are removing foo.c from a project, make sure any instance of #include "foo.h" has been removed from all other c/cpp files

You can instruct LD to emit a linker map containing a cross reference table using the flags -Map=path/to/my_mapfile.map and --cref . More info here:

https://sourceware.org/binutils/docs/ld/Options.html

The map file is very long and terse, but it usually has enough information to help you pinpoint exactly why a given symbol is still being referenced.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM