c++ - multiple definition of #define function -


i have logger.h file , defining macro function 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 included in multiple c files. , using log() function. g++ giving:

/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 clue?

if declared log_debug @ project-level (or in multiple translation units), see

ofstream debug_log("debug.log"); 

line , you'll have multiple definitions.

a possible solution: put single translation unit while rendering others aware of existence

header

#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 

a cpp file

ofstream debug_log("debug.log"); 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -