1

I am trying to set my settings in Visual Studio such that I can write managed C++ code to be called later by a C# program. I start with a new C++ project and here are the settings that I have changed and why:

  • Precompiled Headers to not using precompiled headers. As I just would rather not.
  • Common Language Runtime Support to /clr. As I understand it this is fundamentally needed.
  • Debugging Information Format from /ZI to none. As /clr and /ZI are incompatible
  • Enable minimal rebuild to no. As /clr and /Gm are incompatible.
  • Basic runtime checks to default. As /clr and /RTC1 are incompatible.

After all this I now come across many C2760 error codes located in xlocale or xlocnum. The error specifically states

syntax error: unexpected token 'identifier', expected 'catch'

Now, I am an extreme beginner here so maybe this is trivial and if so I apologize. My goal is to write a simple c++ class, wrap it with managed code, and have a DLL that I can link with a c# program later.

When I look at the MSDN Documentation page, all they say is to compile with /clr and /LD. Well there is no /LD option that I am able to find either, and I know that the /LD option is to create the DLL which is important. Any help or guidance would be greatly appreciated.

P.S. This is Visual Studio Community 2017

PaviniMan
  • 41
  • 2
  • You are an extreme beginner, and want to write managed C++, and call it from C#. You've set yourself quite the learning challenge! You may find this Q&A useful in your endeavor: https://stackoverflow.com/questions/5010957/call-function-from-dll – Eljay Feb 10 '18 at 15:27
  • If your only goal is to share code between multiple .NET projects, a C# class library will work nicely and is much easier to accomplish. – NightOwl888 Feb 10 '18 at 17:15
  • @NightOwl888 my ultimate goal is really just to learn how it works. For example if a complicated class already exists in one language that can follow .Net framework, it might be easier to wrap that class rather than re-write the entire class. Does that make sense? – PaviniMan Feb 10 '18 at 17:39
  • Yes it makes sense if you have the time to learn and want to add it to your skill set. Certainly, consuming 3rd party C++ libraries to reuse functionality is easier than building your own C++ libraries. Just wanted to make sure you realize there is a much quicker way if you had a tight deadline. – NightOwl888 Feb 10 '18 at 17:48

1 Answers1

0

Truth is: you have to have 2 projects.

  1. Just old good C/C++ project. You may put a 3rd-party library here like OpenSSL or any other cross-platform source. Here you may have some challenge to remove/wrap any platform-specific code.
  2. Windows Runtime Component - this is exactly CLI code that acts like bridge between native C/C++ and managed C#. You can find this at File -> New project, then Visual C++ -> Windows Universal. General idea about the Runtime Component is you can seamlessly import CLI methods by signature into C# code, you can call C# callbacks right at the CLI code, all C#/CLI types are binary-compatible etc. This is why you can not "just add specially configured C/C++ project".
Yury Schkatula
  • 5,291
  • 2
  • 18
  • 42