From: Dave Burns on
Hi, I want to use the rand_s function in my application.

I have included the following at the top of my module that uses the
function:

#define _CRT_RAND_S
#include <cstdlib>

I invoke the function as follows:
unsigned number;
errno_t err = rand_s(&number);

The compiler generates the error "C3861: 'rand_s': identifier not found". I
am using VS-2005.

I looked in cstdlib and stdlib.h. I can see the declaration in stdlib.h as
follows:
#if defined(_CRT_RAND_S)

_CRTIMP errno_t __cdecl rand_s ( __out unsigned int *_RandomValue);

#endif

There are a lot of "#if" statements in this header and I'm wondering if one
of them is negating the declaration.

Thanks for any advice, Dave


From: Dave Burns on
I also tried adding the declaration to my module. It compiles now, but the
linker fails with the following:

error LNK2019: unresolved external symbol "__declspec(dllimport) int __cdecl
rand_s(unsigned int *)" (__imp_?rand_s@@YAHPAI@Z) referenced in function ...
Dave


From: Igor Tandetnik on
Dave Burns <dburns0000(a)aol.com> wrote:
> Hi, I want to use the rand_s function in my application.
>
> I have included the following at the top of my module that uses the
> function:
>
> #define _CRT_RAND_S
> #include <cstdlib>
>
> I invoke the function as follows:
> unsigned number;
> errno_t err = rand_s(&number);

Try std::rand_s. Note that you are including <cstdlib> rather than
<stdlib.h> - presumably so that CRT functions be in std namespace.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925