SergiusTheBest / plog

Portable, simple and extensible C++ logging library
MIT License
2.19k stars 385 forks source link

"Shared" example not working on Windows/MinGW #245

Open TeknoVenus opened 1 year ago

TeknoVenus commented 1 year ago

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;

Now build and run the sample

$ ls samples/Shared/
cmake_install.cmake  CMakeFiles  libSharedApp.dll.a  libSharedLib1.dll  libSharedLib1.dll.a  libSharedLib2.dll  libSharedLib2.dll.a  SharedApp.exe
$ ./SharedApp
$ 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

$ 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.
SergiusTheBest commented 1 year ago

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

SergiusTheBest commented 1 year ago

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:

TeknoVenus commented 1 year ago

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.