如何在C ++中使用julia语言(Visual Studio) [英] how to use the julia language in c++ (visual studio)

查看:413
本文介绍了如何在C ++中使用julia语言(Visual Studio)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在c ++中使用julia语言? 茱莉亚语言是否提供一些要包含的库?

Is it possible to use the julia language in c++? Does the julia language provides some libraries to include?

现在,我正在尝试在我的c ++项目中使用一些朱莉娅语言的功能. 这有可能吗?我该怎么办?

For now, I am trying to use some funcitons of the julia language in my c++ project. Is this possbile? What could I do?

提前谢谢.

推荐答案

嵌入朱莉娅

是的,Julia可以嵌入到Julia本身可用的所有平台上的C或C ++程序中,并且在所有情况下,通用方法都相同,但是特别是在Windows中的嵌入变得更加困难,因为当前的框架编译/嵌入( gcc )不是该平台( MSVC )的默认熟悉版本.原因是Julia是使用 gcc 而不是 MSVC 在Windows上构建的.

Embedding Julia

Yes Julia can be embedded in a C or C++ program on all of the platforms which Julia itself is available and in all cases the general approach is the same, but in particular the embedding in Windows is made harder because currently the framework for compilation/embedding (gcc) is not the default familiar one for that platform (MSVC). The reason is that Julia is built on Windows using gcc rather than MSVC.

在较高层次上,嵌入Julia的步骤包括使用Julia发行版提供的资源进行编译(请参见下文),然后初始化程序以启动Julia引擎.

At a high level the steps for embedding Julia include compiling using the resources supplied by the Julia distribution (see below) and then initializing the program to start the Julia engine.

c或c ++程序的所有必需定义都位于 julia.h 中.每个发行版的确切位置都不同,但通常位于 julia-basedir /include中.

All of the necessary defines for either a c or c++ program are located in julia.h. The exact location for it differs for each distribution, but in general it's located in julia-basedir/include.

同样,嵌入Julia的所有必需符号都位于libjulia中.在OS/X和Linux上, libjulia.so 通常可在 julia-basedir /julia/lib中获得,而在Windows上, libjulia.dll julia-basedir /julia/bin.

Likewise all of the necessary symbols to embed Julia are located in libjulia. On OS/X and Linux libjulia.so will be generally available in julia-basedir/julia/lib while on Windows libjulia.dll will be julia-basedir/julia/bin.

以前的版本可能听起来很混乱,但是幸运的是,最新的Julia 0.4发行版中包含一个名为 julia-config.jl 的脚本,它将自动提供所有需要的编译器标志-我写的免责声明.在这种情况下,您要做的就是剪切并粘贴并按照文档中的模式进行操作,创建Makefile,其余的操作由 make 处理.

The previous might all sound confusing, but luckily contained in the newest Julia 0.4 distributions is a script called julia-config.jl which will provide all of the needed compiler flags automatically -- disclaimer I wrote it. In this case all that you need to do is cut and paste and follow the pattern in the documentation, create a Makefile, and make will take care of the rest.

如文档中> 中所述,使用 jl_init 以启动Julia运行时,同时可以选择指定Julia编译基础支持 sys.ji 所在的目录.我发现最好直接指定它,而不是让它默认. julia-config.jl 还提供了 -DJL_INIT_DIR ,可以将其盲目用作jl_init的参数.文档提供了详细信息.

As described in the docs, use jl_init to start the Julia runtime, while optionally specifying the directory where the Julia compiled base support sys.ji can be located. I've found it's best to specify this directly rather than let it default; julia-config.jl also provides -DJL_INIT_DIR which can be blindly used as an argument to jl_init; the docs provides details.

返回Windows.如果您按照在Windows中编译Julia 的说明进行操作,则您将以 MSYS2 编译环境结束.请注意,这些说明有些过时了,并且 MSYS2 已改进然后,它现在变得更简单(例如,无需使用7-zip).顺便说一句,这还允许您直接获取 git –注意@的最后评论通过MSYS2,ntzrmtthihu777现在是最好的git,优于基于旧版 MSYS 的git-bash.

