How to use _CRT_SECURE_NO_WARNINGS - c++

I have compile error in my simple MFC window application generated from wizard with several lines of code:
error C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
I set Configuration Properties>>C/C++>>Preporocessor>>Preprocessor Definitions>> _CRT_NONSTDC_NO_WARNINGS
But this does't helped. I have another very close project that generates only warning in this place and it has no _CRT_NONSTDC_NO_WARNINGS definition.
Only difference between projects is several different options in wizard.
Why _CRT_NONSTDC_NO_WARNINGS does not helps in first project and why second project compiles without problems without this definition?

Add by
Configuration Properties>>C/C++>>Preporocessor>>Preprocessor
Definitions>> _CRT_SECURE_NO_WARNINGS

Under "Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions" add _CRT_SECURE_NO_WARNINGS

If your are in Visual Studio 2012 or later this has an additional setting 'SDL checks' Under Property Pages -> C/C++ -> General
Additional Security Development Lifecycle (SDL) recommended checks; includes enabling additional secure code generation features and extra security-relevant warnings as errors.
It defaults to YES - For a reason, I.E you should use the secure version of the strncpy. If you change this to NO you will not get a error when using the insecure version.
SDL checks in vs2012 and later

For a quick fix or test, I find it handy just adding #define _CRT_SECURE_NO_WARNINGS to the top of the file before all #include
#define _CRT_SECURE_NO_WARNINGS
#include ...
int main(){
//...
}

Adding _CRT_SECURE_NO_WARNINGS to Project -> Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions didn't work for me, don't know why.
The following hint works:
In stdafx.h file, please add
#define _CRT_SECURE_NO_DEPRECATE
before include other header files.

Visual Studio 2019 with CMake
Add the following to CMakeLists.txt:
add_definitions(-D_CRT_SECURE_NO_WARNINGS)

I was getting the same error in Visual Studio 2017 and to fix it just added #define _CRT_SECURE_NO_WARNINGS after #include "pch.h"
#include "pch.h"
#define _CRT_SECURE_NO_WARNINGS
....

Related

VS 2019 C++ Missing symbols in pch.h

I've seen the related question regarding "_T()" not found. I have a console app. The "Debug" version of the console app compiles and runs fine. When I try to compile it as a "Release" version I first get hundreds of _T() Not Found. I tried adding #include <tchar.h> to pch.h which was completely ineffective. Based on what I found in the question about undefined _T() I read, I added the text
#ifdef _UNICODE
#define _T(x) L ## x
#else /* _UNICODE */
#define _T(x) x
#endif /* _UNICODE */
#include <tchar.h>
#include "framework.h"
to pch.h before #include "framework.h". The compiler noticed but now complained as follows:
Error C2039 '_tcscpy_s': is not a member of '`global namespace'' Anon C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29333\atlmfc\include\atlchecked.h 111
plus a few hundred other similar messages.
This is not the first time this has happened. Every project I've tried to build with VS2019 eventually has this happen to it. And, this happens compiling a completely pristine "pch.h" that I had not (initially) modified at all before this error occurred.
I've already tried deleting VS and reinstalling it. My OS is Windows 10 (64-bit)
The list of Include directories is the same for both the Debug and Release configurations.
I feel that this must be related to something in the way Visual Studio is configured, but mostly I need help fixing it. The compiler seems to be getting tangled up in its own header files. What can I do to analyze/fix this?
ADDENDUM:
Just since I started writing this question I have gone back to my project and tried the Debug version again. Now it is getting the same errors. And I made no changes since the previous time I built it earlier today.

"#using" requires C++/CLI mode

My code starts off like this:
#include "stdafx.h"
#include <string>
#using <mscorlib.dll>
#using <System.Data.dll>
#using <System.dll>
using namespace System;
using namespace System::Data::OleDb;
but ends up with the compile error "#using" requires C++/CLI mode
I had this problem initially and managed to solve this through following this post IntelliSense: "#using" requires C++/CLI to be enabled. Updated VS2017 to ver 15.3.0 yesterday and now it is broken again. Project Properties are as follows:
Any assistance would be greatly appreciated.
P.
I came across the same issue and figured out that the settings(Common Language Runtime Support) were set only for Release configuration. I suggest you to check for all the configurations in your solutions Release/Debug etc.. and set the CLR support option and it should work as expected.
You can also control this setting from within the code by putting this in your files
#pragma managed
First I would search your code to be sure this #pragma isn't being used to turn OFF managed building somewhere earlier (in some #include file, perhaps?). Then try putting it in this file>
I'm not sure if this will solve your problem but it should at least eliminate the worry that the file is not being build properly. And perhaps the error message might change to give you a better idea what the problem is
https://msdn.microsoft.com/en-us/library/0adb9zxe.aspx?f=255&MSPPError=-2147217396
C++ CLI Correct way to use #pragma managed / unmanaged

