I am trying to learn C# coming from a C++ background, but I cannot figure out how to link two source files together. I have a relatively simple program called test.cs and a main.cs. All I want to do is in main.cs say:#include <"test.cs">
.
The closest I've been able to come up with is:
<Compile Include="test.cs"/Compile>
However the compiler does not recognize this. So how can I include multiple source files in my main?
You can properly include . C or . CPP files into other source files.
A large C or C++ program should be divided into multiple files. This makes each file short enough to conveniently edit, print, etc. It also allows some of the code, e.g. utility functions such as linked list handlers or array allocation code, to be shared with other programs.
You pass the list of source files to the compiler:
csc.exe /target:library source1.cs source2.cs
If you use Visual Studio when you create a new .NET project you can add as many source files as you like and they will automatically be compiled.
If you are building this in Visual Studio, then simply having the 2 files in the same project is all you need to do.
If you are compiling on the command line using csc you ned to reference both files in the call to csc. See Darin's response for this.
There is no need to reference one file from the other, but the easiest way to make types to be visibile to each other would be to add the classes in each file to the same namespace.
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