vimpunk / mio

Cross-platform C++11 header-only library for memory mapped file IO
MIT License
1.71k stars 157 forks source link

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

Open rost0031 opened 3 years ago

rost0031 commented 3 years ago

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.

cculianu commented 3 years ago

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

kerim371 commented 2 years ago

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

Green-Sky commented 10 months ago

fixed by #88