Compiling C# code with Windows Forms in Linux - c#

I downloaded a few source codes containing C# code that uses windows forms. Is it possible to compile the codes under the Linux environment (Ubuntu 12.04) and run it?
I already tried successfully to compile a few C# source codes with mcs and run the resulting exe file with mono.
The source codes can be downloaded here. If it's possible please describe how would you compile and run chapter 2/OpeningDocument solution.
$ mcs Program.cs
Program.cs(3,14): error CS0234: The type or namespace name `Windows' does not exist in the namespace `System'. Are you missing an assembly reference?
Compilation failed: 1 error(s), 0 warnings
EDIT
$ gmcs -pkg:dotnet Program.cs
Program.cs(17,33): error CS0246: The type or namespace name `Form1' could not be found. Are you missing a using directive or an assembly reference?
Program.cs(17,25): error CS1502: The best overloaded method match for `System.Windows.Forms.Application.Run(System.Windows.Forms.Form)' has some invalid arguments
/usr/lib/mono/gac/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll (Location of the symbol related to previous error)
Program.cs(17,25): error CS1503: Argument `#1' cannot convert `object' expression to type `System.Windows.Forms.Form'
Compilation failed: 3 error(s), 0 warnings

If you're not using the latest version of Mono, this should work:
gmcs -pkg:dotnet *.cs
The latest version of mono uses mcs instead of gmcs. In the previous ones, mcs targets .NET 1.1.

You may wish to run your code against the mono migration analyser to see what fails or isn't supported. You can find the tool here: http://www.mono-project.com/MoMA. NOTE For the tool to work you will need access to a windows machine.
You can also take a look at current mono compatibility with various .Net features at http://www.mono-project.com/Compatibility.
Specifically for windows forms see: http://www.mono-project.com/WinForms
Hope this helps.

Related

Problems running a C# C++/CLI wrapper solution on x64

I am using Visual Studio 2019 version 16.11.10. I am trying to do a small C# project that can communicate with an C++ object with functions. What i understand was that i have to use something called C++/CLI wrapper.
I tried to follow this tutorial: redgate - Creating a C++/CLI Wrapper
The project consists of one solution with 3 projects. The three projects are Sandbox (C# project), Wrapper (C++/CLI wrapper project) and Core (C++ project).
A detail about the Core-project is that you have to configure it as Static library (.lib). To do it you will Right-click on the Core-project and select Properties. Under General we select Static library (.lib).
A detail about the Wrapper-project is that you have select “Not Using Precompiled Headers” at Properties->C/C++->Precompiled Headers
I have done some kind of x86/x64 “alignment” for the projects in the Configuration Manager. If i select x86 as solution platform i will set Sandbox to x86, Core to Win32 and Wrapper to Win32. If i select x64 as solution platform i will set Sandbox, Core and Wrapper to x64
I had problems to run the solution on x86. The problem was that Wrapper and Sandbox used different .NET-versions. So now, if both use the same .NET-version (for example .NET 3.1 or .NET 5) for Wrapper and Sandbox, then i can run the solution on x86.
I have problems running the solution on x64. When i run it i get this error:
Microsoft Visual Studio
The debug executable
'C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\bin\x64\Debug\net5.0\Sandbox.exe'
specified in the 'Sandbox' debug profile does not exist.
OK
I also get these Errors:
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ) Core C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Core\MSVCRTD.lib(exe_main.obj) 1
Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source? Wrapper C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Wrapper\Entity.cpp 16
Error CS0246 The type or namespace name 'Entity' could not be found (are you missing a using directive or an assembly reference?) Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Program.cs 9 Active
Error CS0246 The type or namespace name 'Entity' could not be found (are you missing a using directive or an assembly reference?) Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Program.cs 9 Active
Error CS0246 The type or namespace name 'CLI' could not be found (are you missing a using directive or an assembly reference?) Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Program.cs 2 Active
Error CS0006 Metadata file 'C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Wrapper.dll' could not be found Sandbox C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\CSC 1 Active
Error LNK1120 1 unresolved externals Core C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Core.exe 1
How to be able to run this solution as x64? What i am doing wrong?
EDIT1:
As Alan Birtles suggested, i post the text from the Output-window:
Build started...
1>------ Build started: Project: Core, Configuration: Debug x64 ------
Restored C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Sandbox\Sandbox.csproj (in 3 ms).
1>Entity.cpp
1>MSVCRTD.lib(exe_main.obj) : error LNK2019: unresolved external symbol main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
1>C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Core.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "Core.vcxproj" -- FAILED.
2>------ Build started: Project: Wrapper, Configuration: Debug x64 ------
2>pch.cpp
2>AssemblyInfo.cpp
2>Entity.cpp
2>C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\Wrapper\Entity.cpp(16,1): fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "pch.h"' to your source?
2>Wrapper.cpp
2>Generating Code...
2>Done building project "Wrapper.vcxproj" -- FAILED.
3>------ Build started: Project: Sandbox, Configuration: Debug x64 ------
3>CSC : error CS0006: Metadata file 'C:\Users\Philip\kodning2\vsworkdir\egnaTestProjekt\Solution_CLIExample3\Core\x64\Debug\Wrapper.dll' could not be found
3>Done building project "Sandbox.csproj" -- FAILED.
========== Build: 0 succeeded, 3 failed, 0 up-to-date, 0 skipped ==========

