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.
Unsafe code can be used in Visual Studio IDE by following the given steps: Double click properties in the Solution Explorer to open project properties. Click the Build tab and select the option “Allow Unsafe Code”.
Create a file in your <Project Path>/Assets directory and name it smcs. rsp then put -unsafe inside that file. Save and close that file. Close and reopen Visual Studio and Unity Editor.
Unsafe is a C programming language (C#) keyword used to denote a section of code that is not managed by the Common Language Runtime (CLR) of the . NET Framework, or unmanaged code.
Unsafe code in C# is the part of the program that runs outside the control of the Common Language Runtime (CLR) of the . NET frameworks. The CLR is responsible for all of the background tasks that the programmer doesn't have to worry about like memory allocation and release, managing stack etc.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With