【问题标题】:Embedding Julia in C#在 C# 中嵌入 Julia
【发布时间】:2016-06-29 18:27:12
【问题描述】:

我在 Github 上从 here 中提取了与此 post 相关的代码,但它对我不起作用。

我运行的是 Windows 10、Visual Studio 2012,我的 julia 目录是 c:\julia-0.4.3\bin。我对代码做了一些小改动以反映我的路径:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
    class Program
    {
        [DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void jl_init(string julia_home_dir);

        [DllImport("libjulia.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern void jl_eval_string(string str);

        static void Main(string[] args)
        {
            // Pass

            jl_init(@"C:\Julia-0.4.3\bin");

            // Crash
            jl_eval_string("print(sqrt(2.0))");

        }
    }
}

当我在 Visual Studio 中运行程序时,我得到: 试图加载格式不正确的程序。 (HRESULT 异常:0x8007000B)

如果我将 ConsoleApplication1.exe 文件复制到 c:\julia-0.4.3\bin 并从 cmd 行运行它,我会得到: 未处理的异常:System.BadImageFormatException:尝试加载格式不正确的程序。 (来自 HRESULT 的异常:0x8007000B) 在 ConsoleApplication1.Program.jl_init(字符串 julia_home_dir) 在 ConsoleApplication1.Program.Main(String[] args)

是我做错了什么,还是 Windows 10 是罪魁祸首?我对 C# 有点陌生,所以对于更熟练的程序员来说,这里可能会有一些明显的东西。

谢谢!

【问题讨论】:

    标签: c# julia embedding


    【解决方案1】:

    因为您正在将 32 位库加载到 64 位 exe 中,反之亦然

    尝试转到项目属性并关闭“首选 32 位”

    【讨论】:

      猜你喜欢
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-16
      • 2017-11-10
      • 2019-05-13
      • 2012-01-23
      • 2012-04-23
      相关资源
      最近更新 更多