mio icon indicating copy to clipboard operation
mio copied to clipboard

Function std::wstring s_2_ws(const std::string& s) needs to be declared inline to prevent GCC linking errors

Open rost0031 opened this issue 3 years ago • 3 comments

Windows 7, mingw (MSYS2) gcc 10.2.0

in mio.hpp, line 798:

std::wstring s_2_ws(const std::string& s)
{
    if (s.empty())
        return{};
    const auto s_length = static_cast<int>(s.length());
    auto buf = std::vector<wchar_t>(s_length);
    const auto wide_char_count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s_length, buf.data(), s_length);
    return std::wstring(buf.data(), wide_char_count);
}

The function above would compile with older versions for GCC (9.3.0 on kunbuntu 20.04) but fails to compile in above listed Windows7 environment with an error during linking:

multiple definition of  `mio::detail::win::s_2_ws(std::__cxx11:basic_string<char, std::char_traits<char>, std::allocator<char> > const &);

... 
first defined here

and it lists the same location in the hpp file.

The solution is simple: make the function s_2_ws inline. That gets rid of the error.

inline std::wstring s_2_ws(const std::string& s)
{
    if (s.empty())
        return{};
    const auto s_length = static_cast<int>(s.length());
    auto buf = std::vector<wchar_t>(s_length);
    const auto wide_char_count = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s_length, buf.data(), s_length);
    return std::wstring(buf.data(), wide_char_count);
}

The problem exists in both the single include as well as regular ipp file.

rost0031 avatar Apr 13 '21 22:04 rost0031

I'm having the same issue also building with mingw gcc. Thanks for the tip on how to fix it.

cculianu avatar May 13 '21 18:05 cculianu

Thank you, the same error I got (on Windows though)

kerim371 avatar Mar 10 '22 03:03 kerim371

fixed by #88

Green-Sky avatar Dec 13 '23 14:12 Green-Sky