如何在 MSVC 中使用 intsafe.h 函数?

问题描述 投票:0回答:1

我正在尝试使用 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的文档也没有提及任何库(与this等其他函数相比),所以我不确定要告诉链接器什么

c windows winapi
1个回答
0
投票

IntAdd
在条件块中定义

#if defined(ENABLE_INTSAFE_SIGNED_FUNCTIONS)
...
#endif

如果你使用 c++ - 你得到了

error C3861: 'IntAdd': identifier not found

但是使用 c 编译器可以使用未声明的

IntAdd
但是因为
_IntAdd
(这意味着你使用
x86
__cdecl
)实际上没有在任何 obj 或 lib 中定义,所以你得到了链接器错误

如果您想使用

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.
//
© www.soinside.com 2019 - 2024. All rights reserved.