开发者

Compile CSProj in Visual Studio with /CLR:Safe? Fatal Error LNK1302: only support linking safe .netmodules

开发者 https://www.devze.com 2023-03-14 05:09 出处:网络
So I am having an issue converting an opensource project (SQLite) to use a different build system and now I\'m having trouble linking projects. Essentially I have a VCProj file that has a definition l

So I am having an issue converting an opensource project (SQLite) to use a different build system and now I'm having trouble linking projects. Essentially I have a VCProj file that has a definition like this:

<Tool
   Name="VCLinkerTool"
   AdditionalOptions="/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteCommand.bmp,System.Data.SQLite.SQLiteCommand.bmp&#x0D;&#x0A;/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteConnection.bmp,System.Data.SQLite.SQLiteConnection.bmp&#x0D;&#x0A;/ASSEMBLYRESOURCE:..\System.Data.SQLite\SQLiteDataAdapter.bmp,System.Data.SQLite.SQLiteDataAdapter.bmp"
   AdditionalDependencies="..\System.Data.SQLite\bin\System.Data.SQLite.netmodule"
   OutputFile="..\bin\System.Data.SQLite.DLL"
   GenerateManifest="false"
   IgnoreDefaultLibraryNames=""
   ModuleDefinitionFile="src\sqlite3.def"
   EmbedManagedResourceFile=""
   DelayLoadDLLs="advapi32.dll"
   RandomizedBaseAddress="1"
   DataExecutionPrevention="0"
   ImportLibrary=""
   KeyFile="..\System.Data.SQLite\System.Data.SQLite.snk"
   CLRUnmanagedCodeCheck="true"
/>

And I am unable to properly link the System.Data.SQLite.netmodule due to the following error:

Linking...

4>..\System.Data.SQLite\bin\System.Data.SQLite.netmodule : fatal error LNK1302: only support linking safe .netmodules; unable to link pure .netmodule

And the System.Data.SQLite.netmodule is generated from a C#/.NET project. How is it I can force it to compile with the /clr:safe option since according to MSDN this is the way to fix the issue. The problem is is that I don't know how to compile it with /clr:safe. How is it I can do that in the csproj file or in visual studio somewhere? If I am off base in my attempts to fix this please let me know a better way.

UPDATE: So I have determined the issue but I'm not sure why it is an issue. So the interop project (the C project which compiles the sqlite code and links) uses the VCLinkerTool to link to the System.Data.SQLite.netmodule. There is then a C# project that creates the System.Data.SQLite.netmodule using the following command (Anything in {} was added to reduce length):

C:\Windows\Microsoft.NET\Framework\v3.5\Csc.exe /noconfig /unsafe- /nowarn:1701,1702 /platform:AnyCPU /errorreport:pro开发者_StackOverflow社区mpt /doc:..\bin\System.Data.SQLite.XML {DLL REFEERENCFES /debug- /filealign:512 /optimize+ /out:obj\Release\System.Data.SQLite.netmodule {RESOURCES AND CS FILES}

The problem is with the /platform:AnyCPU. In my build it is /platform:x86 and for some reason this causes issues when linking and I'm not sure why but this is what I have narrowed it down to since I can change it to AnyCPU and it will build and link properly. Any insight on this is appreciated.


Ok so I finally narrowed down what the issue was and maybe someone can spread some more light onto why that is but I have a rough idea why. Basically you can't have the VCLinkerTool link to .netmodules that were created targetting the /platform:x86 (and I assume any other variant that isn't AnyCpu. I would assume this has to do with the way that the linking occurs internally when linking the mixed mode dll with a .netmodule. So it looks like for this to work you have to have your C# project compiled with /platorm:AnyCpu.


This may be what you are looking for:

To set this compiler option in Visual Studio

1. In Solution Explorer, right-click the project name, and then click Properties to open the project Property Pages dialog box.

2. Select the Configuration Properties folder.

3. On the General property page, modify the Common Language Runtime support property.

Note

When /clr is enabled in the Property Pages dialog box, compiler option properties that are not compatible with /clr are also adjusted, as required. For example, if /RTC is set and then /clr is enabled, /RTC will be turned off.

Also, when you debug a /clr application, set the Debugger Type property to Mixed or Managed only. For more information, see Project Settings for a C++ Debug Configuration.

For information about how the create a module, see /NOASSEMBLY (Create a MSIL Module).


You would have to edit the CSProj to not use features that cause the code to no longer be safe. These are easy to spot -- anywhere where you're using P/Invoke, or anywhere where there's an unsafe code block, that code is not safe. If any of the code in your assembly is unsafe, the whole assembly gets marked as unsafe.

If you remove the unsafe code the resultant assembly will automatically be safe.

(In the case of SQLite I believe this will be impossible, because SQLite is a C library, which cannot be compiled as safe)

0

精彩评论

暂无评论...
验证码 换一张
取 消