7

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.

Reza Afzalan
  • 5,646
  • 3
  • 26
  • 44
Yves
  • 11,597
  • 17
  • 83
  • 180
  • Have you read the manual chapter on [Embedding Julia](http://docs.julialang.org/en/release-0.3/manual/embedding/) in C/C++? What have you tried? – mbauman Mar 10 '15 at 15:24
  • @MattB. Yes, I ve read it. However, I am using visual studio 2013 under the system windows10. I dont know how to use the gcc in my pc... – Yves Mar 10 '15 at 15:38
  • @MattB. First, I download the julia-0.3.6. Then I add the path: /julia-0.3.6/include to the "Include Directories" of my project. In my opinion there should be some lib but I cant find them. So obviously, I will get the LNK error. In the julia-0.3.6, there is a file whose name is lib but there is only a file named sys.ji. So I dont know what to do now. Could u help me? – Yves Mar 10 '15 at 16:27
  • I'm not familiar with either embedding or MSVC, so I'm afraid I can't help you directly. But you will definitely have a better chance of getting a good response by editing your question to break down the problem and be more specific. E.g., "How do I link against libjulia with MSVC?" Post the source for the simple hello-world-from-julia program you're trying to compile and show the error messages. See this Stackoverflow page for details: http://stackoverflow.com/help/how-to-ask – mbauman Mar 10 '15 at 17:55

2 Answers2

12

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
Community
  • 1
  • 1
waTeim
  • 9,095
  • 2
  • 37
  • 40
  • I followed your last (1-5) steps and got this error error LNK2019: unresolved external symbol __imp__jl_eval_string referenced in function _main – Nicole Jun 23 '15 at 04:41
  • @Nicole I too fumbled about with various linker options for a while. Can you verify that you have set the right library directory, the linker is actually finding libjulia.lib, that you are compiling for 64 bit? I'm confident this will eventually work. – waTeim Jun 23 '15 at 06:02
  • :I actually included 1) C:\Julia-0.3.9\include ---> VC++ Inc Dir 2) VC++--->Lib Dir to C:\Julia-0.3.9\bin 3) C/C++-->Additional Inc Dir--->C:\Julia-0.3.9;C:\Julia-0.3.9\bin;C:\Julia-0.3.9\include\julia 4) Linker-->Input--->Additional Dep--->libjulia.lib I also have created the libjulia.lib in bin folder and so I did not paste it somewhere else. So, where do you think is my problem? – Nicole Jun 23 '15 at 06:13
  • I also compiled using Win 64. – Nicole Jun 23 '15 at 06:13
  • @Nicole That looks right, perhaps missing some non-incremental flags. I will attempt to reproduce and document using a gist. You may find [the discussion on dev](https://groups.google.com/forum/#!topic/julia-dev/jCIQkp0dU5Q) useful in the meantime. – waTeim Jun 23 '15 at 06:43
  • @Nicole You might find [this script](https://github.com/waTeim/node-julia/blob/master/tools/create_julia_libs.py) useful (script name and location changed). – waTeim Jul 03 '15 at 16:10
3

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

tiho
  • 6,655
  • 3
  • 31
  • 31