Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This function or variable may be unsafe. To disable deprecation, use _CRT_SECURE_NO_WARNINGS

Tags:

c++

I'm working on a C++ DDL, however I get the following issue in some places:

C4996 'sprintf': This function or variable may be unsafe. Consider using sprintf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

I did try #define _CRT_SECURE_NO_WARNINGS, but the issue remains.
This is the code:

sprintf(szDebugString, "%s: 0x%x (%s%s%i)", ptrName, (DWORD)funcPtr, interfaceName, interfaceVersion.c_str(), i);
like image 533
Joakim Carlsson Avatar asked Mar 30 '16 11:03

Joakim Carlsson


People also ask

How do I disable deprecation in Visual Studio?

This function or variable may be unsafe. Consider using safe-version instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS.

What is _crt_secure_no_warnings?

_CRT_SECURE_NO_WARNINGS means you don't want the compiler to suggest the secure versions of the library functions, e.g. scanf_s when you use scanf .

How do I add CRT secure no warnings in Visual Studio?

Detail steps as below: Right Click 'Project Properties->C/C+±>Preprocessor' Add '_CRT_SECURE_NO_WARNINGS' to preprocessor Definitions and click ok. In addition, if you have any other issue, please create a new feedback to us!


3 Answers

You have to define _CRT_SECURE_NO_WARNINGS before #include <Windows.h>.

Alternatively, use the safe version:

sprintf_s(szDebugString, sizeof(szDebugString), "%s: 0x%x (%s%s%i)",
    ptrName, (DWORD)funcPtr, interfaceName, interfaceVersion.c_str(), i);
like image 59
trojanfoe Avatar answered Oct 04 '22 03:10

trojanfoe


put this define into stdafx.h.

E.g.

#pragma once
#define _CRT_SECURE_NO_WARNINGS

#include "targetver.h"

#include <stdio.h>
#include <tchar.h>
like image 37
Vyacheslav Avatar answered Oct 04 '22 03:10

Vyacheslav


To turn off the warning for an entire project in the Visual Studio IDE:

1- Open the Property Pages dialog for your project.

2- Select the Configuration Properties > C/C++ > Advanced page.

3- Edit the Disable Specific Warnings property to add 4996. Choose OK to apply your changes.

like image 45
david Avatar answered Oct 04 '22 03:10

david