CppCodeProvider、CompileAssemblyFromFile 抛出 System.NotImplementedException

发布于 2024-10-07 15:59:31 字数 1370 浏览 0 评论 0原文

使用框架 CodeDom 类编译 C++/CLI 的文档有些缺乏。

背景:我正在尝试为我编写的一个软件编写系统测试。该软件生成代码,我想测试该代码是否可以编译,以及编译后的代码是否按预期工作。

要编译和测试的代码位于几个源文件中。我想将源文件编译成程序集,然后动态调用其中的方法。我尝试过使用相同的 C# 源代码,并在几分钟内成功构建和测试。 C++ 编译器总是抛出 System.NotImplementedException。

下面的代码让我大吃一惊:

namespace CppCompilerTest {
    class Program {
        static void Main(string[] args) {
            try {
                string strSourceDir = @"D:\Projects\CppCompilerTest\CppSource";

                using (var codeProvider = new CppCodeProvider7()) {
                    var options = new CompilerParameters { GenerateExecutable = false, GenerateInMemory = false };
                    options.ReferencedAssemblies.Add("System.dll");
                    CompilerResults results = codeProvider.CompileAssemblyFromFile(
                        options,
                        new[] {
                            Path.Combine(strSourceDir, "SourceFile1.cpp"),
                            Path.Combine(strSourceDir, "SourceFile2.cpp")                               
                        }
                    );
                }
            } catch(Exception ex) {
                Console.WriteLine(ex);
            }

            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }
    }
}

我是否完全忽略了某些事情?我想知道 cl.exe 方式是否是更快的路线......

The documentation for compiling C++/CLI using the frameworks CodeDom classes is somewhat lacking.

The background: I'm trying to write system tests for a piece of software I have written. The software generates code, and I want to test that the code can be compiled, and that the compiled code works as intended.

The code to be compiled and tested resides in a couple of source files. I want to compile the source file into an assembly, and then dynamically invoke the methods in it. I have experimented with doing the same C# sources, and I had a successful build and test in minutes. The C++ compiler always throws System.NotImplementedException.

Here's the code that blows up on me:

namespace CppCompilerTest {
    class Program {
        static void Main(string[] args) {
            try {
                string strSourceDir = @"D:\Projects\CppCompilerTest\CppSource";

                using (var codeProvider = new CppCodeProvider7()) {
                    var options = new CompilerParameters { GenerateExecutable = false, GenerateInMemory = false };
                    options.ReferencedAssemblies.Add("System.dll");
                    CompilerResults results = codeProvider.CompileAssemblyFromFile(
                        options,
                        new[] {
                            Path.Combine(strSourceDir, "SourceFile1.cpp"),
                            Path.Combine(strSourceDir, "SourceFile2.cpp")                               
                        }
                    );
                }
            } catch(Exception ex) {
                Console.WriteLine(ex);
            }

            Console.WriteLine("Press any key to continue");
            Console.ReadKey();
        }
    }
}

Have I totally overlooked something? I'm wondering if maybe the cl.exe way is the faster route...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文