Applying _CRT_SECURE_NO_WARNINGS only to a fragment of code - visual-c++

Is there a way to apply _CRT_SECURE_NO_WARNINGS only to s fragment of code? I tried the obvious
#define _CRT_SECURE_NO_WARNINGS
// some code here
#undef _CRT_SECURE_NO_WARNINGS
It seems it doesn't work.

The _CRT_SECURE_NO_WARNINGS flag is applied to the declarations of the relevant functions when they're included in your source code. You can't really turn them on or off on a per-call basis, it's an all-or-nothing setting. That's why the #define _CRT_SECURE_NO_WARNINGS directive must come before the #include directives that include the relevant function declarations.
As an alternative, you can bracket your calls with #pragma warning directives to turn off that specific warning:
#pragma warning(push)
#pragma warning(disable:4996)
printf("Look Ma, no warnings!\n");
printf("This is awesome!\n");
#pragma warning(pop)
// or for just one call,
#pragma warning(suppress:4996)
printf("We'll look the other way just this once.\n");

Related

#define new DEBUG_NEW in stdafx.h

I've noticed that several of our projects do the whole
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
thing in their stdafx.h.
Will this memory leak detection work as intended when specified in the stdafx?
Don't put it in stdafx.h. Doing so can give you undesired side effects.
Here's why.
In most cpp files, you have something like this:
#include "stdafx.h"
#include <AcmeHeader.h>
#include "MyHeader.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
Note that the redefinition of new is explicitly supposed to happen after all headers are included. If you define DEBUG_NEW in stdafx.h, then that definition will also be applied to AcmeHeader.h and MyHeader.h, which can cause problems with headers that try to redefine operator new.
I've also run into cases where I've wanted to remove the redefinition of "new" for just one or two files, but that's a rare situation.

Preprocessor definitions in code and project properties

I have simple console application in which I must set preprocessor definition _CRT_SECURE_NO_WARNINGS. According to my understanding I can set it my code right after #include "stdafx.h" by puting line #define _CRT_SECURE_NO_WARNINGS or in Project Properties -> Preprocessor -> Preprocessor Definitions. If I do first way I still have problems in code if I do second way - it solves my problem. Why I can't define #define _CRT_SECURE_NO_WARNINGS in source code?
The best way to solve this problem is really on the project properties, causing it to be used globally as a compiler flag. If you use the #define _CRT_SECURE_NO_WARNINGS in a file, only that file will have that warning suppressed.

winsock deprecated no warnings

I am trying to create a UDP multicast socket program using VS2015 (C++ console application).
I got the following error,
Error C4996 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings mulitcast_listener
I tried to add _WINSOCK_DEPRECATED_NO_WARNINGS symbol to my project settings via "Project"->"Properties"->"Configuration properties"->"C/C++"->"Preprocessor"->"Preprocessor definitions"
.. But still it says the same.
And then I tried to add symbol above #include "stdafx.h"
like
#define _WINSOCK_DEPRECATED_NO_WARNINGS 1
and then No(/sdl-) on "Project"->"Properties"->"Configuration properties"->"C/C++"->General->SDL checks
now I get a error message saying
Warning C4603 '_WINSOCK_DEPRECATED_NO_WARNINGS': macro is not defined or definition is different after precompiled header
Finally I tried to implement
inet_pton(AF_INET, HELLO_GROUP, (PVOID *)(&mreq.imr_multiaddr.s_addr));
instead of
mreq.imr_multiaddr.s_addr = inet_addr(HELLO_GROUP);
I need to understand why the error didn't resolved even after adding the _WINSOCK... macro.
Thanks in advance.
As noted in the comments, the solution is to make sure that the line
#define _WINSOCK_DEPRECATED_NO_WARNINGS
is placed after
#include "stdafx.h"
but before the other #include statements.
While the previous advice works, it is ignoring the purpose of stdafx.h. The idea is that you place #include statements for header files that don't change frequently inside stdafx.h in order to make use of precompiled headers. Therefore you should ideally place
#define _WINSOCK_DEPCRECATED
inside stdafx.h, before other #include statements that it affects, in particular before including winsock2.h or other winsock related headers.
// pch.h
#ifndef PCH_H
#define PCH_H
#define _WINSOCK_DEPRECATED_NO_WARNINGS // defined here and it worked
#include "framework.h"
#include "xxx.h"

