MSYS2/MinGW with GCC 10.1: scope issues for snprintf, may need explicit cstdio include
There have been several issues compiling C++ projects in GCC 10.1 under MSYS2/MinGW recently, usually missing an explicit include of cstd... headers. This project seems to be one of them:
[8/82] Building CXX object third_party/glslang/glslang/CMakeFiles/glslang.dir/MachineIndependent/Scan.cpp.o
FAILED: third_party/glslang/glslang/CMakeFiles/glslang.dir/MachineIndependent/Scan.cpp.o
E:\MABS\msys64\mingw32\bin\ccache.exe g++ -DENABLE_HLSL -DENABLE_OPT=1 -DGLSLANG_OSINCLUDE_WIN32 -I../third_party/glslang/glslang/.. -mthreads -mtune=generic -O2 -pipe -Wimplicit-fallthrough -O3 -DNDEBUG -Wextra-semi -w -Wall -Wmaybe-uninitialized -Wuninitialized -Wunused -Wunused-local-typedefs -Wunused-parameter -Wunused-value -Wunused-variable -Wunused-but-set-parameter -Wunused-but-set-variable -fno-exceptions -Wno-reorder -fno-rtti -Werror=deprecated-copy -std=c++11 -MD -MT third_party/glslang/glslang/CMakeFiles/glslang.dir/MachineIndependent/Scan.cpp.o -MF third_party\glslang\glslang\CMakeFiles\glslang.dir\MachineIndependent\Scan.cpp.o.d -o third_party/glslang/glslang/CMakeFiles/glslang.dir/MachineIndependent/Scan.cpp.o -c ../third_party/glslang/glslang/MachineIndependent/Scan.cpp
In file included from E:/MABS/msys64/mingw32/i686-w64-mingw32/include/stdio.h:1555,
from E:/MABS/msys64/mingw32/i686-w64-mingw32/include/locale.h:12,
from E:/MABS/msys64/mingw32/include/c++/10.1.0/clocale:42,
from E:/MABS/msys64/mingw32/include/c++/10.1.0/i686-w64-mingw32/bits/c++locale.h:41,
from E:/MABS/msys64/mingw32/include/c++/10.1.0/bits/localefwd.h:40,
from E:/MABS/msys64/mingw32/include/c++/10.1.0/string:43,
from E:/MABS/build/shaderc-git/third_party/glslang/glslang/Include/Common.h:103,
from ../third_party/glslang/glslang/MachineIndependent/../Include/Types.h:43,
from ../third_party/glslang/glslang/MachineIndependent/Scan.cpp:48:
E:/MABS/msys64/mingw32/i686-w64-mingw32/include/sec_api/stdio_s.h:493:23: error: conflicting declaration of 'int sprintf_s(char*, size_t, const char*, ...)' with 'C' linkage
493 | _SECIMP int __cdecl sprintf_s(char *_DstBuf,size_t _DstSize,const char *_Format,...);
| ^~~~~~~~~
In file included from ../third_party/glslang/glslang/MachineIndependent/../Include/Types.h:43,
from ../third_party/glslang/glslang/MachineIndependent/Scan.cpp:48:
E:/MABS/build/shaderc-git/third_party/glslang/glslang/Include/Common.h:56:22: note: previous declaration with 'C++' linkage
56 | #define snprintf sprintf_s
| ^~~~~~~~~
In file included from E:/MABS/msys64/mingw32/include/c++/10.1.0/ext/string_conversions.h:43,
from E:/MABS/msys64/mingw32/include/c++/10.1.0/bits/basic_string.h:6535,
from E:/MABS/msys64/mingw32/include/c++/10.1.0/string:55,
from E:/MABS/build/shaderc-git/third_party/glslang/glslang/Include/Common.h:103,
from ../third_party/glslang/glslang/MachineIndependent/../Include/Types.h:43,
from ../third_party/glslang/glslang/MachineIndependent/Scan.cpp:48:
E:/MABS/msys64/mingw32/include/c++/10.1.0/cstdio:175:11: error: 'snprintf' has not been declared in '::'
175 | using ::snprintf;
| ^~~~~~~~
E:/MABS/msys64/mingw32/include/c++/10.1.0/cstdio:185:22: error: 'snprintf' has not been declared in '__gnu_cxx'
185 | using ::__gnu_cxx::snprintf;
| ^~~~~~~~
In file included from ../third_party/glslang/glslang/MachineIndependent/SymbolTable.h:70,
from ../third_party/glslang/glslang/MachineIndependent/Scan.cpp:49:
../third_party/glslang/glslang/MachineIndependent/../Include/InfoSink.h: In member function 'glslang::TInfoSinkBase& glslang::TInfoSinkBase::operator<<(float)':
../third_party/glslang/glslang/MachineIndependent/../Include/InfoSink.h:78:58: error: 'snprintf' was not declared in this scope
78 | snprintf(buf, size, (fabs(n) > 1e-8 && fabs(n) < 1e8) || n == 0.0f ? "%f" : "%g", n);
| ^~~~~~~~
../third_party/glslang/glslang/MachineIndependent/../Include/InfoSink.h:40:1: note: 'snprintf' is defined in header '<cstdio>'; did you forget to '#include <cstdio>'?
39 | #include <cmath>
+++ |+#include <cstdio>
40 |
In file included from ../third_party/glslang/glslang/MachineIndependent/SymbolTable.h:70,
from ../third_party/glslang/glslang/MachineIndependent/Scan.cpp:49:
../third_party/glslang/glslang/MachineIndependent/../Include/InfoSink.h: In member function 'void glslang::TInfoSinkBase::location(const glslang::TSourceLoc&)':
../third_party/glslang/glslang/MachineIndependent/../Include/InfoSink.h:100:9: error: 'snprintf' was not declared in this scope
100 | snprintf(locText, maxSize, ":%d", loc.line);
| ^~~~~~~~
../third_party/glslang/glslang/MachineIndependent/../Include/InfoSink.h:100:9: note: 'snprintf' is defined in header '<cstdio>'; did you forget to '#include <cstdio>'?
In file included from ../third_party/glslang/glslang/MachineIndependent/Scan.cpp:49:
../third_party/glslang/glslang/MachineIndependent/SymbolTable.h: In member function 'bool glslang::TSymbolTableLevel::insert(glslang::TSymbol&, bool)':
../third_party/glslang/glslang/MachineIndependent/SymbolTable.h:414:13: error: 'snprintf' was not declared in this scope
414 | snprintf(buf, 20, "%s%d", AnonymousPrefix, symbol.getAsVariable()->getAnonId());
| ^~~~~~~~
In file included from ../third_party/glslang/glslang/MachineIndependent/Scan.cpp:49:
../third_party/glslang/glslang/MachineIndependent/SymbolTable.h:71:1: note: 'snprintf' is defined in header '<cstdio>'; did you forget to '#include <cstdio>'?
70 | #include "../Include/InfoSink.h"
+++ |+#include <cstdio>
71 |
In file included from ../third_party/glslang/glslang/MachineIndependent/Scan.cpp:57:
../third_party/glslang/glslang/MachineIndependent/preprocessor/PpContext.h: In member function 'int glslang::TPpContext::TokenStream::Token::get(glslang::TPpToken&)':
../third_party/glslang/glslang/MachineIndependent/preprocessor/PpContext.h:265:17: error: 'snprintf' was not declared in this scope
265 | snprintf(ppToken.name, sizeof(ppToken.name), "%s", name.c_str());
| ^~~~~~~~
In file included from ../third_party/glslang/glslang/MachineIndependent/Scan.cpp:57:
../third_party/glslang/glslang/MachineIndependent/preprocessor/PpContext.h:88:1: note: 'snprintf' is defined in header '<cstdio>'; did you forget to '#include <cstdio>'?
87 | #include "PpTokens.h"
+++ |+#include <cstdio>
88 |
ninja: build stopped: subcommand failed.
logs.zip collected by media-autobuild suite
This is the actual error (quoted from your log above)
E:/MABS/msys64/mingw32/i686-w64-mingw32/include/sec_api/stdio_s.h:493:23: error: conflicting declaration of 'int sprintf_s(char*, size_t, const char*, ...)' with 'C' linkage 493 | _SECIMP int __cdecl sprintf_s(char *_DstBuf,size_t _DstSize,const char *_Format,...); | ^~~~~~~~~ In file included from ../third_party/glslang/glslang/MachineIndependent/../Include/Types.h:43, from ../third_party/glslang/glslang/MachineIndependent/Scan.cpp:48: E:/MABS/build/shaderc-git/third_party/glslang/glslang/Include/Common.h:56:22: note: previous declaration with 'C++' linkage 56 | #define snprintf sprintf_s
This is the line that causes the error https://github.com/KhronosGroup/glslang/blob/24dcbd1b1f68ff268bfa22dc4b72bec73d52abe7/glslang/Include/Common.h#L53
If you removed the check for MINGW_HAS_SECURE_API in that header, the build will succeed.
Suggestion by @1480c1:
One fix I can see is to move all of their preprocessing to after including the headers so that their defines will only affect the files that include
Common.hinstead of affecting the included system files too.