Why does #pragma warning(push,0) enable additional warnings in MSVC 2015? - c++

MCVE:
#pragma warning(push,0)
#include <QWidget>
#pragma warning(pop)
#include <QVariant>
int main(int argc, char *argv[])
{
}
compiling with Visual Studio 2015 (choose "Qt Application" as project type if it matters) and /W4 i get 2xWarning C4100 (unreferenced formal parameter) for argc and argv which is what i expect and are actually interested in. But in addition i get 5xWarning C4251(class X needs to have dll-interface to be used by clients of struct Y) somewhere inside qvariant.h.
With different included files from Qt i can produce different warnings, so it's not just C4251. I was not able to reproduce it with standard library includes yet. The warnings disappear if i remove/comment the #pragma warning-lines or set the projects /W flag to a lower level. The level pushed seems to have no effect, so e.g. with /W0 and #pragma warning(push,4) i get no warnings.
What i would have expected is, that #pragma warning(push,0) disables all warnings inside <QWidget> and #pragma warning(pop) restores the initial state. Instead, Visual Studio seems to somehow detect that those files are from an external library (or is it something inside the Qt code?), but as soon as there is a push/pop pair the pushed warning-level is ignored and instead the initially set warning level via /W flag is used.
I came across this problem initially when using CMake and cotire, which automatically generates a header file to be used as precompiled header. So in my code i don't use #pragma warning (push/pop) which compiles fine without Qt-Warnings. But the generated file wraps the includes in a push/pop pair and thus generates about 200 Warnings in my project. I'm trying to understand why this happens in order to find a solution which requires me neither to wrap all my manual includes in push/pop nor to maintain a private modification to cotire.

Related

Detect warning in MSVC 2017

Recently, we discovered that one of our external libraries (DLib) is suppressing warnings at a global level. Writing the following code gives us a warning:
[[deprecated]] int findDeprecated() { return 42; }
int test = findDeprecated();
Including a dlib header suppresses the warning:
#include <dlib/matrix.h>
[[deprecated]] int findDeprecated() { return 42; }
int test = findDeprecated();
Having to trigger all of these warnings in order to vet our external libraries is very cumbersome and not easy to integrate in our codebase.
Hence we would like to create a new test that conceptually looks like:
#include <dlib>
#include <boost>
static_assert(getWarningLevel(4996) == 4, "Deprecated Warning Was suppressed");
However, from searching around, we can't find a way to request the warning level as search engines always tell us how to enable the warning.
Does a way exist in order to retrieve the warning level?
Some restrictions to the answer:
We are only interested in Visual Studio 2017 (or above)
Extra points: Have a solution for clang-cl as well
You can save and restore your warning level(s) with:
#pragma warning(push) // Save current warning state
#include <offendingheaderfile.h>
#pragma warning(pop) // Restore saved warning state

boost.future causing problems with clr

I am using visual studio 2012 with a clr library that needs to link to a native library.
My library which is using boost::future.
I am having this problem when I use future::then(F &&) function against the managed project:
Error 910 error LNK2022: metadata operation failed (8013119F) : A TypeRef exists which should, but does not, have a corresponding TypeDef: (dummy): (0x0100003e). D:\ClrProject\somefile.obj
I tried, as suggested in other questions, to make the dummy types in the library complete, since I cannot forward declare a nested struct from inside a template, as can be done with boost::thread::dummy struct.
This did not solve the problem.
My setup is the following:
Boost 1.55.
Using boost .dlls.
define BOOST_RESULT_OF_USE_DECLTYPE
define BOOST_THREAD_PROVIDES_FUTURE
define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
I am using these very defines on the dependent .dll as well, to make sure that all parts of the API are exposed correctly.
Solved.
#Hans Passant your suggestion was right. I just wasn't putting the #pragma the correct way.
I had to put the pragmas so that my headers are compiled as unmanaged code. Once that was done, it seems that template instantiations for my code were emitted as unmanaged. At that point, problems disappeared.
So what I did is something like this:
#pragma managed(push, off)
#include "MyHeaderWithFutures.h"
#pragma managed(pop)
void f() {
//
f = myObject.Something().then(...);
}

Search for avaliable library before #pragma comment in c++?

