How to use std::cout without a main in a file

Hi,

I would like to know how i could use std::cout in a file that does not contains a class and which is a part of a project that contain already a main () in another file.

I am trying something in the file :

1
2
3
4
5
6
//FileWithoutAMain.hpp

#include <iostream>
#define LOG(x) (std::cout << (x) << std::endl)

LOG("This is file is used in the .exe");


Thanks in advance for your help,

MoonDragon
Huh? What's that supposed to be for? When did you intend this to be printed? If it's at compile time, I think most compilers supply pragmas for that (although you don't wanna do that, output should be reserved to errors and warnings).
You can use std::cout in any file as long as you call it from a function.
If you want some code to be executed, you can hack this behaviour using some kind of weird initialization.
But I'm not so sure what you are really trying to achieve there...
I have a library (developed by someone else) that contains a lots of files (themselves containing namespaces) and i would like to have a kind of flag that will indicate me when this file/namespaces are used.
Define "used".
For headers, you can detect which are being included using the guard macros
eg:
1
2
3
4
5
// some header file that may be included by other headers
#ifndef GUARD_IDENTIFIER
#define GUARD_IDENTIFIER
// code
#endif 

Somewhere in your code
1
2
3
#ifdef GUARD_IDENTIFIER
// the file abov has been included (either directly or indirectly)
#endif 

Notice that header guards may change in different version of that library.

For source files, "used" may mean "linked to" and this is something you have full control if you have the library in source form.
I am not sure to understand your solution

// some header file that may be included by other headers
#ifndef GUARD_IDENTIFIER
#define GUARD_IDENTIFIER
// code
#endif



In my code do you mean?

#ifdef GUARD_IDENTIFIER
// the file abov has been included (either directly or indirectly)
std::cout << "the file abov has been included (either directly or indirectly)" ;

#endif


If you place that in a function is OK. But what do you need that information for?
That is known at compile time, while cout is for program output.
You could put that code in your main and it would work... somewhat.
You could put that code in your main and it would work... somewhat.


Definitively, this what i am planning to doo.

Thanks.
Topic archived. No new replies allowed.