1

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.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
Buse
  • 45
  • 6
  • 3
    Do you need to compile all your code as c++-cli? Can't you leave most of it as c++ with a small cli wrapper? – Alan Birtles Oct 17 '22 at 12:09
  • You will only need a small C# wrapper, only expose what you really need to interact with. I think future maps pretty well to a C# Task. So make a wrapper that creates a managed task which interacts with the underlying C++ future and return that. – Pepijn Kramer Oct 17 '22 at 12:23
  • @PepijnKramer I can see what you are proposing, only problem with this is, that in the library, there is a class "router" which also needs to schedule some asynchronous events. There is a "runtime" which schedules those events. But maybe the runtime could be rewritten so that it uses C# Task as you are saying. I will give it a try. – Buse Oct 17 '22 at 14:09
  • @AlanBirtles I have tried compiling only parts that I needed as C++/CLI, but I always got to the point that due to some includes, the code needed ``. – Buse Oct 17 '22 at 14:11
  • 1
    My idea was : 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 – Pepijn Kramer Oct 17 '22 at 15:07
  • @PepijnKramer I followed your suggestion and it seems it will work this way. One of my problems was that the macros `#pragma managed(push, off)` and `#pragma managed(pop)` seem not to work reliably with header files. Best approach to set if file is built with clr support or not seems to be via Properties->C/C++->Common Language Runtime Support (/clr) on individual files. If you would make your comment to an answer, I will accept it as solution. – Buse Oct 19 '22 at 11:59
  • Nice to hear you managed to make progress. – Pepijn Kramer Oct 19 '22 at 13:20

1 Answers1

0

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

Pepijn Kramer
  • 9,356
  • 2
  • 8
  • 19