KratosMultiphysics/CoSimIO

Using CoSimIO in different TU / from other (linked) libraries

philbucher opened this issue · 1 comments

This concerns the following line:

static std::unordered_map<std::string, std::unique_ptr<Connection>> s_co_sim_connections;

I played around and here is my findings:

  • static is internal linkage, i.e. we can include it in different TU (see also https://docs.microsoft.com/en-us/cpp/cpp/program-and-linkage-cpp?view=msvc-160)
  • when using static and including it from different libraries (e.g. Core and CoSimApp to use Kratos terms) then s_co_sim_connections is different in the two, this is the current situation and does not work!
  • without static it works, but then I cannot include CoSimIO multiple times in different TU within the same exe/lib
  • For this to work too I need to use extern in the following way:
    #ifdef CO_SIM_IO_EXTERN
    extern std::unordered_map<std::string, std::unique_ptr<Connection>> s_co_sim_connections;
    #else
    std::unordered_map<std::string, std::unique_ptr<Connection>> s_co_sim_connections;
    #endif
    => now I can include CoSimIO (in one TU) ONCE regularly, the other times with CO_SIM_IO_EXTERN like this:
    #define CO_SIM_IO_EXTERN
    #include "co_sim_io.hpp"

I didn't yet check with more complex dependencies

properly fixed in #189