Include file in C#, Mono (gmcs)

4.7k Views Asked by At

I'm writing a really small application in C# and I need to include an .cs file with some class. How do I do that? I'm looking for some equivalent to PHP's "include file" or Python's "from file import something". Obviously "using" is not enough and the file has to be somehow linked. And I really don't want to start some new super-huge-totally-useless project using MonoDevelop or VisualStudio, I would like to stay with simple gmcs and command line.

2

There are 2 best solutions below

1
On BEST ANSWER

You simply include both file names on the command line and ensure that the namespaces are the same or that the namespace of the included file is imported via a using statement or via fully qualified references. Then the command line for compilation looks like this:

gmcs mainFile.cs includeFile.cs

Note that the mono command line is designed to support the exact same syntax (with a few additions) as the Microsoft compiler so this is true for both of them.

Fundamentally this is what the project files and visual studio are doing (albeit going through an in memory msbuild equivalent)

0
On

There are two ways to "include" a file in .NET (and Mono)

  1. Compile several files together.

    gmcs mainFile.cs includeFile.cs
    

    then files are then compiled together to a single assembly

  2. Compile the includeFile to a separate assembly and reference that from the main assembly

    gmcs -target:library includeFile.cs
    gmcs -r:includeFile.dll mainFile.cs
    

    this way you get two assemblies