Search code examples
c++boostc++-cli

C++/CLI usage with unmanaged code - Boost, <future>


I am trying to compile a C++ library for use with C# on Windows (the library is Vanetza). The library uses CMake, I am building from Microsoft Visual Studio Community 2022 (64-bit). I can build the library as shared DLL no problem. For use with C#, I thought of compiling the library as C++/CLI (common language runtime support enabled). At first, I could not compile it because the project uses Boost, but some compilation issues were solved by surrounding every Boost include with #pragma managed(push, off) and #pragma managed(pop). Issue which blocks me now is that <future> header cannot be used with C++/CLI - I am getting this compilation error from lot of different places:

Severity    Code    Description Project File    Line    Suppression State
Error   C1189   #error:  <future> is not supported when compiling with /clr or /clr:pure.   geonet  C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.33.31629\include\future    13  

and I cannot solve this. I think that some Boost module that is used needs this header. Is there any solution to this or is there another approach that could be used? I need not only to get to some C++ functions of the library, but also to use some of its classes and to see some of its structures.

I tried compiling the library as normal C++ and then make an interfacing C++/CLI library which would be called from C#, but the header is again used somewhere. I think that using P/Invoke (DLLImport) is impractical for me as it is only making possible to use C/C++ functions, but for classes and structures it cannot be used (as I understand it).

Is only possible solution to rewrite the parts that need <future> myself using C# constructs as mentioned in this question?

As per other similar questions, it seems that sometimes the project can be built such that Boost works without any problems and sometimes major code rewrite is needed. Could someone recommend me what would a usable approach would be in my case? I could use older Boost version (namely 1.58.0), but I do not know if it would help.


Solution

  • Write a class in c++/cli that connects to your C++ code. The code will call the router which I presume will return a future. Then in manged/cli you create a task that hooks up to that future. So you have the original code compile to a static C++ lib and link that into your managed C++/cli dll