AddictedCS SoundFingerprinting giving netstandard error

I am attempting to use AddictedCS SoundFingerprinting (https://github.com/AddictedCS/soundfingerprinting).
When I download any recent version and build with Visual Studio 2019 I get the expected output with no errors or warnings (SoundFingerprinting.dll).
The issue is that when I attempt to call functionality from the DLL in a .net console application I get errors relating to netstandard referencing.
I've attempted to add a new reference manually as the error message suggests, but get no further - same error occurs.
Here is my code, taken straight from the SoundFingerprinting example:
var hashedFingerprints = await FingerprintCommandBuilder.Instance
.BuildFingerprintCommand()
.From("C:/Users/Asher/Desktop/testSound.wav")
.UsingServices(audioService)
.Hash();
Here is the error log I am getting when compiling my console app with either mcs or csc:
C:\Users\Asher\Desktop\Raw_0.1>mcs TestProg.cs
-r:SoundFingerprinting.dll -r:protobuf-net.dll TestProg.cs(46,40): error CS0012: The type System.Object' is defined in an assembly that
is not referenced. Consider adding a reference to assembly
netstandard, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=cc7b13ffcd2ddd51'
C:\Users\Asher\Desktop\Raw_0.1\SoundFingerprinting.dll (Location of
the symbol related to previous error) Compilation failed: 1 error(s),
0 warnings
I attempted this on my desktop PC running Windows 10, and on my Surface Pro 6 running Windows 10. I also attempted this using VS2017. The result was the same each time.
Also tried to use the library in Unity3D which caused a crash with no prompt or stacktrace output.
To Reproduce
Use: Windows 10, Visual Studio 2019
Steps to reproduce the behavior:
Download latest release (v7.2.0-beta3)
Build libraries
Attempt to call functionality from SoundFingerprinting.dll via a console application
I encountered exactly the same problem today. What fixed it for me was simply downloading the latest version of protobuf-net from nuget:
Install-Package protobuf-net -Version 3.0.101
Also, please don't paste pictures of code but the code itself next time.

Can i build C# projects on Ubuntu with Jenkins?

We have Jenkins running on an Ubuntu OS 18.04.01 LTS and an older project written in C# / .NET 4.5
The application was always build manually in the past and I would like to use Jenkins to introduce CI so nobody needs to build the project manually anymore. The application does not have to run on Linux or anything like that, we just need the buildfiles (executables and DLL files). Is it possible to build this C# solution with Jenkins on Ubuntu?
I know about MONO and XBUILD but actually I can't manage to build the project under ubuntu because of different issues which do not happen under Windows, e.g. see here:
UserControls/ExpandablePanel.cs(33,35): error CS1043: Invalid accessor body `=>', expecting `;' or `{'
UserControls/ExpandablePanel.cs(33,66): error CS1043: Invalid accessor body `=>', expecting `;' or `{'
UserControls/ExpandablePanel.cs(35,41): error CS1043: Invalid accessor body `=>', expecting `;' or `{'
UserControls/ExpandablePanel.cs(37,40): error CS1043: Invalid accessor body `=>', expecting `;' or `{'
UserControls/ExpandablePanel.cs(133,22): error CS1644: Feature `pattern matching' cannot be used because it is not part of the C# 6.0 language specification
UserControls/PagedDataGridView.cs(441,68): error CS1043: Invalid accessor body `=>', expecting `;' or `{'
1 Warning(s)
6 Error(s)
Is it even possible or do I definitely need to run Jenkins on a Windows machine?
It is possible. You can build C# project on Linux/Ubuntu. Try building using mono. When you will install mono package, one of the binaries that you will get will be xbuild. To build just invoke the following command:
xbuild <path to csproj/solution file>
Try building this manually first and then integrate with jenkins.

How to make MSBUILD resolve System.Action<T1,T2> correctly?

My projects build fine in Visual Studio and also using MSBUILD on my machine.
On the build agent however if I run MSBUILD I get the error below.
I have already tried to specify a target framework of 4.0 but without success.
Any other ideas?
"c:\Test\Test.csproj" (default target) (6) ->
(CoreCompile target) ->
ItemSearchDelegate.cs(17,10): error CS0433: The type 'System.Action<T1,T2>' exists in both 'c:\Windows\Microsoft.NET\
Framework\v4.0.30319\mscorlib.dll' and 'c:\Windows\assembly\GAC_MSIL\System.Core\3.5.0.0__b77a5c561934e089\System.Core.
dll'
What I find most interesting: some lines above that error from MSBUILD, I see:
CSC : warning CS1685: The predefined type 'System.Action' is defined in multiple assemblies in the global alias; using definition from 'c:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll'
So it decides to use v4 just to complain a second later?
All I do is: open the command line at the location of the .CSPROJ file and run "msbuild". Nothing fancy.

Compile code using Tuple using Mono's mcs

I have some C# code using Tuples:
public class Test {
static void Main() {
Tuple<int, int> t = Tuple.Create(0, 1);
}
}
I tried compiling using
mcs -debug+ -o Test.exe Test.cs
but it gives the error
Test.cs(3,9): error CS0246: The type or namespace name `Tuple' could not be found. Are you missing a using directive or an assembly reference?
Compilation failed: 1 error(s), 0 warnings
I thought it might be trying to compile against an old version of mscorlib which lacks tuples. Looking at the man page, it seems you specify the version using -sdk:4, but that doesn't work either:
$ mcs -sdk:4 Test.cs
Unhandled Exception: System.TypeLoadException: Type 'System.Dynamic.BinaryOperationBinder' not found in assembly 'System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'
(followed by a stack trace).
I am running:
$ mcs --version
Mono C# compiler version 2.10.8.1
on Ubuntu Precise. According to the documentation, Mono has supported .NET 4.0 since version 2.8, and in particular supports System.Tuple, so that shouldn't be the issue.
How do you compile code that uses Tuples?
I would expect it to fail with mcs but work with dmcs. I've just installed Mono 2.10.9 on Windows, clean, and here were my results with your code (including using System; at the top):
c:\Users\Jon\Test>mcs Test.cs
Test.cs(4,9): error CS0246: The type or namespace name `Tuple' could not be
found. Are you missing a using directive or an assembly reference?
Compilation failed: 1 error(s), 0 warnings
c:\Users\Jon\Test>dmcs Test.cs
Test.cs(4,25): warning CS0219: The variable `t' is assigned but its value is
never used
Compilation succeeded - 1 warning(s)
The difference is that dmcs uses framework v4 by default whereas mcs uses v2. You can get it to work with mcs just by specifying the v4 framework:
mcs -sdk:4 Test.cs
Try that, and also double check that you really had the same problem when you used dmcs. I wouldn't be surprised if you'd seen that it wasn't a clean compile but didn't notice that it was a different message.

Categories

Resources