I am trying to compile a simple program using the intsafe.h header with MSVC:
#include <intsafe.h>
int main(void) {
int result;
return IntAdd(10, 10, &result);
}
When trying to compile this program I get an error from the linker
/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
However, I can’t find where the IntAdd symbol exists. I used dumpbin against all of the .lib files that came with the MSVC distribution and none of them showed this symbol. The documentation for IntAdd also makes no mention of any library (in contrast to other functions like this), so I am not sure what to tell the linker
IntAdd
defined in conditional block
#if defined(ENABLE_INTSAFE_SIGNED_FUNCTIONS)
...
#endif
if you use c++ – you got
error C3861: 'IntAdd': identifier not found
but with c compiler let use not declared IntAdd
but because _IntAdd
(this mean that you use x86
and __cdecl
) really not defined in any obj or lib you got linker error
if you want use IntAdd
do next:
#define ENABLE_INTSAFE_SIGNED_FUNCTIONS
#include <intsafe.h>
also read comment from 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.
//
Agree that this is bizarre, the definitions in intsafe.h are inline.
ENABLE_INTSAFE_SIGNED_FUNCTIONS
must be defined, otherwiseIntAdd
not visible at allNote that when C23 eventually goes live, there will be a
stdckdint.h
for this very purpose. Then you won’t need these fishy MS libs any longer, given that you’ll use a standard C compiler.