2

In C# projects (.csproj files), it is possible to treat all warnings as errors except a specific set of warnings. Typically, this makes sense for obsolete/deprecated warnings, as these are there specifically to not break a build while announcing an upcoming API change.

Is the same also possible somehow for C++/CLI projects, i.e. in .vcxproj files?

(I am referring to seeing C4947 as a warning despite all other warnings being treated as errors.)

An answer from 2017 suggests it is not possible for plain C++ projects compiled by Microsoft's compiler by means of a compiler switch. However, I am hoping for the .vcxproj Xml format to support some element that causes such a behaviour.

I have tried inserting the <WarningsNotAsErrors>4947</WarningsNotAsErrors> element from the .csproj file into the .vcxproj file, but that won't help. Also, seeing that <TreatWarningsAsErrors> from the .csproj file is called <TreatWarningAsError> in .vcxproj (i.e. singular instead of plural), I tried <WarningNotAsError>4947</WarningNotAsError>, but to no avail.

I have read somehints about treating only specific warnings as errors, but this, of course, is not a viable solution. There are hundreds, if not thousands, of warnings (possibly even more in future compiler versions), all of which I want to treat as errors, except for a single one.

O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114

1 Answers1

-1

We don't yet a have a complete and perfect answer to this. This is the best it seems we can do.

Have been wanting to do this as well, though more generally for C++.

Mainly for deprecating functions just like this post here, which has a couple of suggested workarounds.

https://social.msdn.microsoft.com/forums/vstudio/en-US/88ffa3e4-1ad4-410a-b50b-d6575cb914bd/override-wx-behavior-using-pragma-warning

Not sure whether it suits your situation.

Here is the best I can find to emit a message when the deprecated function is used, which if double clicking on will take you to that location. It's still not a warning as far as the compiler is concerned though.

int my_function(int an_arg);

#define STRINGIFY_X(x) #x
#define STRINGIFY(x) STRINGIFY_X(x)
#define DepFunc(Func) __pragma(message( __FILE__ "(" STRINGIFY(__LINE__) "): warning C4996: '" STRINGIFY(Func) "': My message about deprecated function." )) Func

#define my_function DepFunc(my_function)

// Can push and undefine so we don't get the message when we define the function
#pragma push_macro("my_function")
#undef my_function 

int my_function(int an_arg)
{
    return an_arg + 2;
}

#pragma pop_macro("my_function")

int some_situation()
{
    // use it somewhere
    return my_function(1); // line 50
}

Get message like this with the line where it is used.

C:\ ..\myFile.cpp(50): warning C4996: 'my_function': My message about deprecated function.
Malcolm
  • 56
  • 4