yaml-cpp icon indicating copy to clipboard operation
yaml-cpp copied to clipboard

DLL interface classes have data members from C++ standard library causing C4251 when building DLL

Open abdes opened this issue 7 years ago • 2 comments

Many classes in the yaml-cpp API such as in binary.h, ostream-wrapper.h etc... are exported as part of the DLL interface while having members of types from the STL. This causes multiple C4251 warnings on MSVC.

Example:

yaml-cpp\include\yaml-cpp\node\detail\node_data.h(122): warning C4251: 'YAML::detail::node_data::m_undefinedPairs': class 'std::list<YAML::detail::node_data::kv_pair,std::allocator<_Ty>>' needs to have dll-interface to be used by clients of class 'YAML::detail::node_data' [yaml-cpp\yaml-cpp.vcxproj]
          with
          [
              _Ty=std::pair<YAML::detail::node *,YAML::detail::node *>
          ]
yaml-cpp\include\yaml-cpp\node\detail\node_data.h(121): note: see declaration of 'std::list<YAML::detail::node_data::kv_pair,std::allocator<_Ty>>'
          with
          [
              _Ty=std::pair<YAML::detail::node *,YAML::detail::node *>
          ]

STL code is not DLL ABI friendly and it is not recommended to have DLL interface classes having members of types from STL as the behavior is not predictable.

abdes avatar Nov 20 '18 13:11 abdes

C:\SDKs\vcpkg\installed\x64-windows\include\yaml-cpp\parser.h(81): warning C4251: 'YAML::Parser::m_pScanner': class 'std::unique_ptr<YAML::Scanner,std::default_delete<_Ty>>' needs to have dll-interface to be used by clients of class 'YAML::Parser' with [ _Ty=YAML::Scanner ] C:\SDKs\vcpkg\installed\x64-windows\include\yaml-cpp/parser.h(81): note: see declaration of 'std::unique_ptr<YAML::Scanner,std::default_delete<_Ty>>' with [ _Ty=YAML::Scanner ] C:\SDKs\vcpkg\installed\x64-windows\include\yaml-cpp\parser.h(82): warning C4251: 'YAML::Parser::m_pDirectives': class 'std::unique_ptr<YAML::Directives,std::default_delete<_Ty>>' needs to have dll-interface to be used by clients of class 'YAML::Parser' with [ _Ty=YAML::Directives ] C:\SDKs\vcpkg\installed\x64-windows\include\yaml-cpp/parser.h(82): note: see declaration of 'std::unique_ptr<YAML::Directives,std::default_delete<_Ty>>' with [ _Ty=YAML::Directives ] C:\SDKs\vcpkg\installed\x64-windows\include\yaml-cpp\binary.h(61): warning C4251: 'YAML::Binary::m_data': class 'std::vector<uint8_t,std::allocator<_Ty>>' needs to have dll-interface to be used by clients of class 'YAML::Binary' with [ _Ty=uint8_t ] C:\SDKs\vcpkg\installed\x64-windows\include\nlohmann/json.hpp(18807): note: see declaration of 'std::vector<uint8_t,std::allocator<_Ty>>' with [ _Ty=uint8_t ] C:\SDKs\vcpkg\installed\x64-windows\include\yaml-cpp\ostream_wrapper.h(45): warning C4251: 'YAML::ostream_wrapper::m_buffer': class 'std::vector<char,std::allocator<char>>' needs to have dll-interface to be used by clients of class 'YAML::ostream_wrapper' C:\SDKs\vcpkg\installed\x64-windows\include\nlohmann/json.hpp(4056): note: see declaration of 'std::vector<char,std::allocator<char>>' C:\SDKs\vcpkg\installed\x64-windows\include\yaml-cpp\emitter.h(125): warning C4251: 'YAML::Emitter::m_pState': class 'std::unique_ptr<YAML::EmitterState,std::default_delete<_Ty>>' needs to have dll-interface to be used by clients of class 'YAML::Emitter' with [ _Ty=YAML::EmitterState ] C:\SDKs\vcpkg\installed\x64-windows\include\yaml-cpp/emitter.h(125): note: see declaration of 'std::unique_ptr<YAML::EmitterState,std::default_delete<_Ty>>' with [ _Ty=YAML::EmitterState ]

Zingam avatar Feb 11 '19 20:02 Zingam

Stucked at this problem on Release linkage. Previously defined YAML_CPP_STATIC_DEFINE in one of the header files hoping and totally forgot about this stuff. So other files who individually included yaml-cpp/yaml.h knew nothing about the definition. Had to define YAML_CPP_STATIC_DEFINE in the build file to avoid such failures in the future.

So if anyone gets such errors during linkage, here is a possible solution: define YAML_CPP_STATIC_DEFINE in yours build system or just pass it as a command line argument :)

newohrim avatar Nov 20 '24 23:11 newohrim