简体   繁体   中英

Why am I getting error CS0116 when building using a .rsp file?

I'm trying to build a project using an csc.exe and a .rsp file, but I keep getting an error CS0116: A namespace cannot directly contain members such as fields or methods. To troubleshoot, I made the simplest program ever...

class Program
{
    public static void Main()
    {

    }
}

From the command line, I run the command

"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc.exe" Test.rsp

Test.rsp looks like this

/nologo
/target:exe
/platform:x86
/out:"Test.exe"
"Test.cs"

If I compile Test.cs directly, I don't get this error. For example, if I run this command at the command line...

"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc.exe" /target:exe /platform:x86 /out:"Test.exe" "Test.cs"

The "Program" is created successfully.

So why is compiling using a .rsp file causing this error?

The compiler is trying to interpret the file as C#. If you want to use a response file, you need to let the compiler know it's a build file and not a code file. From the docs :

The @ option lets you specify a file that contains compiler options and source code files to compile

For example:

"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc.exe" @Test.rsp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM