我正在try 用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等其他函数形成对比),所以我不确定要告诉链接器什么

推荐答案

IntAdd在条件块中定义

#if defined(ENABLE_INTSAFE_SIGNED_FUNCTIONS)
...
#endif

如果你用c++-你会得到

error C3861: 'IntAdd': identifier not found

但是有c个编译器让使用没有声明IntAdd 但是因为_IntAdd(这意味着您使用x86__cdecl)实际上没有在任何obj或库中定义,所以您得到了链接器错误

如果要使用IntAdd,请执行下一步操作:

#define ENABLE_INTSAFE_SIGNED_FUNCTIONS
#include <intsafe.h>

我还阅读了来自insafe.h的 comments

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

C++相关问答推荐

如何通过Zephyr(Devicetree)在PR Pico上设置UTE 1?

为什么静态说明符为内联函数生成外部定义?

在Windows上构建无聊的SSL x64

C strlen on char array

Ebpf内核代码:permission denied:invalid access to map value

在C语言中使用scanf()时我无法理解的警告

为什么即使在强制转换时,此代码也会溢出?

在列表中插入Int指针(C)

Make Node函数.S有什么问题吗?

如何在VS 2022中正确安装额外的C头文件

使用%f格式说明符打印整数值

链接器脚本和C程序使用相同的头文件,这可能吗?

程序如何解释变量中的值

为什么GCC不能在 struct 初始值设定项中以sizeof作为条件的三进制中处理复合文字的编译时求值?

将char*数组深度复制到 struct 中?

STM32:代码的执行似乎取决于它在闪存中的位置

根据输入/输出将 C 编译过程分为预处理、编译、汇编和链接步骤

获取 struct 中匿名 struct 的大小

我们可以在不违反标准的情况下向标准函数声明添加属性吗?

如何让 unlinkat(dir_fd, ".", AT_REMOVEDIR) 工作?