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

1wnzp6jl  于 2023-10-22  发布在  Windows
关注(0)|答案(1)|浏览(157)

我试图编译一个简单的程序,使用intsafe.h头与MSVC:

#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的文档也没有提到任何库(与类似的其他函数相反),所以我不确定该告诉链接器什么

xv8emn3q

xv8emn3q1#

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.
//

相关问题