How to compile a solution that uses unsafe code?
Asked Answered
K

1

12

How to compile a Visual Studio 2010 solution using xbuild that uses unsafe code?

Currently I get this error:

Implementation/MaximumImageColorClassifier.cs(35,13): error CS0227: Unsafe code requires the `unsafe' command line option to be specified

But running:

xbuild -unsafe [solution file]

or:

xbuild /unsafe [solution file]

Gives an error:

MSBUILD: error MSBUILD0004: Too many project files specified

xbuild help shows that that's the proper syntax:

xbuild /? ... xbuild [options] [project-file]

But there is no unsafe option, and no "generic" option to provide some compilation flags.

I saw this question: Can I pass an argument to msc through xbuild on the command line? but it has no definite answer and maybe there is a specific workaround for unsafe code?

Also, in my case xbuild is using dmcs compiler.

Kleenex answered 27/9, 2013 at 12:46 Comment(0)
K
27

Eventually, I've found the solution myself. It is to put:

    <AllowUnsafeBlocks>true</AllowUnsafeBlocks>

in csproj file, under appropriate PropertyGroup (related to the configuration xbuild is using to build the solution). It is correctly interpreted and unsafe code compiles fine.

Kleenex answered 27/9, 2013 at 16:15 Comment(2)
where exactly do you add this?Accepted
@Accepted You should have a PropertyGroup tags for release and debug builds (and possibly some other build configurations you have) in your csproj file. It should go as a child element of each of these.Kleenex

© 2022 - 2024 — McMap. All rights reserved.