How to use julia language in C ++ (visual studio)

Is it possible to use julia language in C ++? Does julia include some libraries?

I'm currently trying to use some of the julia language features in my C ++ project. Is it possible? What can I do?

early.

+5
source share
2 answers

Attachment Julia

Yes, Julia can be embedded in a C or C ++ program on all platforms that Julia itself is available, and in all cases the general approach is one and the same, but, in particular, implementation in Windows is becoming more complicated, because the structure for compilation / attachment ( gcc ) is not standard for this platform by default ( MSVC ). The reason is that Julia is built on Windows using gcc , not MSVC .

High level

At a high level, the steps for implementing Julia include compiling using the resources provided by the Julia distribution (see below), and then initializing the program to launch the Julia engine.

enable julia.h

All necessary definitions for a c or C ++ program are located in julia.h. The exact location for it differs for each distribution, but in general it is located in julia-basedir / include.

link to libjulia

Similarly, all the necessary symbols for embedding Julia are in libjulia. On OS / X and Linux, libjulia.so will generally be available in julia-basedir / julia / lib, and on Windows libjulia.dll will be julia-basedir / julia / bin.

or optionally in 0.4: use julia-config.jl

The previous one may seem confusing, but, fortunately, the latest Julia 0.4 distributions have a script called julia-config.jl that will automatically provide all the necessary compiler flags - disclaimer I wrote it . In this case, all you have to do is cut and paste and follow the template in the documentation, create a Makefile, and make will take care of the rest.

initialize with jl_init

As described in the docs , use jl_init to start runtime Julia, and if necessary, specify the directory where the Julia sys.ji database support can be located. I found that it is better to specify this directly, and not by default; julia-config.jl also provides -DJL_INIT_DIR , which can be used blindly as an argument to jl_init; The documents contain detailed information.

Problem with windows

Return to Windows. If you follow the instructions to compile Julia on Windows , you will get the MSYS2 compilation environment . Please note that these instructions are somewhat outdated, and MSYS2 has progressed since then, so it is now easier (for example, using 7-zip is not required). As a side, it also allows you to directly get git - note that the last comment from @ ntzrmtthihu777 is now the best as git via MSYS2, superior to git - bash, which is based on earlier MSYS .

NCA

Now MSYS2 does provide gcc , but you shouldn't use it because it implicitly uses a thread model (POSIX) different from the one used by Julia (Winthreads), and instead you should get gcc from mingw-builds , which gives you the ability to select a model during installation; it is also indicated in the Julia compilation README window, but this is repeated. Other tools can be obtained from the MSYS2 pacman package manager.

Mingw builds provides the installer, and I found that the following fstab would be enough to make mccw-builds gcc available in the right place:

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 

Looking beyond

If you manage to create a compilation environment suitable for compiling Julia from the source code, you can verify this by compiling Julia, then the above instructions will work, including simplifying julia-config.jl / Makefile, and create one that embeds Julia and will. exe, which will work even when called outside of MSYS2 , which is nice.

But if you want to use MSVC directly, then I must warn you that compiling Julia with MSVC is still in its early stages, so the above approach with MSVC replaced by gcc will not work at present, but there is a chance that libjulia can be connected; libraries created by mingw are expected to be used by at least MSVC .

Updated MSVC assembly (creating julialib.lib)

libjulia.dll is a library containing all the characters needed to embed Julia, and in addition, although it was created by gcc, it can be used by MSVC because all these characters have C names and are not C ++ with the name-mangled. However, it is not used directly, but requires the creation of .lib . This can be done as follows.

Create julialib.lib

  • use dumpbin to export characters from dll to file, for example:

    dumpbin /exports libjulia.dll > output

  • convert the output to a .def file by deleting extraneous text and placing EXPORT at the top of the time, as described in detail here

  • Use lib to create .lib from .def .

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

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

  • Build a project by including julia.h and a link to libjulia.lib
+9
source

WaTeim's answer is correct, go vote for him! I just want to complement it with the steps for using the library in a Visual Studio project. The following assumes libjulia.lib is in your subfolder Julia bin and you want to use a 64-bit application. I did this in Visual Studio 2013.

First, configure a 64-bit solution / project, if it is not already done. Select / click / check the following:

  • Build / configuration manager
  • Active Solutions Platform / New
  • X64 / Copy Settings from Win32 / Creating New Project Platforms

Then configure the project:

  • Right Click Project / Properties
  • Select Configuration = All Configurations , Platform = x64
  • Debugging / environment: PATH=$(JULIA_HOME);$(PATH)
  • C / C ++ / General / Additional Directories Include: $(JULIA_HOME)\..\include\julia
  • Linker / General / Additional Library Links: $(JULIA_HOME)
  • Linker / Input / Additional Dependencies: add libjulia.lib to the list

Make sure your active x64 platform configuration then builds your solution. It worked for me, hope it works for you too :)

+1
source

Source: https://habr.com/ru/post/1215071/


All Articles