c# - 在 Visual Studio 中通过 C# 类编译 C++ 代码

标签 c# c++ visual-studio compilation

我用这个方法在VS中编译C++文件。但即使我提供了正确的文件,它也会返回 false。谁能帮我... 这是名为 CL 的类

class CL
{
    private const string clexe = @"cl.exe";
    private const string exe = "Test.exe", file = "test.cpp";
    private string args;

    public CL(String[] args)
    {
        this.args = String.Join(" ", args);
        this.args += (args.Length > 0 ? " " : "") + "/Fe" + exe + " " + file;
    }

    public Boolean Compile(String content, ref string errors)
    {
        if (File.Exists(exe))
            File.Delete(exe);
        if (File.Exists(file))
            File.Delete(file);

        File.WriteAllText(file, content);

        Process proc = new Process();
        proc.StartInfo.UseShellExecute = false;
        proc.StartInfo.RedirectStandardOutput = true;
        proc.StartInfo.RedirectStandardError = true;
        proc.StartInfo.FileName = clexe;
        proc.StartInfo.Arguments = this.args;
        proc.StartInfo.CreateNoWindow = true;

        proc.Start();
        //errors += proc.StandardError.ReadToEnd();
        errors += proc.StandardOutput.ReadToEnd();

        proc.WaitForExit();

        bool success = File.Exists(exe);

        return success;
    }
}

这是我的按钮点击事件

    private void button1_Click(object sender, EventArgs e)
    {
        string content = "#include <stdio.h>\nmain(){\nprintf(\"Hello world\");\n}\n";
        string errors = "";

        CL k = new CL(new string[] { });
        if (k.Compile(content, ref errors))
            Console.WriteLine("Success!");
        else
            MessageBox.Show("Errors are : ", errors);
    } 

最佳答案

在您的 Visual Studio 安装文件夹中应该有以下路径:

VC\bin\x86_amd64\1033\1033

该路径下应该有一个clui.dll。将其复制到父文件夹 (VC\bin\x86_amd64\1033)。这应该可以解决您的问题。

我从 http://connect.microsoft.com/VisualStudio/feedback/details/108528/command-line-issue-when-building-for-64bit-in-32bit-with-cl-exe 中获取了解决方案:

关于c# - 在 Visual Studio 中通过 C# 类编译 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3043487/

相关文章:

C++ 迭代器 : Can iterators to an abstract class be implemented as a nested class?

C++ 编程 - 这种分配是不可避免的内存泄漏吗?

.net - 何时通过 NuGet 使用 MVC Scaffolding 与通过 MVC3 Tools Update 使用 MVC Scaffolding

asp.net - 无法访问 IIS 元数据库

c# - 将类视为使用接口(interface)定义的类

c# - lambda 表达式中的语句和返回 lambda

c# - 我可以创建一个拥有超过 500 个成员的枚举吗?

c++ - std::enable_if 的第二个参数有什么用?

git - Visual Studio Online Git - 如何确保/包被忽略?

c# - .NET MySqlCommand.Transaction - 它的目的是什么?