我的 C 代码中的定义有问题:出于测试目的,我需要在编译期间将 vsnprintf
定义为 _tests_vsnprintf
,例如:
gcc -Dvsnprintf=_tests_vsnprintf file.c
这允许我从 vsnpintf
调用返回错误,以便测试我的应用程序中的错误管理。它在 Linux 上运行良好,但不能在 Windows 上构建,MSVC 1900 (Visual Studio 2015) 提示宏重新定义 ( C1189 )。
错误来自 stdio.h
中的这段代码:
#if defined vsnprintf
...
#error Macro definition of vsnprintf conflicts with Standard Library function declaration
#endif
我不知道如何用最少的修改来消除错误。
你能帮帮我吗?
最佳答案
VC header 中的代码专门阻止您这样做:
#if defined vsnprintf
// This definition of vsnprintf will generate "warning C4005: 'vsnprintf': macro
// redefinition" with a subsequent line indicating where the previous definition
// of vsnprintf was. This makes it easier to find where vsnprintf was defined.
#pragma warning(push, 1)
#pragma warning(1: 4005)
#define vsnprintf Do not define vsnprintf as a macro
#pragma warning(pop)
#error Macro definition of vsnprintf conflicts with Standard Library function declaration
#endif
正如@Olaf 提到的,如果您重新定义标准函数,您可能会遇到库代码问题。但是,如果您仅将其限制在您的代码中并确保任何标准 header 均未选择新定义,则您应该能够执行 #define vsnprintf
之后你包括<stdio.h>
.
#include <stdio.h>
#define vsnptrintf my_test_vsnprintf
关于c - MSVC 1900 并定义 vsnprintf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42536311/