I have a piece of code which relies on the "Xinput.lib" file provided by windows in order to interface with a xbox controller.
Recently I installed VS2012 which seems to have caused "Xinput.lib" to no longer be available to the compiler. I found a workaround where I instead I use "Xinput9_1_0.lib" on the system running the VS2012 compiler. This however causes a problem since I am developing on multiple systems where one is running VS2012 and the other is running VS2010. To compile I have to change between "Xinput9_1_0.lib" and "Xinput.lib" within the code.
What I am wondering is if there is a way to search for the available library before using #pragma comment to link the library?
Something like this (I realize this is in no way valid, just a demonstration of intent):
if("Xinput.lib" exists) {
#pragma comment(lib,"Xinput.lib")
}
else if("Xinput9_1_0.lib" exists) {
#pragma comment(lib,"Xinput9_1_0.lib")
}
Use the macro for the particular version of VS you're interested in:
#ifdef MSC_VER < 1700 //pre 2012
#pragma comment(lib,"Xinput.lib")
#else
#pragma comment(lib,"Xinput9_1_0.lib")
#endif

Automatically Including OpenCV Libraries of Different Versions

I've been automatically including the opencv library files for my c++ code on Visual Studio 2008 on Windows 7 with the following code:
#ifndef NDEBUG
#pragma comment(lib, "opencv_core231d.lib")
#pragma comment(lib, "opencv_highgui231d.lib")
#pragma comment(lib, "opencv_imgproc231d.lib")
#else
#pragma comment(lib, "opencv_core231.lib")
#pragma comment(lib, "opencv_highgui231.lib")
#pragma comment(lib, "opencv_imgproc231.lib")
#endif
but I run into trouble when the system has a different version of the opencv library installed because the .lib files have the version (in this case, 2.31) in the filename. Is there a good way to automatically or near-automatically detect what version of the opencv library is available then slide in the appropriate version string into the pragma?
There are small changes between different OpenCV versions - not much, but enough to crash your app when you change from 2.0 to 2.2 or from 2.2 to 2.3.1. Also, the beta 2.4 release has enough changes from previous ones.
So best is to test your app with an OpenCV version, and deliver it with those dlls only.
A small example:
Mat a(3,3,CV_8UC3);
a.setTo( Scalar(10) ); // in 2.3.1 will set all channels to 10,
// in 2.2 will only set first channel.
The corresponding 2.2 call would be
a.setTo(Scalar::all(10));
Or
a = 0; // runs fine on 2.3.1. Equivalent to setTo().
// Does not compile on earlier versions
Another example is cv::drawPoly(), which has a different signature on 2.2 and 2.3.1.
Given the fact that those changes are not well documented, the chance to miss one of them by mistake is really high.
I have worked out a solution to having code run on various versions of OPENCV by merging the following articles:
How to make a string out of macros
Concatenating strings in macros
Pragma statements in #defines with support for VS2008 here on MSDN
Bonus: How to have global settings defined in a solution
As noted, this can cause some serious grief if you pop between incompatible versions but for those versions and features supported this may be useful to some. Use this technique to set the opencv version once and have your code automatically link to the desired version by either
define the OPENCV_VERSIONin one place in your source code
Define it in a Property Sheet
Define it in a system environment variable.
My code ended up like so:
#include <iostream>
// #define OPENCV_VERSION $(OPENCV_VERSION)
// #define OPENCV_VERSION 220
#define QUOTE(name) #name
#define STR(macro) QUOTE(macro)
#define LINK_TO_OPENCV(libname) __pragma(comment(lib, "opencv_" #libname STR(OPENCV_VERSION)))
#define LINK_TO_OPENCV_DEBUG(libname) __pragma(comment(lib, "opencv_" #libname STR(OPENCV_VERSION) "d"))
#ifndef NDEBUG
LINK_TO_OPENCV_DEBUG("core")
LINK_TO_OPENCV_DEBUG("highgui")
LINK_TO_OPENCV_DEBUG("imgproc")
#else
LINK_TO_OPENCV("core")
LINK_TO_OPENCV("highgui")
LINK_TO_OPENCV("imgproc")
#endif
int main()
{
std::cout << STR(LINK_TO_OPENCV("core")) << "\n";
return 0;
}
And now to set the OPENCV_VERSION anywhere you like. For example, you can have a single header file included by everyone that has the line:
#define OPENCV_VERSION 220
Or you can goto Project->Properties->C/C++->Preprocessor and set Preprocessor Definitions to OPENCV_VERSION=220. Or you can do the same thing in a shared property sheet for the entire solution.
Or, and this is important, you can use this technique to define a global environment variable in windows itself called OPENCV_VERSION_ENV and set its value to the version code (say, 220). Then you can put set the preprocessor definition to OPENCV_VERSION=$(OPENCV_VERSION_ENV) and you will bring in the environment variable into the link command. You CANNOT do a #define OPEN_VERSION $(OPENCV_VERSION_ENV) but since the property pages will automatically translate $(macros) we can get environment variables there.

getting the right compiler for C++

