提问人:Will Ayd 提问时间:10/20/2023 更新时间:10/20/2023 访问量:100
如何在MSVC中使用intsafe.h函数?
How to use intsafe.h functions with MSVC?
问:
我正在尝试使用带有 MSVC 的 intsafe.h 标头编译一个简单的程序:
#include <intsafe.h>
int main(void) {
int result;
return IntAdd(10, 10, &result);
}
尝试编译此程序时,我从链接器收到错误
/opt/msvc/bin/x86/cl test.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.37.32825 for x86
Copyright (C) Microsoft Corporation. All rights reserved.
test.c
Microsoft (R) Incremental Linker Version 14.37.32825.0
Copyright (C) Microsoft Corporation. All rights reserved.
/out:test.exe
test.obj
test.obj : error LNK2019: unresolved external symbol _IntAdd referenced in function _main
test.exe : fatal error LNK1120: 1 unresolved externals
但是,我找不到 IntAdd 符号存在的位置。我对 MSVC 发行版附带的所有 .lib 文件使用了 dumpbin,但没有一个文件显示此符号。IntAdd 的文档也没有提到任何库(与其他类似的函数相比),所以我不确定该告诉链接器什么
答:
3赞
RbMm
10/20/2023
#1
IntAdd
在条件块中定义
#if defined(ENABLE_INTSAFE_SIGNED_FUNCTIONS)
...
#endif
如果你使用 C++ - 你得到了
error C3861: 'IntAdd': identifier not found
但是使用 C 编译器让我们使用未声明,但因为(这意味着您使用 和 )实际上没有在任何 obj 或 lib 中定义,您得到了链接器错误IntAdd
_IntAdd
x86
__cdecl
如果要使用“下一步执行”:IntAdd
#define ENABLE_INTSAFE_SIGNED_FUNCTIONS
#include <intsafe.h>
另请阅读来自 insafe.h 的评论
/////////////////////////////////////////////////////////////////////////
//
// signed operations
//
// Strongly consider using unsigned numbers.
//
// Signed numbers are often used where unsigned numbers should be used.
// For example file sizes and array indices should always be unsigned.
// (File sizes should be 64bit integers; array indices should be size_t.)
// Subtracting a larger positive signed number from a smaller positive
// signed number with IntSub will succeed, producing a negative number,
// that then must not be used as an array index (but can occasionally be
// used as a pointer index.) Similarly for adding a larger magnitude
// negative number to a smaller magnitude positive number.
//
// intsafe.h does not protect you from such errors. It tells you if your
// integer operations overflowed, not if you are doing the right thing
// with your non-overflowed integers.
//
// Likewise you can overflow a buffer with a non-overflowed unsigned index.
//
评论
ENABLE_INTSAFE_SIGNED_FUNCTIONS
必须定义,否则根本不可见IntAdd
stdckdint.h