emilk / loguru

A lightweight C++ logging library
The Unlicense
1.77k stars 256 forks source link

Compile error when cross-compiling for Windows using MinGW-W64 #128

Open rubenwardy opened 4 years ago

rubenwardy commented 4 years ago

Hey, thanks for making this library! I thought I'd update it today, but I now have this compile error when compiling for Windows:

loguru.cpp:(.text+0x2e97): undefined reference to `__imp__dupenv_s'

I include loguru.hpp like this:

#define LOGURU_WITH_STREAMS 1
#define LOGURU_THREADNAME_WIDTH 6
#include <loguru.hpp>

I've modified loguru.cpp to include the above file rather than loguru.hpp, in order to get the correct settings

The Windows build environment can be reproduced here: https://gitlab.com/rubenwardy/docker-sfml-rvwp/blob/master/install_mingw.sh

Toolchain:


set(CMAKE_SYSTEM_NAME Windows)

set(TOOLCHAIN_PREFIX x86_64-w64-mingw32)

# cross compilers to use for C and C++
set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}-gcc)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}-g++)
set(CMAKE_RC_COMPILER ${TOOLCHAIN_PREFIX}-windres)

SET( CMAKE_EXE_LINKER_FLAGS  "${CMAKE_EXE_LINKER_FLAGS} -static-libgcc -static-libstdc++" )

# target environment on the build host system
#   set 1st to dir with the cross compiler's C/C++ headers/libs
set(CMAKE_FIND_ROOT_PATH /usr/${TOOLCHAIN_PREFIX} /usr/local/mingw64 ./extlibs)

set(LUA_INCLUDE_DIR /usr/local/mingw64/include/lua5.1/)
set(LUA_LIBRARY /usr/local/mingw64/lib/liblua5.1.a)
set(OPENAL_LIBRARY /usr/local/mingw64/lib/libopenal32.a)

# modify default behavior of FIND_XXX() commands to
# search for headers/libs in the target environment and
# search for programs in the build host environment
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)
rubenwardy commented 4 years ago

https://github.com/emilk/loguru/blob/2e1424d575e276e010c072967c0672863c623cbf/loguru.cpp#L662-L665

This issue appears to be introduced by https://github.com/emilk/loguru/commit/86b8c648ff7cf2c49ed4c6c64e413fa61a420225

emilk commented 4 years ago

Windows compilations proves to be an ongoing issue for Loguru. Microsoft has in their infinitesimal wisdom decided to take standard function names and deprecate them and/or rename them, and do so differently for different version of their compiler. We seem to be stuck in a loop of "here is a PR that fixes Windows compilation for me, but breaks it for someone else". We either need a better CI with many versions of MSVC, or a dedicated champion for getting Loguru to work well on Windows.

Since I I don't even have a Windows installation I will just watch on from the sidelines and merge PRs that people send in. So long as they don't break the Mac/*nix builds, I'm good :)

rubenwardy commented 4 years ago

I also don't have Windows, my technique for supporting it is a mixture of CI and praying

I've fixed my CI build by reverting the commit I linked, the old getenv version compiles fine. When I need to test the Windows version or give it to people then I'll have to fix the issue properly. My project is a personal project so may never happen :)

Lazzu commented 4 years ago

I'm getting this fixed on MinGW-W64 when I replace line 661 with #ifdef _MSC_VER so it only detects if it's compiling it on MSVC.

ambarishsatheesh commented 4 years ago

I'm getting this fixed on MinGW-W64 when I replace line 661 with #ifdef _MSC_VER so it only detects if it's compiling it on MSVC.

Thanks, this also fixed the issue for me too

marknovose commented 4 years ago

I'm getting this fixed on MinGW-W64 when I replace line 661 with #ifdef _MSC_VER so it only detects if it's compiling it on MSVC.

Thanks, this solved it for me.