4

I'm trying to build an application developed in VS 2010 with VS2017. When I'm building the application I'm getting the following error:

error: : Macro definition of snprintf conflicts with Standard Library function declaration

I tried to solve this issue like as here. But It doesn't work in my case.

I'm using windows 10 with VS2017 community 15.8.2.

JosepB
  • 2,205
  • 4
  • 20
  • 40
  • 2
    Do you have a definition like this anywhere in your code: #define snprintf ? – Isma Sep 05 '18 at 07:30
  • 1
    The error tells you exactly what is wrong. Somewhere in your code you have a macro definition for snprintf. You need to find that definition and remove it or rename it. If you are using that definition anywhere in your code then you need to verify that the macro definition is compatible with the standard library function. If not then you need to rewrite the code as appropriate. Don't expect a magic bullet solution, you've got to understand the code you are working with and solve the problem in a way that works for your code. – john Sep 05 '18 at 07:36
  • Yes, you are right. Sorry for the inconvenience and thanks a lot! – JosepB Sep 05 '18 at 07:46
  • 1
    I'm so glad you asked this question. Thank you. – Francisco Zarabozo Aug 31 '20 at 13:23

2 Answers2

7

As the error in your question shows, you have a macro definition for snprintf that is no longer compatible with your current version.

So you need to look for the following:

#define snprintf _snprintf

You can either remove it or if you need to also compile your code with Visual Studio 2010 you can add the following condition:

#if _MSC_VER < 1700 
#define snprintf _snprintf
#endif
Isma
  • 14,604
  • 5
  • 37
  • 51
3

The error message is supposed to helpfully tell you what file has the offending #define. In my case, it was wrong. CMAKE was adding a header (my_config.h in my case) that didn't directly show up by tracing the error code. And doing a solution wide search for #define snprintf _snprintf yielded zero results.

How I found the offending #define:

  • Attempt to build.

  • Get error message: #error: Macro definition of snprintf conflicts with Standard Library function declaration (compiling source file D:\PathToFile\libmysql.c). libmysql.c has no such definition, nor do any of the headers it includes.

  • Double-click the error message. This opens stdio.h in a new tab, at the line where the error is generated (line 1914 in my case).

    1906 #if defined snprintf
    1907     // This definition of snprintf will generate "warning C4005: 'snprintf': macro
    1908     // redefinition" with a subsequent line indicating where the previous definition
    1909     // of snprintf was.  This makes it easier to find where snprintf was defined.
    1910     #pragma warning(push, 1)
    1911     #pragma warning(1: 4005) // macro redefinition
    1912     #define snprintf Do not define snprintf as a macro
    1913     #pragma warning(pop)
    1914     #error Macro definition of snprintf conflicts with Standard Library function declaration
    1915 #endif
    
  • Hover over the word snprintf where it checks for the define (line 1906 in my case). Intellisense will show you |>| #define snprintf _snprintf in a tooltip.

  • Right-click the word snprintf (not the tooltip) and click either Peek Definition or Go To Definition. This pops up the offending #define which you can now delete or modify as needed.

    493 #define ssize_t SSIZE_T
    494 #define strcasecmp _stricmp
    495 #define strncasecmp _strnicmp
    496 #define snprintf _snprintf // <-- Offending line.
    497 #define strtok_r strtok_s
    498 #define strtoll _strtoi64
    499 #define strtoull _strtoui64
    
  • In my case, the offending line was 496, which I simply deleted.

  • Go back to stdio.h and hover over the snprintf word and it should no longer give you a tooltip as it's not defined (mine also changed from purple to white).

MichaelS
  • 257
  • 2
  • 6