Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compile a solution that uses unsafe code?

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.

like image 211
BartoszKP Avatar asked Sep 27 '13 12:09

BartoszKP


People also ask

How do I allow unsafe code?

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”.

How do I allow unsafe code unity?

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.

What does unsafe code mean?

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.

What is unsafe code in C# with example?

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.


1 Answers

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.

like image 153
BartoszKP Avatar answered Sep 26 '22 00:09

BartoszKP