diff --git a/library/CMakeLists.txt b/library/CMakeLists.txt index 416e42c41..d3bff72df 100644 --- a/library/CMakeLists.txt +++ b/library/CMakeLists.txt @@ -57,6 +57,7 @@ include/wdirent.h SET(MAIN_SOURCES Core.cpp ColorText.cpp +CompilerWorkAround.cpp DataDefs.cpp Debug.cpp Error.cpp diff --git a/library/CompilerWorkAround.cpp b/library/CompilerWorkAround.cpp new file mode 100644 index 000000000..402553bd8 --- /dev/null +++ b/library/CompilerWorkAround.cpp @@ -0,0 +1,32 @@ +#include + +namespace DFHack { +namespace neverCalled { + +/** + * gcc/linstdc++ seems to generate code that links libstdc++ back to first + * shared object using std::regex. To allow plugins unload with std::regex in + * the code we need the std::regex functions inside libdfhack.so. + * + * If your plugin decides to use any overloads that aren't listed here it may + * stay in memory after dlclose. + */ +std::regex stdRegexPluginUnloadWorkaround() +{ + std::regex fake("foo"); + std::string haystack("bar is foo in the world"); + std::regex fake2(std::string("bar")); + if (std::regex_match(haystack, fake)) + std::swap(fake, fake2); + if (std::regex_search(haystack, fake)) + std::swap(fake, fake2); + const char* haystack2 = "foo"; + if (std::regex_match(haystack2, fake)) + std::swap(fake, fake2); + if (std::regex_search(haystack2, fake)) + std::swap(fake, fake2); + return fake; +} + +} +}