Connecting to DLL library? RRS feed

  • Question

  • Hi Folks:

       Developing on VS 2017 Community, Win 10 Pro, all C++, no MFC. 

       I sometimes use libraries from other sources.  I don't remember running into this problem. 

       I'm trying to link to the Botan crypto library.  I'm getting this warning when performing debug builds:

    warning C4275: non dll-interface class 'std::exception' used as base for dll-interface class 'Botan::Exception'

       The exception class in question:

    * Base class for all exceptions thrown by the library
    class BOTAN_DLL Exception : public std::exception
          explicit Exception(const std::string& msg) : m_msg(msg) {}
          Exception(const char* prefix, const std::string& msg) : m_msg(std::string(prefix) + " " + msg) {}
          const char* what() const BOTAN_NOEXCEPT override { return m_msg.c_str(); }
          std::string m_msg;

       I don't want to alter the Botan library's source.  I am open to changing parameters to its build script. 

       What do I need to do on my end, source code or build parameters, to avoid this warning?


    Thursday, July 12, 2018 9:14 PM

All replies

  • As I understand it the warning is caused because std::exception is not an exported class.  According to the discussion at https://social.msdn.microsoft.com/Forums/en-US/d3eb2bb6-03eb-4a2e-9c1a-94821905ba3f/warning-c4275-non-dllinterface-class?forum=vclanguage this warning may be safely ignored if the same compiler is used to build all modules with /MD specified so that they share the same runtime.

    • Proposed as answer by Baron Bi Friday, July 13, 2018 1:30 AM
    Thursday, July 12, 2018 10:45 PM
  • You have two options to disable the warning. First, you can wrap the include for the header(s) in:

    #pragma warning(push)
    #pragma warning(disable:4275)
    #include <header>
    #pragma warning(pop)

    this will disable that warning for only the header(s) that you wrap.

    The other option is to disable the warning via the compiler settings. This is something not recommended at all, because it will disable all instances of this warning.

    But remember that this warning is because of the potential data corruption that can occur in this kind of situation. There shouldn't be any scope for std::exception going wrong since construction will call the constructors exported by the DLL, and this should then call the std::exception constructors through the DLL. The what member is virtual and only accesses std::string through the DLL interface, and the destructor is virtual, so the Exception constructor will always be called, and this will destroy the std::string through the DLL, and then call the DLL's copy of ~exception.

    If you want to be safe though, make sure that you build everything using the DLL version of the runtime (/MD for release and /MDd for debug). I shouldn't have to tell you to use the same version of the compiler, since you can't use /MD or /MDd successfully if you don't.

    This is a signature. Any samples given are not meant to have error checking or show best practices. They are meant to just illustrate a point. I may also give inefficient code or introduce some problems to discourage copy/paste coding. This is because the major point of my posts is to aid in the learning process.

    Friday, July 13, 2018 1:42 AM