vimpunk / mio

Cross-platform C++11 header-only library for memory mapped file IO

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

rost0031 opened this issue · 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.

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

commented

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

fixed by #88