0

I am trying to compile a simple CLR project, which has no dependency.

When I compile this project I get 973 warnings (C4945) stating that

c:\windows\microsoft.net\framework\v4.0.30319\system.dll : warning C4945: 'xxx' : cannot import symbol from 'c:\windows\microsoft.net\framework\v4.0.30319\system.dll': as 'xxx' has already been imported from another assembly 'System'

As I mentioned that my project has no dependency on other projects, I tried to compile my cpp files one by one, starting with stdafx.cpp.

While doing this I noticed that if I include #include <msclr\marshal.h> I get those warnings, and if I don't there are no warnings.

Now I have following queries.

  1. Is #include <msclr\marshal.h> deprecated and replaced by something else?
  2. If no, how can I remove those warnings? Is #pragma warning disable the only way?
dhiraj suvarna
  • 505
  • 7
  • 20
  • Show us the code for a minimum reproducible sample! – xMRi Aug 22 '17 at 12:09
  • Create a new CLR project using Visual Studio 2012. `File -> New Project -> CLR -> Class Library` In the `stdafx.h` include `#include ` and compile it. Let me know, if this helps or let me know how can i share a sample – dhiraj suvarna Aug 22 '17 at 12:56
  • I have only VS-2015 and I can't reproduce this. No errors no warnings. – xMRi Aug 22 '17 at 13:28
  • Can you change the Platform Toolset in Project Properties and check it? – dhiraj suvarna Aug 22 '17 at 13:47
  • No. I don' t have this toolset installed. Even setting the toolset to VS-2013 (the oldest one I have installed) shows no warnings or errors. – xMRi Aug 22 '17 at 13:50
  • Thank You @xMRi. Appreciate your efforts, may be someone else can help. :) – dhiraj suvarna Aug 22 '17 at 13:52
  • Marshal.h is but the canary in the coal mine, it contains a `#using ` directive. These reference assemblies must come from c:\program files\reference assembles, not from c:\windows\microsoft.net. So the compiler is using the wrong `/AI` option. That's a pretty critical problem, using the wrong reference assemblies can cause [very nasty runtime problems](https://stackoverflow.com/questions/13748055/could-not-load-type-system-runtime-compilerservices-extensionattribute-from-as). I only remember that VS2012 was prone to this problem, not something I can check anymore. – Hans Passant Aug 23 '17 at 11:48
  • @hans so what do you suggest? – dhiraj suvarna Aug 23 '17 at 22:11
  • Keep your VS version updated so people can help you. – Hans Passant Aug 23 '17 at 22:25
  • I would have, if I was the decision maker. I work for my bosses. Think I will have to deal with this. – dhiraj suvarna Aug 24 '17 at 09:17
  • @HansPassant: On the other hand, should I use something else to convert between different strings in CLR other than marshal? – dhiraj suvarna Aug 24 '17 at 17:06

1 Answers1

0

This is still a problem for VS2019, but you can force the header to skip #using <System.dll> like this:

#pragma push_macro("_CRT_WINDOWS")
#define _CRT_WINDOWS
#include <msclr\marshal.h>
#pragma pop_macro("_CRT_WINDOWS")

This works for the other marshal headers as well (marshal_cppstd.h, etc.)

SStewart
  • 1
  • 1