LunaticJape
LunaticJape

Reputation: 1584

How to know which included header a function comes from?

I have a big class A that has dozens included headers, each header has its own included header as well.

I'm creating a new class that would use a function which is also used in class A. I do not want to include the whole class A in my new class, so I try to find the header who brought that function to class A.

What's the best way to do it?

Upvotes: 0

Views: 66

Answers (1)

n. m. could be an AI
n. m. could be an AI

Reputation: 119877

If you are not using an IDE or an appropriate editor plugin (you should), then the easiest way is to add a deliberate error to a file and look at the error message. Note, this may or may not work with your compiler.

int foo(); // defined somewhere but we don't know where

// ask the compiler
foo(42);

Error messages:

test.cpp:42:8: error: too many arguments to function ‘int foo()’
   42 |     foo(42);
      |     ~~~^~~~
foo.h:38:5: note: declared here
   38 | int foo();
      |      ^~~

You should not blindly #include <foo.h> if it comes from a third party library. It might be a file that end users are not supposed to include directly. Double check.

Upvotes: 3

Related Questions