0

I would like to compile a simple console application which uses a reference from a .dll to learn how basic dynamic linking in csharp works. The main file is test.cs:

using System;
using TestLibrary;

namespace Test
{
    class Test
    {
        static int Main()
        {
            Console.WriteLine(TestLibrary.AddThreeNumbers(1,2,3));
            return 0;
        }
  
    }

}

The class library is:

using System;

namespace TestLibrary
{
    class TestLibrary
    {
        int AddThreeNumbers(int a, int b, int c)
        {
            return a+b+c;
        }
  
    }

}

I use the following in the powershell developer command prompt to compile library.cs into a dll:

csc library.cs -target:library -out:library.dll

This works fine. But I then try to link to make an executable as follows:

csc test.cs -reference:TestLibrary=library.dll

But this returns an error:

test.cs(2,7): error CS0246: The type or namespace name 'TestLibrary' could not be found (are you missing a using directive or an assembly reference?)

Is this is a problem with my code or a problem with how I am compiling it? Any help would be much appreciated.

user32882
  • 5,094
  • 5
  • 43
  • 82
  • try with `csc /r:library.dll /out:test.exe test.cs` and ensure the lib is in the same folder as the `test.cs` file – Gusman Oct 17 '20 at 15:33
  • I think that may have worked... is `/r` the same as `-reference`? I don't see any indication that backslashes are possible in https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe – user32882 Oct 17 '20 at 15:37
  • Yes, it's the same: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/reference-compiler-option Check the remarks section. I think the problem in your call was the order, you added the reference after the code file, csc is a bit picky with the parameter order. – Gusman Oct 17 '20 at 15:42
  • But where does it say that forward slashes are OK? Also if you want to post your command as the final answer I will accept it... – user32882 Oct 17 '20 at 15:43
  • Forward slashes and dashes are both valid as parameter indicators. – Gusman Oct 17 '20 at 15:45
  • Yes... but where does it say that lol... – user32882 Oct 17 '20 at 15:46
  • The convention for windows is to use slashes, not dashes, but some programs also accept dashes. https://stackoverflow.com/questions/15683347/when-implementing-command-line-flags-should-i-prefix-with-a-fowardslash-or – Gusman Oct 17 '20 at 15:48

1 Answers1

1

As @Gusman said the command to use is :

csc /r:library.dll /out:test.exe test.cs
user32882
  • 5,094
  • 5
  • 43
  • 82