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.
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.
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.
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.
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.
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.
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.
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.
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
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.
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.
use dumpbin to export symbols from the dll into a file for example:
dumpbin /exports libjulia.dll > output
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
Use lib to create a .lib from the .def.
lib /def:libjulia.def /out:libjulia.lib /machine:x64
Place libjulia.lib in the same directory as libjulia.dll
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:
Then setup your project's configuration:
All Configurations
, Platform = x64
PATH=$(JULIA_HOME);$(PATH)
$(JULIA_HOME)\..\include\julia
$(JULIA_HOME)
libjulia.lib
to the listEnsure your active configuration is for platform x64
then build your solution. It worked for me, hopefully it will work for you as well :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With