有一个.h文件,之前实在Linux下用GCC编译的,现在想把它 到Windows使用MSVC编译,其中有这么一行
void Log(Log level, const char *tag, const char *format, ...) __attribute__((format(printf, 3, 4)));
关于这个格式化检查的在MSCV下如何替换
在 Visual Studio 中,可以使用 __format_string 标记替代 GCC 和 Clang 中的 attribute((format(printf, ...)))。将代码中的原语句:
void Log(Log level, const char *tag, const char *format, ...) __attribute__((format(printf, 3, 4)));
替换为:
void Log(Log level, const char* tag, _Printf_format_string_ const char* format, ...);
其中,_Printf_format_string_是一种 SAL(Source Code Annotation Language)注释格式,在MSVC中用于标识输入参数的格式。
通过这种方式,可以使得在 Visual Studio 中编译代码时也能进行参数格式检查。
void __declspec(format(printf, 3, 4)) Log(Log level, const char *tag, const char *format, ...);