Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use the julia language in c++ (visual studio)

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

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

thanks in advance.

like image 646
Yves Avatar asked Mar 10 '15 15:03

Yves


2 Answers

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.

High level

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.

include julia.h

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.

link to libjulia

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.

or optionally in 0.4: use julia-config.jl

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.

initialize using 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.

The problem with Windows

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.

gcc

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 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

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.

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.

Updated MSVC Compilation (creating julialib.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.

Create julialib.lib

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

    dumpbin /exports libjulia.dll > output

  2. 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

  3. Use lib to create a .lib from the .def.

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

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

  5. Build project by including julia.h and link to libjulia.lib
like image 141
waTeim Avatar answered Nov 14 '22 22:11

waTeim


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

First setup the 64 bit solution / project if not done already. Select / click / check the following:

  1. Build / Configuration Manager
  2. Active solution platform / New
  3. X64 / Copy settings from Win32 / Create new project platforms

Then setup your project's configuration:

  1. Right click project / Properties
  2. Select Configuration = All Configurations, Platform = x64
  3. Debugging / Environment: PATH=$(JULIA_HOME);$(PATH)
  4. C/C++ / General / Additional Include Directories: $(JULIA_HOME)\..\include\julia
  5. Linker / General / Additional Library Directories: $(JULIA_HOME)
  6. Linker / Input / Additional Dependencies: add libjulia.lib to the list

Ensure your active configuration is for platform x64 then build your solution. It worked for me, hopefully it will work for you as well :)

like image 43
tiho Avatar answered Nov 15 '22 00:11

tiho