I have some pragma warning (disable : xxx) in cpp files (not headers):
now if we omit pragma warning (default : xxx) in same cpp file, does that warning stay disabled across all cpp files in project or just in this cpp file?
also if we compile multiple projects, does that disabled warning affect all projects? assuming pragma in cpp file only not in headers.
for example, I have:
#ifdef NDEBUG // In release build using CrtDebug functions has no effect!
#define CRT_DBG_REPORT(...) 0
#pragma warning (disable : 6326) // Potential comparison of a constant with another constant
#pragma warning (disable : 26814) // The const variable can be computed at compile time
#pragma warning (disable : 26477) // Use nullptr rather than 0 or NULL
#pragma warning (disable : 4127) // conditional expression is constant
#pragma warning (disable : 4100) // unreferenced formal parameter
#else
#define CRT_DBG_REPORT _CrtDbgReportW
#endif // NDEBUG
I would like to omit setting back to 'default' but ensure it only disables the warnings for this cpp file.
edit
thanks to comment section suggestion...
If we enable Unity build, what is the behavior of "undefauled" (only disabled) cpp pragmas? Project properties -> Advanced -> Unity.
If the warnings are disabled in the cpp file, they will only affect the lines underneath the pragma (they won't affect other compilation units). Unity builds would probably cause an issue though (at a guess, can't test this right now).
Generally speaking, if you are enabling/disabling warnings via pragmas, this is probably the way to go:
#define PUSH_DISABLE_WARNINGS \
__pragma(warning(push)) \
__pragma(warning(disable : 6326)) \
__pragma(warning(disable : 26814)) \
__pragma(warning(disable : 26477)) \
__pragma(warning(disable : 4127)) \
__pragma(warning(disable : 4100))
#define POP_DISABLE_WARNINGS \
__pragma(warning(pop))
and then later on...
PUSH_DISABLE_WARNINGS
/* warnings will only be disabled here */
POP_DISABLE_WARNINGS
Related
We have a VS 2019 project which includes external source libraries which we add to our project. For instance we are using WTL (Windows Template Library). During compile time, these libraries produce huge numbers of warnings (particularly for members which do not have default values).
For those files, we would like to completely disable warnings, or at least specify which warnings should be excluded for certain directories, because
We are not going to change that code
There are so many warnings, that we can easily miss some from our own code that we should resolve
I saw a post about some flags that you should be able to pass to disable warnings for "external" files, but I do not see any options in the project settings.
There are several ways to disable the warning:
Project Properties->C/C++->General->Warning Level->select level
Here is the Warning Level:
Turn off all warnings (/W0): Turn off the display of all warning
messages. Level 1 (/W1): Display serious warning messages. Level 2
(/W2): Display level 1 warnings and some less serious warnings, such
as warnings about hidden class members. This is the default warning
level on the command line. Level 3 (/W3): Display level 2 warnings
and some less serious warnings, such as warnings about expressions
that always evaluate to true or false. Level 4 (/W4): Display all
level 3 warnings and informational warnings.
Or you could choose to disable specific warnings in Project Properties->C/C++->Advanced->Disable Specific Warnings
You could use warning pragma.
Syntax:
#pragma warning(
warning-specifier : warning-number-list
[; warning-specifier : warning-number-list ... ] )
#pragma warning( push [ , n ] )
#pragma warning( pop )
Also, you could refer to Microsoft about How to: Enable and Disable Code Analysis for Specific C/C++ Warnings.
To enable or disable a code analysis warning
2.1.Create a header file that lists all the code analysis warnings and their initial state, as shown in the following code:
// WarningState.h
#pragma warning ( default : 6001 )
#pragma warning ( disable : 6011 )
// more warnings here
// end of file
2.2.Include WarningState.h in the application header file. In this case, MyApplication.h represents the header file.
// MyApplication.h file
#include "WarningState.h"
// ...
// end of file
2.3.Include MyApplication.h file in the source code file. In this case, MyApplication.cpp represents the source file.
// MyApplication.cpp file
#include "MyApplication.h"
2.4.To modify the warning state, use the pragma warning-specifier in a .cpp file, as shown in the following code:
// MyApplication.cpp file
#include "MyApplication.h"
#pragma warning ( disable: 6001 )
#pragma warning ( default : 6001 )
To disable all code analysis warnings for included third-party files
Add the following code to your header file.
#include <codeanalysis\warnings.h>
#pragma warning( push )
#pragma warning ( disable : ALL_CODE_ANALYSIS_WARNINGS )
#include <third-party include files here>
#pragma warning( pop )
Is there any way to prevent Visual Studio from printing out warnings from included libraries?
\Wall gives me loads of warnings from STL and Qt headers, although I only want to see those originating from my own code (i.e. the code which is part of the current Visual Studio project).
You can use pragma to set the warning levels for each file.
So before you include
#pragma warning( push )
#pragma warning( disable : 4705 )
#pragma warning( disable : 4706 )
#pragma warning( disable : 4707 )
// Some code
#include your files here
#pragma warning( pop )
More information here: http://msdn.microsoft.com/en-us/library/2c8f766e%28v=vs.80%29.aspx
That's the only portable way (if using -isystem with other compilers):
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: ...)
#endif
#include <Q...>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
Hopefully they will implement isystem one of these days:
https://visualstudio.uservoice.com/forums/121579-visual-studio-2015/suggestions/14717934-add-a-cl-exe-option-for-system-headers-like-gcc-s
You can use warning level 4, it will only include warnings for your code.
I am tired of having to look at warnings during our compilations - warnings that come from MS include files.
"C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\mmsystem.h(1840): warning C4201: nonstandard extension used : nameless struct/union"
I have seen this thread that suggests changing the header itself (but then each of my team mates has to do it and we have to do it on the build server - not to mention it is a glorious HACK)
Is there a better way? I don't want to turn it off globally - just to suppress it for certain files or directories.
Any suggestions?
EDIT
For some stupid reason I didn't think I could set warning levels across include files. Thanks - that does the trick.
Something like
#pragma warning(push, disable: 4201)
#include <mmsystem.h>
#pragma warning(pop)
How about using #pragma warning extension in VC++?
http://msdn.microsoft.com/en-us/library/2c8f766e(VS.80).aspx
#pragma warning (push, 2) // Temporarily setting warning level 2
#include <mmsystem.h>
#pragma warning (pop) // Restore back
You may also try ..
#pragma warning (disable: 4201)
#include <mmsystem.h>
#pragma warning (default)
With VS 2017 Community, to avoid warning C4083, I use :
#pragma warning(push)
#pragma warning(disable:4201)
#include <dxvahd.h>
#pragma warning(pop)
Please look at the following file: (it is a complete file)
#ifndef TEES_ALGORITHM_LIBRARY_WRAPPER_H
#define TEES_ALGORITHM_LIBRARY_WRAPPER_H
#ifdef _TEES_COMPILE_AS_LIB
#include <dfa\Includes\DFC_algorithms.hpp>
#include <DFA\FuzzyClassifier\FuzzyAlgorithmIntialization\InitFuzzyAlgorithm.hpp>
typedef teesalgorithm::tees_fuzzy_algorithms algorithms_switchyard_class;
#else
#include <DFA\Includes\commercial_algorithms.hpp>
//An incomplete class to hide implementation
class algorithms_switchyard_class;
#endif
class AlgorithmLibraryWrapper {
algorithms_switchyard_class * algorithmPtr_;
typedef teesalgorithm::tees_paramObj paramObj_type;
typedef teesalgorithm::tees_errorObj errorObj_type;
typedef teesalgorithm::tees_statusObj statusObj_type;
typedef teesalgorithm::tees_dataObj dataObj_type;
typedef teesalgorithm::tees_outputObj outputObj_type;
public:
AlgorithmLibraryWrapper(const std::string& sAlgName, paramObj_type& paramObj, errorObj_type& errObj, statusObj_type& statusObj, const char* sFilePath);
static bool dataReader(const std::string& sFileName, dataObj_type& dataObj, errorObj_type& errObj, statusObj_type& statusObj);
bool runalgorithm(const dataObj_type& dataObj, outputObj_type& outObj, errorObj_type& errObj, statusObj_type& statusObj);
~AlgorithmLibraryWrapper();
};
#ifdef _TEES_USE_COMPILED_ALGORITHM_LIB
# ifdef _MSC_VER
#if _MSC_VER < 1400 // If VC 2003
#ifdef _DEBUG
#error No AlgorithmLibWrapper libraries compiled for this version of VC
#else
#error No AlgorithmLibWrapper libraries compiled for this version of VC
#endif
#elif defined(UNDER_CE) // Win CE
#ifdef _DEBUG
#pragma comment( lib, "AlgorithmLibWrapperCEd" )
#else
#pragma comment( lib, "AlgorithmLibWrapperCE" )
#endif
#else // If VC 2005
#ifdef _DEBUG
#pragma comment( lib, "AlgorithmLibWrapperd" )
#else
#pragma comment( lib, "AlgorithmLibWrapper" )
#endif
#endif
#endif
#endif
#endif //TEES_ALGORITHM_LIBRARY_WRAPPER_H
I am getting the following errors; I don't know why. I manually counted the preprocessor directives also.
AlgorithmLibraryWrapper.hpp:10:1: unterminated #ifdef
AlgorithmLibraryWrapper.hpp:7:1: unterminated #ifndef
I am using the poor vxWorks gcc compiler. Please let me know if the fault is mine or the compiler's.
It could be that the problem is in the included files (if there actually are unbalaced #if/#endifs.
I would try preprocessing with another compiler. You can use gcc for that, doesn't matter it wouldn't compile. Just get gcc (or MinGW if you're on Windows) and run
cpp -Iinclude_direcories your_file
Or, if you don't like gcc, get MSVC Express edition. Again, you can preprocess code that even doesn't compile, so no problem with nonworking library etc.
Most compilers have an option that will give you the output from the preprocessor so you can check what it's doing. For example,
gcc -E file.c >file.preproc
will give you the pre-processed source so you can check the balancing of #if against #endif.
At a guess, one of the files you are #including from this one has a mismatched #ifdef/#endif pair. You need to look at all the files (as the preprocesor does), not just this one.
As others have noted, this is most likely due to mismatched include guards.
If the files you are including are under your control (i.e. not part of a 3rd party closed source library), you could consider replacing the #ifndef et. al. guards (which are used to prevent multiple inclusion) with #pragma once. This will eliminate the possibility of having mismatched preprocessor directives.
One caveat of this is that pragma once is non-standard, so it will only work if your compiler supports it.
I have tried to compile your source using vs 6.0 but did not get the error you have mentioned. As others said may be the error is coming from the included header file . For me to get your code compiled i need to comment the above header.
Please check the above header once.
I'd debug this by commenting out sections one by one and trying to identify which section is causing the error.
It could be your compiler does not like the nested #ifdefs or does not interpret the syntax correctly. Maybe it doesn't like the #pragmas.
This is a long shot, but in your source file you have the following line:
# ifdef _MSC_VER
where there is whitespace between the '#' character and the directive name (ifdef). This is valid in C/C++; however, it's not too commonly seen so I wouldn't be very surprised if the odd compiler choked on it.
What is the best way to disable the warnings generated via _CRT_SECURE_NO_DEPRECATE that allows them to be reinstated with ease and will work across Visual Studio versions?
If you don't want to pollute your source code (after all this warning presents only with Microsoft compiler), add _CRT_SECURE_NO_WARNINGS symbol to your project settings via "Project"->"Properties"->"Configuration properties"->"C/C++"->"Preprocessor"->"Preprocessor definitions".
Also you can define it just before you include a header file which generates this warning.
You should add something like this
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
And just a small remark, make sure you understand what this warning stands for, and maybe, if you don't intend to use other compilers than MSVC, consider using safer version of functions i.e. strcpy_s instead of strcpy.
You could disable the warnings temporarily in places where they appear by using
#pragma warning(push)
#pragma warning(disable: warning-code) //4996 for _CRT_SECURE_NO_WARNINGS equivalent
// deprecated code here
#pragma warning(pop)
so you don't disable all warnings, which can be harmful at times.
i work on a multi platform project, so i can't use _s function and i don't want pollute my code with visual studio specific code.
my solution is disable the warning 4996 on the visual studio project. go to Project -> Properties -> Configuration properties -> C/C++ -> Advanced -> Disable specific warning add the value 4996.
if you use also the mfc and/or atl library (not my case) define before include mfc _AFX_SECURE_NO_DEPRECATE and before include atl _ATL_SECURE_NO_DEPRECATE.
i use this solution across visual studio 2003 and 2005.
p.s. if you use only visual studio the secure template overloads could be a good solution.
You can also use the Secure Template Overloads, they will help you replace the unsecure calls with secure ones anywhere it is possible to easily deduce buffer size (static arrays).
Just add the following:
#define _CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES 1
Then fix the remaining warnings by hand, by using the _s functions.
Combination of #[macbirdie] and #[Adrian Borchardt] answer. Which proves to be very useful in production environment (not messing up previously existing warning, especially during cross-platform compile)
#if (_MSC_VER >= 1400) // Check MSC version
#pragma warning(push)
#pragma warning(disable: 4996) // Disable deprecation
#endif
//... // ...
strcat(base, cat); // Sample depreciated code
//... // ...
#if (_MSC_VER >= 1400) // Check MSC version
#pragma warning(pop) // Renable previous depreciations
#endif
For the warning by warning case, It's wise to restore it to default at some point, since you are doing it on a case by case basis.
#pragma warning(disable: 4996) /* Disable deprecation */
// Code that causes it goes here
#pragma warning(default: 4996) /* Restore default */
The best way to do this is by a simple check and assess. I usually do something like this:
#ifndef _DEPRECATION_DISABLE /* One time only */
#define _DEPRECATION_DISABLE /* Disable deprecation true */
#if (_MSC_VER >= 1400) /* Check version */
#pragma warning(disable: 4996) /* Disable deprecation */
#endif /* #if defined(NMEA_WIN) && (_MSC_VER >= 1400) */
#endif /* #ifndef _DEPRECATION_DISABLE */
All that is really required is the following:
#pragma warning(disable: 4996)
Hasn't failed me yet; Hope this helps
you can disable security check. go to
Project -> Properties -> Configuration properties -> C/C++ -> Code Generation -> Security Check
and select Disable Security Check (/GS-)
You can define the _CRT_SECURE_NO_WARNINGS symbol to suppress them and undefine it to reinstate them back.
Another late answer... Here's how Microsoft uses it in their wchar.h. Notice they also disable Warning C6386:
__inline _CRT_INSECURE_DEPRECATE_MEMORY(wmemcpy_s) wchar_t * __CRTDECL
wmemcpy(_Out_opt_cap_(_N) wchar_t *_S1, _In_opt_count_(_N) const wchar_t *_S2, _In_ size_t _N)
{
#pragma warning( push )
#pragma warning( disable : 4996 6386 )
return (wchar_t *)memcpy(_S1, _S2, _N*sizeof(wchar_t));
#pragma warning( pop )
}