Returning to Windows. If you follow the instructions to compiling Julia in Windows, you will end up with an MSYS2 compilation environment. Note that these instructions are somewhat out of date, and MSYS2 has advanced since then, so it is now simpler (for example use of 7-zip is not necessary). As an aside, this also allows you to obtain git directly -- note the last comment by @ntzrmtthihu777 is now the best one as git via MSYS2 superior to git-bash which is based on the older MSYS.

现在 MSYS2 确实提供了 gcc ,但是您不能使用它,因为它隐式使用的线程模型(POSIX)与Julia使用的模型不同. (Winthreads),您必须从 mingw-builds 获得gcc,这为您提供了选择安装过程中的模型; Julia编译窗口自述文件也指出了这一点,但是需要重复.其他工具可以从 MSYS2 程序包管理器 pacman 中获得.

Now MSYS2 does indeed provide a gcc, but you must not use it because it implicitly uses a threading model (POSIX) that is different from the one used by Julia (Winthreads) and instead you must obtain gcc from mingw-builds which gives you an option to pick the model during the install; this is indicated by the Julia compilation Window README too, but it bears repeating. Other tools can be obtained from the MSYS2 package manager pacman.

Mingw构建提供了一个安装程序,我发现以下fstab足以使mingw构建gcc在正确的位置可用:

Mingw builds provides an installer, and I've found that the following fstab will be sufficient to make the mingw-builds gcc available in the right location:

none / cygdrive binary,posix=0,noacl,user 0 0

c:/mingw-w64/x86_64-4.9.2-win32-seh-rt_v3-rev1/mingw64 /mingw64 ntfs binary,noacl,auto 0 0

超越视野

如果您成功创建了一个适合从源代码编译Julia的编译环境-您可以通过实际编译Julia来进行验证-那么上面的说明(包括julia-config.jl/Makefile简化)将起作用,并将产生嵌入Julia的程序,它将是一个.exe,即使在 MSYS2 之外调用也可以正常工作.

Looking beyond

If you are successful creating a compilation environment suitable for compiling Julia from source -- you can verify this by actually compiling Julia -- then the instructions above including the julia-config.jl/Makefile simplification will work, and will produce a program that embeds Julia and will be a .exe that will work even when invoked outside of MSYS2, which is nice.

但是,如果您希望直接使用 MSVC ,那么我应该警告您,使用 MSVC 编译Julia仍处于早期阶段,因此上述方法 MSVC 替代了 gcc 目前尚无法使用,但是有可能将 libjulia 链接到;预期mingw创建的库是可用的至少由MSVC

But if you are looking to use MSVC directly, then I should warn you that compilation of Julia with MSVC is still int the early stages, so the above approach with MSVC substituted for gcc will not work currently, but there is the possibility that libjulia could be linked to; it is expected that libraries created by mingw are usable by MSVC at least.

libjulia.dll是一个库,其中包含嵌入Julia所需的所有符号,此外,尽管它是由gcc创建的,但由于所有这些符号都具有 C ,因此它可以被MSVC使用.命名,并且没有 C ++ 的名字被篡改.但是,它不能直接使用,而是需要创建一个 .lib .可以通过以下方式完成.

libjulia.dll is the library that contains all the symbols necessary to embed Julia, and in addition, though it is created by gcc, it can be used by MSVC because all of those symbols have C naming, and are not C++ name-mangled. However, it's not usable directly, but requires a .lib to be created. Which can be done in the following way.

  1. 使用dumpbin将dll中的符号导出到文件中,例如:

  1. use dumpbin to export symbols from the dll into a file for example:

dumpbin /exports libjulia.dll > output

通过删除多余的文本并将 EXPORTS 放在时间的顶部,将输出转换为 .def 文件,详细说明此处

transform the output to a .def file by removing the extraneous text and placing EXPORTS at the top of the time as described in detail here

使用lib从 .def 创建 .lib .

lib /def:libjulia.def /out:libjulia.lib /machine:x64

将libjulia.lib与libjulia.dll放在同一目录

Place libjulia.lib in the same directory as libjulia.dll

这篇关于如何在C ++中使用julia语言(Visual Studio)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