OpenZWave / open-zwave

a C++ library to control Z-Wave Networks via a USB Z-Wave Controller.
http://www.openzwave.net/
GNU Lesser General Public License v3.0
1.05k stars 918 forks source link

Doesn't compile on Windows #2623

Open kdschlosser opened 2 years ago

kdschlosser commented 2 years ago

First error is

openzwave\cpp\src\command_classes\Supervision.cpp(56): error C7555: use of designated initializers requires at least '/std:c++20'

startig at line 55 in Supervision.cpp you have the following code.

m_sessions.push_back({
    .session_id = m_last_session_id, 
    .command_class_id = _command_class_id,
    .index = _index
});

I am not that proficient in C code bit I am guessing that the use of = in the initialize requires c++20.

So I add /std:c++20 to the command line arguments for cl.exe and then I get the next error.

openzwave\cpp\src\Utils.cpp(170): error C2440: 'static_cast': cannot convert from '_Ostr' to 'std::ostringstream &'

and starting at line 167 in Utils .cpp you have the following code

#if __cplusplus==201103L || __APPLE__
            return to_string(x);
#else
            return static_cast< std::ostringstream & >( ( std::ostringstream() << std::dec << x ) ).str();
#endif

I do want to mention that when I do not set the compiler flag to c++20 cl.exe defaults to c++14 and Utils.cpp compiles just fine

changing the code in Utils.cpp to read

#if __cplusplus==201103L || __cplusplus>=202002L || __APPLE__
            return to_string(x);
#else
            return static_cast< std::ostringstream & >( ( std::ostringstream() << std::dec << x ) ).str();
#endif

and setting the /std:c++20 compiler flag and also /Zc:__cplusplus solves the problem.

The /Zc:__cplusplus flag instructs cl.exe to set the correct standard version to the __cplusplus macro. If that flag does not get used the __cplusplus macro will default to 199711L (c++11). kind of dumb that it works his way but it is Microsoft that made it.