I am trying to learn c++ but most of the tutorials and books I have read or looked up teaches you this...
(I am assuming like most tutorials, they are teaching in the beginning to code either in win32 console or CLR console. In either case the following does not work.)
#include <iostream>
int main( )
{
std::cout << "Hello World\n";
return (0);
}
The IDE that i have is Visual C++ 2008 Express edition and they accept code like this
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
Or like this
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
return 0;
}
Honestly I do not no the difference in none of these and I am not sure if I should just download a older compiler so that it works. If someone can tell me what the difference in these are and where to go from there. That will help tremendously. Thanks
[Edited]
I am trying to do a simple hello world. But I get the error "system can not find path specified." I have screenshot that shows what the error looks like. It also is saying that my project is out of date when I clearly save the file before I build it. Apparently it can not find the executable file. I went to the debug fold and did not see any .exe file.
[Edited]
Ok, now When I try to build the project I get the following errors
1>------ Rebuild All started: Project: test, Configuration: Debug Win32 ------
1>Deleting intermediate and output files for project 'test', configuration 'Debug|Win32'
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>test.cpp
1>c:\users\numerical25\desktop\test\test\test.cpp(1) : warning C4627: '#include <iostream>': skipped when looking for precompiled header use
1> Add directive to 'stdafx.h' or rebuild precompiled header
1>c:\users\numerical25\desktop\test\test\test.cpp(6) : error C2653: 'std' : is not a class or namespace name
1>c:\users\numerical25\desktop\test\test\test.cpp(6) : error C2065: 'cout' : undeclared identifier
1>Build log was saved at "file://c:\Users\numerical25\Desktop\test\test\Debug\BuildLog.htm"
1>test - 2 error(s), 1 warning(s)
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
Here is the code I used
#include <iostream>
#include "stdafx.h"
int main( )
{
std::cout << "Hello World\n";
return (0);
}
Note: I tried using it with and without the #include "stdafx.h" When I tried it without the #include "stdafx.h", it said I might be missing it.
Not sure what you're asking. The first two examples you gave are valid C++ programs that should (will) compile with VC++. The third example is a C++/CLI program that must be compiled with the /CLR compiler switch (this is called Managed C++).
EDIT: Adding more specific information (from a comment below):
The first two examples are standard (native) C++ (albeit, the second example has MS-proprietary macros). They compile to native code. The third is C++/CLI (a "managed" extension to C++). It compiles to managed (.NET) code. Only the third snippet interacts with the .NET framework in any way. All three are absolutely buildable and runnable using the appropriate projects in VS 2008 (no command line necessary)!
Based on your latest update, it looks like you have probably modified some project properties and changed some paths. The app is building, but when you try to run it via VS (you should do this with <Ctrl>+F5, by the way), the executable cannot be found (there are several ways you could have messed this up by changing or playing with various settings).
Please note the difference between building and running. Building is the process of compiling and linking your source code. Running is launching the resulting executable. You seem to be confused between these (judging from your complaints about the "...out of date" dialog box). It is normal to get the "...out of date" dialog box if you try to run without rebuilding after you have made a change to the project (even if that change is saved). Just make sure you click "yes." You need to build the project before you can run it.
My recommendation is to completely delete your project and solution. Create a new empty project, as suggested elsewhere in this now-very-heavyweight thread, and don't modify any project settings. If this doesn't work, something is seriously wrong!
ANOTHER EDIT: Just for completion, since this question kept changing:
As others have already pointed out, your ultimate problem with the first snippet is the use of precompiled headers (PCH). PCH are turned on by default in new VS C++ projects. Their purpose is to speed compilation when many implementation files include the same set of headers -- preventing the compiler from having to parse the header files for each compilation unit.
You have three options:
(Recommended) Disable PCH -- Project Properties --> Configuration Properties --> C/C++ --> Precompiled Headers: Set Create/Use Precompiled Header to Not Using Precompiled Headers. (You don't need to do anything with the "stdafx.h" file or the #include for it.)
Place your commonly used #includes in "stdafx.h". In your case, you would put #include <iostream> in "stdafx.h".
Place your #includes after `#include "stdafx.h". Microsoft requires that the "stdafx.h" be the first included file in a compilation unit.
A minor point, which I don't see elsewhere in the answers: When using precompiled headers, such as your stdafx.h, you need to include them first. Change it to:
#include "stdafx.h"
#include <iostream>
and that should fix the errors about it.
Alternatively, it may be easier to simply switch off precompiled headers: Project > Properties > Configuration Properties > C/C++ > Precompiled Headers > Switch first option to "Not using precompiled headers". They can be useful for big projects but will just be awkward and annoying while you're learning, since they have extra rules (like this "must be included first") which aren't requirements of standard C++ .
The "difference" is pedantic. The latter are just Microsoft-specific entry points.
As you are learning C++, I recommend you use a compiler, and preferably an operating system that lets you focus on C++, and not the platform. For this I recommend g++, on an Linux distribution such as Ubuntu.
Try this tutorial, there are many others that are similar that quickly let you overcome being tied to the tools, and focus on C++.
int main();
int main(int argc, char* argv[]);
These are standard C++.
int _tmain(int argc, _TCHAR* argv[]);
int wmain(int argc, wchar_t* argv[]);
These are Windows-specific to handle Unicode arguments. See What is the difference between _tmain() and main() in C++?.
int main(array<System::String^>^ args);
This is not C++. This is C++/CLI.
For best portability, always use the first form.
Also,
int main(int argc, char** argv, char** envp);
This is a usually seen POSIX extension. Windows supports this form of main too. The envp means (pointer to) environment variables.
int main(int argc, char** argv, char** envp, char** apple);
This is for Mac only, obviously.
void main();
And this is wrong (nonstandard, some compilers (e.g. gcc) will reject it).
Visual C++ Express will compile the first example just fine.
However, you need to ensure the proper project settings:
Create an "Empty Project"
"Add a new item..." to the project via the "Project" menu. Select C++ (.cpp) file.
Copy/Paste code into new file
Press F5 to compile and run.
When "Project is out of date" dialog appears, press "Yes" (build the project)
The steps above ensure VC++ Express does not treat your file as a special Win32/Windows console application.
EDIT: added additional step 5 to prevent "Can't find..." dialog.
I managed to get the same dialog by making sure the exe file does not exist, and answering "No" to the build dialog. With a clean, empty project the exe file does not exist yet. It must be built first. If you answer "no" don't build it, VC++ dutifully does not build the exe and later complains about not being able to find it when it tries to run it later.
As STingRaySC pointed out, all three of your examples will compile in VC2008 express; it's just that examples 2 and 3 are what VC2008 Express will load up initially when you create a project (one of the examples is for Managed C++, as STingRaySC mentioned).
You can just delete the code in your second example (the C++ Win32 Console Application project) and paste in the more standard hello world program from your first example. It should compile and run just fine in VC2008 Express - it did for me.
I. Precompiled header
#include "stdafx.h"
is some kind of tricky stuff that comes your way.
If you create a project VC will normally switch on precompiled header.
This means that one header stdafx.h is created which is compiled only once.
This is done to speed up compile time in big environments. If you start C++
it will confuse you.
If you use stdafx.h it has to be the first header in the cpp file.
II. Unicode (Utf16)
int _tmain(int argc, _TCHAR* argv[])
Microsoft uses UTF16 to implement unicode strings.
This means you get two versions of main.
int main(int argc, char* argv[])
int main(int argc, wchar_t* argv[])
This is also confusing if you start.
To simply start you can use whatever editor you want.
Create the file.
Open a Visdual studio 2008 command prompt
cl main.cpp
main.exe
and you will see Hello World using code from books.
Afterwards try to understand some of the settings of VC.
But you should always use an empty project.
Else you have to care about stdafx, UNICODE, ...
_tmain with the _TCHAR argv is the way the C runtime allows you to handle unicode. If _UNICODE is defined, then _tmain will expand to wmain, and the _TCHAR argument will be of type wchar_t. If _UNICODE is not defined, then _tmain will expand to main, which will be the ANSI standard.
Therefore, so long as _UNICODE is not defined, the second snippet you posted is compliant with the standard.
Lots of waxing lyrical and some misinformation for you sift through already, but I suggest following wonsungi's advice. But to clarify his advice:
File->New->Project
Select Project Type "Win32", then Template "Win32 Console Project"
Give the project a name and location
OK
Select "Application Settings"
Check "Empty Project"
In the "Solution Explorer", right click the "Sources" folder, then Add->New Item
Type the name of the file, in the "name" box using a .cpp extension (you can ignore the templates if you wish).
Enter your code in the new file.
Woot!! I figured it out!!! Below is my original code
#include <iostream>
int main( )
{
std::cout << "Hello World\n";
return (0);
}
It was missing the header file #include "stdafx.h" . So I had to include it in there so I added it like this
#include <iostream>
#include "stdafx.h"
int main( )
{
std::cout << "Hello World\n";
return (0);
}
I was still getting an error like what you see in my edited question at the bottom. So What I did is I took #include and added it in my header file and then it worked!!!!!
Even the the books and alot of tutorials show to add #include to the actual cpp, for some reason in express edition I had to add it to header for it to work. I don't know WHY but it's a solution and now it works.
Download and install Dev-C++ on your system. If the code doesn't work on Visual C++, try it out on Dev-C++ (which uses the GCC compiler). You may get the same results or a different error message. Whenever you get an error message you don't understand, do a Internet search for the error message.