plog icon indicating copy to clipboard operation
plog copied to clipboard

"Shared" example not working on Windows/MinGW

Open TeknoVenus opened this issue 2 years ago • 3 comments

The example provided in Shared does not work correctly when built with GCC using MinGW64 - the log output from the two shared libraries is never printed.

I've attempted to provide some instructions on how to reproduce, let me know if you have any issues.

Steps to Reproduce

If you don't have a MinGW environment;

  • Install MSYS2 from here: https://www.msys2.org/ if you don't have it installed
  • Launch a MINGW64 shell
  • Install relevant packages from pacman to get a build env if you don't have one
    • pacman -S mingw-w64-x86_64-cmake mingw-w64-x86_64-gcc mingw-w64-x86_64-ninja

Now build and run the sample

  • Clone Plog from master branch
  • Important: Edit the SharedApp/main.cpp to rename SharedLib1.dll and SharedLib2.dll in the LoadLibraryW calls to libSharedLib1.dll and libSharedLib2.dll (since cmake will prefix dll names with lib in this environment)
  • Build samples
$ mkdir build && cd build
$ cmake -DPLOG_BUILD_SAMPLES=1 ..
$ cmake --build .
  • Check shared app build as expected
$ ls samples/Shared/
cmake_install.cmake  CMakeFiles  libSharedApp.dll.a  libSharedLib1.dll  libSharedLib1.dll.a  libSharedLib2.dll  libSharedLib2.dll.a  SharedApp.exe
  • Run SharedApp.exe
$ ./SharedApp
  • Check the resulting log file
$ cat Shared.txt
2023-04-01 17:53:59.295 DEBUG [6804] [main@30] Hello from app!

The log messages from the two libraries is missing

Expected Behaviour

The shared libraries will import the Plog instance and print the expected log messages

Notes

Check the exported symbols from SharedApp.exe with Dependencies (https://github.com/lucasg/Dependencies), we can see SharedApp.exe does export the plog symbols:

2023-04-01 17_57_47-Dependencies (WoW64)

So the symbols are correctly being exported, but the shared libraries are not importing them correctly.

Calling plog::Logger<0>::getInstance(); in one of the shared libraries returns a null pointer.

Environment

  • MINGW64 installed with MSYS2
  • GCC 12.2 compiler
$ gcc --version
gcc.exe (Rev6, Built by MSYS2 project) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  • CMake 3.25.1
  • Plog version d60df3a1efa99061f637c263fe4e467c273d8655
    • Same behaviour with last tagged release 1.1.9

TeknoVenus avatar Apr 01 '23 17:04 TeknoVenus

Your bug report is very good! I'll check this issue.

SergiusTheBest avatar Apr 03 '23 16:04 SergiusTheBest

Unfortunately MinGW doesn't play well with exporting/importing classes and inline functions (and they should be inline as they are in headers). I don't see an easy way to fix this.

However you can achieve the similar functionality with the Chained example.

Related issues that have interesting details:

  • https://github.com/skypjack/entt/issues/719
  • https://sourceforge.net/p/mingw-w64/bugs/906/

SergiusTheBest avatar Apr 05 '23 18:04 SergiusTheBest

Thanks for looking into this, really appreciate it.

Looks like the chained example will work fine for my needs, so can use that instead.

Might be worth adding a note in the readme that sharing the log instance across libs won't work properly in MinGW.

TeknoVenus avatar Apr 06 '23 13:04 TeknoVenus