fstream and ostrstream is undefined

HI,
I am converting my project from vc6 to latest using vs 2010. I get problem on compiling my code
Error 931 error C2065: 'ostrstream' : undeclared identifier
1100 IntelliSense: identifier "fstream" is undefined
I have included the required files as told in Google
#if ! defined(_FSTREAM_)
#include <fstream>
#endif
#if ! defined(_STRSTREAM_)
#include <strstream>
#endif
When i press F12 on the fstream or ostrstream it takes to the respective files where these class are defined. Is there any other includes i have to do, i have been searching for this for long time with no luck :(
Thanks
Arvind
Add these to your library list:
#include <stdio.h>
using namespace std;
Forget all the preprocessor stuff; it's redundant at best (the files in questiion will have reinclusion guards) and at worst an error (you're assuming the #defines used, which are arbitary). Just use code like this:
#include <fstream>
#include <strstream>
Also note that the strstream header is deprecated. You should use sstream instead, but note the newer classed in this file word differently to the deprecated ones).
Without using namespace std; as its good practice.
#include<fstream>
std::fstream fileio;
Where the fileio is the object created by fstream. The fstream class needs to be created using the std keyword as it is part of the standard library.
Hence using : using namespace std; in the code makes it work.
NOW using using namespace std; can cause conflicts in shared libraries.
#include <fstream>
using namespace std;
fstream fileio;
What worked for me was a combination of two answer. After some trial and error, fstream squiggly red lines vanished when I added,
#include <fstream>
using namespace std;

suppress gcc warnings : "warning: this is the location of the previous definition"

I need a set of wrappers around the standard system calls-open,listen,etc.
For these i have a few "#define" as in:
#define open(a,b,c) JCL_Open(a,b,c)
But when i compile the header and associated .c files, i am getting the following warning:
/jcl_wrappers.h:114:1: warning: "open" redefined
/jcl_wrappers.h:113:1: warning: this is the location of the previous definition
How can i suppress these warnings?
Put include guards in your header file.
Basically you need to put these two lines at the beginning of your header file
#ifndef _yourincludefilename_h_
#define _yourincludefilename_h_
and
#endif /* _yourincludefilename_h_ */
at the end of you include file.
Edit:
rascher is right, that open is not a good name for a macro, as it will conflict with a library function. Usually it is good C convention to make macros all uppercase, so I would suggest to change your macro to
#define OPEN(a,b,c) JCL_Open(a,b,c)
or even better
#define XYZ_OPEN(a,b,c) JCL_Open(a,b,c)
where XYZ is a prefix specific to your code.
Leave the standard functions alone and rename the function:
#define myopen(a,b,c) JCL_Open(a,b,c)
Someone will thank you later.
You might try using compile guards. Like:
my_headers.h:
#ifndef __MY_HEADERS
#define __MY_HEADERS
#define open(a,b,c) JCL_Open(a,b,c)
#endif
This will only do do what is between the #ifndef and #endif if the '__MY_HEADERS' macro has been defined. So, everything in your .h file will only be declared once.
You could use the same construct to see if the "open" macro is already defined.
Also be aware that there is already a C function called open(): http://www.manpagez.com/man/2/open/ . May not be a great idea to use the same name for your macro!
Wrap the define in:
#ifndef JCL_WRAPPERS_H
#define JCL_WRAPPERS_H
... your macro's
#endif
This makes sure your macro's are only defined once if you including your header in multiple places.
It's generally a smart thing to do for all prototypes in headers also.

Resources