TheChemist
TheChemist

Reputation: 357

Is there a way to print the filename of the file that included the current file?

Sorry for the confusing title, but I'm not sure of a better way to say it concisely.

Let me break it down with an example to make it clearer.

  1. We have 2 files, Foo.h and Bar.h
  2. Inside of Foo.h, we have #include <Bar.h>
  3. Inside of Bar.h, I would like to be able to have something like the following code:

    #pragma message ("Included from " __INCLUDER_FILENAME__)

  4. At compile time, the message "Included from Foo.h" would appear in the log.

Does anyone know if this is possible and, if so, how to do it?

Upvotes: 2

Views: 649

Answers (2)

Robert Andrzejuk
Robert Andrzejuk

Reputation: 5222

GCC:

Has a __BASE_FILE__ definition

Visual Studio:

Go to Project configuration -> C/C++ / Preprocessor. In Preprocessor definitions add:

__BASE_FILE__="%(Filename)%(Extension)"

Upvotes: 0

Jesper Juhl
Jesper Juhl

Reputation: 31467

There is no standard way to do that. Compiler (or rather preprocessor) extensions may exist, but that would be tool-chain specific.

Upvotes: 3

Related Questions