Find out if a function is called within a C++ project?

后端 未结 8 545
失恋的感觉
失恋的感觉 2021-02-01 19:15

I\'m trying to remove functions that are not used from a C++ project. Over time it\'s become bloated and I\'m looking to remove functions that aren\'t used at all.

I ha

相关标签:
8条回答
  • 2021-02-01 20:13

    I suppose the easiest way is to remove a function (or class, variable, anything else you might think is unneeded) and then see if it compiles. If the function is used you will get a compile or link error at some point during the rebuild.

    Generally you should remove the definition rather than the declaration, as otherwise with things like overloaded functions and specialised templates it may compile and link to one of the others, not causing an error but changing the programs behaviour. By removing the definition, the compiler still sees the declaration, but the linker will fail to link it.

    Items that a declared but not defined wont cause an error if their not used, since the linker will never try to link to them.

    0 讨论(0)
  • 2021-02-01 20:21

    Visual Studio can generate call graphs, showing 'called-by' for each function. Doxygen will do the same if you don't want to use Visual Studio.

    However both these methods will fail to detect a function called through a pointer, but that should normally be easy to check manually.

    0 讨论(0)
提交回复
热议问题