Multiple function definition #define
I have a logger.h file and a macro function definition for logging:
//logger.h:
#ifndef _LOGGER_H_
#define _LOGGER_H_
#ifdef LOG_DEBUG
ofstream debug_log("debug.log");
#define log(...) debug_log << __FILE__ << ":" << __PRETTY_FUNCTION__ << ":" << __LINE__ << "| " << __VA_ARGS__ << std::endl
#else
#define log(...)
#endif
#endif
This header file is included in several c. and using the log () function. g ++ gives:
/tmp/ccMAjYSm.o:(.bss+0x0): multiple definition of `debug_log'
/tmp/ccHj3w7u.o:(.bss+0x0): first defined here
/tmp/cc3LQ9GQ.o:(.bss+0x0): multiple definition of `debug_log'
/tmp/ccHj3w7u.o:(.bss+0x0): first defined here
Any hint?
+3
source to share
2 answers
If you have declared LOG_DEBUG
at the project level (or in multiple translation units) they will all see
ofstream debug_log("debug.log");
and you will have multiple definitions.
Possible solution: put it in a single translation unit, showing everyone else information about its existence
Heading
#ifndef _LOGGER_H_
#define _LOGGER_H_
#ifdef LOG_DEBUG
extern ofstream debug_log;
#define log(...) debug_log << __FILE__ << ":" << __PRETTY_FUNCTION__ << ":" << __LINE__ << "| " << __VA_ARGS__ << std::endl
#else
#define log(...)
#endif
#endif
Cpp file
ofstream debug_log("debug.log");
+3
source to share