libcosim icon indicating copy to clipboard operation
libcosim copied to clipboard

Is it safe to disable MSVC compiler warning C4251?

Open kyllingstad opened this issue 6 years ago • 1 comments

In our CMakeLists.txt we specify the MSVC compiler switch /wd4251, which disables warning C4251. The warning is:

'identifier' : class 'type' needs to have dll-interface to be used by clients of class 'type2'

It was I who disabled it, and I think it is OK to do so, but nevertheless, it would be good if others could think of it too. It's kind of an ominous warning.

CSE Core (and CSE CLI) won't compile without it, at least not without other modifications.

kyllingstad avatar Jul 25 '19 11:07 kyllingstad

Here is my interpretation of what the message tells us:

  • identifier is a function that is being compiled by us. It could be our own function, but it could also be an inline function from some library header.
  • In that function, the classes type and type2 are being used together in some way.
  • The (compiled) code for type and type2's methods resides in one or two DLLs.
  • However, the interfaces of one or more of those methods aren't considered safe for DLL use, because they input or output nontrivial types.

That last point is where I think the problem lies, and why I think this is a warning in the first place: If the two DLLs are different, and they are compiled with different compiler settings (debug vs. release, say), then those nontrivial types could be interpreted and manipulated differently in the two DLLs. For example, different compiler settings could lead to different memory layouts. std::string would be a prime example of such a type.

The reason I think this is not a problem for us is that we are using a package manager that ensures (I hope!) that the compiler settings for all our dependencies are the same, and the responsibility is only on us to make sure that we use the same settings as the package manager.

Does this sound right to you?

kyllingstad avatar Jul 25 '19 11:07 kyllingstad