how to print something in the header file?

Nov 13, 2011 at 8:55pm
hi i'm using codeblocks on XP to write a simple program that checks when the header files get called. So i want to print a simple statement such as "enters blah" at the beginning of a header, and this is what i wrote:

1
2
3
4
5
6
7
8
9
#ifndef DRIVER_H
................

void enterDriver_H(){
    cout<<"enters Driver.h"<<endl;
}

...........
#endif 


and the complier tells me that theres multiple definition of `enterDriver_H(), can someone tell me what to do, please? thx
Nov 14, 2011 at 2:51am
Well, since you have your header file resumed here, I can only say that you are missing #define DRIVER_H, but even so, header files are not meant to have full function definitions, only function declarations. It should look like this:

Driver.h:
1
2
3
4
5
#ifndef DRIVER_H
#define DRIVER_H

extern void enterDriver_H();
#endif // DRIVER_H 


And you should have a Driver.cpp file with the actual function definition:

Driver.cpp:
1
2
3
4
5
6
#include "Driver.h"

void enterDriver_H()
{
    cout << enters Driver.h" << endl;
} 


But I'm not sure this is what you want either. I think you want to see a message while you compile, and your message as posted will be shown in runtime and only if the function enterDriver_H() is called at some point.

So I guess you want this:

http://msdn.microsoft.com/en-us/library/x7dkzch2.aspx


Driver.h:
1
2
3
4
5
6
7
#ifndef DRIVER_H
#define DRIVER_H

#pragma message("Including header file" __FILE__ ".")

extern void enterDriver_H();
#endif // DRIVER_H 


I just don't know if your compiler understands #pragma message().
Nov 15, 2011 at 4:54am
thx for the reply, but I did include all the proper "def" parts. so you are saying that I cannot print anything from the header files at runtime, since it can only have function declarations? I also don't think th "extern" was necessary in front....

btw I tried the #pragma method and it did work, i could see the note in the build log.
Nov 15, 2011 at 12:02pm
You have to make a function and check every single "include" file if it's included or not.
And probably you DO need the "extern" keyword.
Topic archived. No new replies allowed.