Multiple Function Definition

I defined a function to display a message when debug flags are disabled in the header file, as shown below:

#ifdef  NDEBUG

#define debug_msg(expr, msg)        (static_cast<void>(0))

#else /* Not NDEBUG.  */

#ifndef SHOW_DEBUG_H_
#define SHOW_DEBUG_H_

#include <stdio.h>
void _show_in_debug(const char *_file, unsigned int _line,
        const char *_function, const char *_msg)
{
    printf("%s\t%d\t%s\t%s\n", _file, _line, _function, _msg);
    fflush(NULL);
}

#endif

#define debug_msg(expr, msg)                \
  ((expr)                               \
   ? _show_in_debug(__FILE__, __LINE__, __func__, msg)  \
   : static_cast<void>(0))

#endif

when I include the header more than in the file, I get the following error:

several definitions of `_show_in_debug (char const *, unsigned int, charconst *, char const *) '

I don’t know exactly what I'm doing wrong here, any help?

+4
source share
3 answers

Even with defenders turned on, you get a definition _show_in_debug in every compilation unit. Linking these units then leads to a multiple-definition error.

static, :

static void _show_in_debug(const char *_file, unsigned int _line,
        const char *_function, const char *_msg)
{
    printf("%s\t%d\t%s\t%s\n", _file, _line, _function, _msg);
    fflush(NULL);
}
+3

.c, . .

(.. , ), .c(.. .c), , .

, :

/* ... as you quoted ... */
    void _show_in_debug(const char *_file, unsigned int _line,
        const char *_function, const char *_msg);
/* ... rest of what you quoted ... */

, :

#incude <stdio.h>
#include "Yourheader.h"

#ifdef  NDEBUG
void _show_in_debug(const char *_file, unsigned int _line,
        const char *_function, const char *_msg)
{
    printf("%s\t%d\t%s\t%s\n", _file, _line, _function, _msg);
    fflush(NULL);
}

#endif
+3

, , , . ? , _show_in_debug() , . , , .

See http://www.cprogramming.com/declare_vs_define.html for more details (especially, see the "General Cases" section).

+2
source

Source: https://habr.com/ru/post/1680247/


All Articles