Visual Studio 2015 won't suppress error C4996

Visual Studio 2015 Community Edition gives the following error when compiling in debug, but not when compiling in release:
std::copy::_Unchecked_iterators::_Deprecate': Call to 'std::copy' with parameters that may be unsafe - this call relies on the caller to check that the passed values are correct. To disable this warning, use -D_SCL_SECURE_NO_WARNINGS. See documentation on how to use Visual C++ 'Checked Iterators'
I was able to trace back the source of this error to lines 214 and 242 of this third party library I'm using to write bitmap images. I don't fully understand what is going on in these parts, so I'd rather not mess with it.
I'm trying to disable this error, but Visual Studio won't let me. I've tried the following solutions that were suggested in the documentation, on StackOverflow, or elsewhere:
Add 4996 to the "Disable Specific Warnings" field in Project Settings > Configuration Properties > C/C++ > Advanced.
Add /wd4996 to the "Command Arguments" field in Project Settings > Configuration Properties > Debugging.
Add #pragma warning (disable : 4996) at the top of the offending file, and/or above the offending function.
Add _SCL_SECURE_NO_WARNINGS, _SCL_NONSTDC_NO_WARNINGS, _SCL_OBSOLETE_NO_WARNINGS, _SCL_SECURE_NO_WARNINGS_GLOBAL, and combinations thereof to the "Preprocessor Definitions" field in Project Settings > Configuration Properties > C/C++ > Preprocessor.
Add the definitions from the previous solution with a #define directive to the top of the offending file.
Add the definitions from the previous solution but prefixed with /D or with -D to the "Command Arguments" field.
But none of this fixes the issue for me.
What could possibly be the reason for Visual Studio to keep insisting on displaying this error?
define NO_WARN_MBCS_MFC_DEPRECATION
Disabling warning 4996 has no effect on std::copy warnings. To suppress this warning place the following at the top of your source file:
#define _SECURE_SCL_DEPRECATE 0
#include <algorithm>
Add _CRT_NONSTDC_NO_WARNINGS to preprocessor definitions.
In your stdafx.h:
#pragma warning( push )
#pragma warning( disable: 4996)
#include <algorithm>
#pragma warning( pop )
Worked for me VS2015 update 3

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

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);
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);
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.
put this define into stdafx.h.
E.g.
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
In my point of view, on a Windows project, it is not a good idea to disable the warning; a better idea is to improve the code. Mute the warning not just keeps this potential code vulnerability unnoticed, but also blinds programmers when introducing other potential code vulnerabilities.
From the docs:
You can turn off the warning for a specific line of code by using the warning pragma, #pragma warning(suppress : 4996). You can also turn the warning off within a file by using the warning pragma, #pragma warning(disable : 4996).
https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-3-c4996?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(C4996)%26rd%3Dtrue&view=vs-2017

boost problem in windows 7

I have written the following code
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/filesystem.hpp>
#include <boost/system/windows_error.hpp>
using namespace boost::system;
int main(){
boost::asio::io_service io;
boost::asio::deadline_timer t(io,boost::posix_time::seconds(5));
t.wait();
std::cout<<"hello world";
return 0;
}
and I get the following error:
1>LINK : fatal error LNK1104: cannot open file 'libboost_system-vc100-mt-gd-1_44.lib'
I dont know how and what do, can you please explain what is happening and what steps I can take to fix it?
Ok, for MSVC++ 2010
Under Project Properties, goto
Configuration Properties -> Linker -> General -> Additional Library Directories and add there the path to the *.lib file (For example: C:\boost_1_41_0\stage\lib)
As far as I can tell from the error message it compiles but can't find the boost compiled libraries.
These you have to build yourselves unless you can find them prebuilt.
IIRC boost are built using a tool called bjam. I think this explains it rather throughly: http://www.highscore.de/cpp/boostbuild/index.html.
After it's built you have to instruct the compiler to link it using the project properties.
I suspect you haven't built the libraries. You can get the pre-built libraries from BoostPro or you can build them yourself following the instructions at http://www.boost.org/doc/libs/1_44_0/more/getting_started/windows.html
I was working in one instance of Visual Studio 2010. When I started up another, to scrawl out a bit of code, I was shocked to see the same error message. Reset includes and lib (Project->NameofProject Properties then select VC++ Directories) and toggled back and forth between debug and release, at first just once, then a few more times, as I grew increasingly alarmed at none of this working.
Even though the IDE didn't report any activity ('Build Failed,' was all it said in the place where it shows includes/libs being enumerated) after a few minutes (of furious web browsing) I came back to discover that it had silently self-fixed.