如何在 C++ 中使用 julia 语言(Visual Studio)
是否可以在 c++ 中使用 julia 语言?Julia 语言是否提供了一些要包含的库?
Is it possible to use the julia language in c++? Does the julia language provides some libraries to include?
目前,我正在尝试在我的 c++ 项目中使用 julia 语言的一些功能.这可能吗?我能做什么?
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 可以嵌入到 Julia 本身可用的所有平台上的 C 或 C++ 程序中,并且在所有情况下,一般方法都是相同的,但特别是在 Windows 中的嵌入变得更加困难,因为目前的框架编译/嵌入 (gcc) 不是该平台 (MSVC) 的默认熟悉的.原因是 Julia 是在 Windows 上使用 gcc 而不是 MSVC 构建的.
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,然后 ma??ke 会处理其余的工作.
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 - 请注意@的最后评论ntzrmtthihu777 现在是最好的 git via MSYS2 优于基于旧 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,但你不能使用它,因为它隐含地使用了与 Julia 使用的不同的线程模型 (POSIX)(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 builds 提供了一个安装程序,我发现以下 fstab 足以使 mingw-builds 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 创建的,但它可以被 MSVC 使用,因为所有这些符号都有 C命名,并且不是 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.
使用 dumpbin 将符号从 dll 导出到文件中,例如:
use dumpbin to export symbols from the dll into a file for example:
dumpbin/exports libjulia.dll >输出
将输出转换为 .def 文件,方法是删除多余的文本并将 EXPORTS 放在时间的顶部,如详细所述这里
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.
Use lib to create a .lib from the .def.
lib/def:libjulia.def/out:libjulia.lib/machine:x64
将 libjulia.lib 与 libjulia.dll 放在同一目录中
Place libjulia.lib in the same directory as libjulia.dll
相关文章