Compiling C++ file for usage in Julia

I have been trying to replicate the example given in a different post, to which I can’t link, but here’s the relative URL: /t/can-someone-provide-a-simple-example-of-using-cxxwrap/115908/6

I’m on Windows (because there doesn’t seem to be a Debian package), and I’ve installed the MinGW compiler, which has successfully compiled a simple “Hello World” program.

Unfortunately, upon running

g++ -shared -fPIC -o greetings.so -I"C:\Users\<My name>\.julia\artifacts\6907059f4600a8fd77020c49e83de7dbbea3a28c\include" -I"C:\users\<MyName>\AppData\local\programs\julia-1.10.5\include\julia" greetings.cpp

I get a multitude of error messages ascertaining the lack of certain references (of the type undefined reference to `__imp_jl_compute_fieldtypes' or similar).

The file that I’m trying to compile looks like this:

#include <string>
#include "jlcxx/jlcxx.hpp"

std::string greet() {
	return "Hello World!";
}

JLCXX_MODULE define_julia_module(jlcxx::Module& mod) {
	mod.method("greet", &greet);
}

I’d be grateful for some hints as to what may be going wrong here.

Debian package of what?

Of Julia.

There is no need for a Debian package of Julia. On Linux, just install juliaup using

curl -fsSL https://install.julialang.org | sh

and then install the desired Julia version with

juliaup install 1.10
juliaup default 1.10

or whatever version you want. And you can update it with

juliaup update
1 Like

Thanks, I (vaguely) knew of that, but on Windows, I got the Chocolatey Julia package, which is being kept up to date automatically, whence I’m working under Windows.

A very bad reason to use Windows. I at least often need different versions of Julia, and with juliaup you can install any number of versions in parallel and switch easily per project. And programming on Linux is so much faster and more convenient, in particular if it comes to C++. But also Julia is much faster on Linux, building system images works in parallel on Linux (not on Windows), and operations on many small files are also much faster on Linux.

I do use Linux for C++ programming, unless it be for inclusion into Julia.

1 Like

Hi, and welcome to the Julia community!

I can confirm that your code and the one from the post you referred to also does not work for me on Windows. The CMake example from the CxxWrap.jl github page did work without any issues though.

I’m certainly no expert on these matters, but it seems on Windows you need to explicitly link to the binaries:

g++ -shared -fPIC -o greetings.so -I (...)\.julia\artifacts\6907059f4600a8fd77020c49e83de7dbbea3a28c\include -I (...)\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\include\julia -L (...)\.julia\artifacts\6907059f4600a8fd77020c49e83de7dbbea3a28c\bin -L (...)\.julia\juliaup\julia-1.10.4+0.x64.w64.mingw32\bin .\greetings.cpp  -lcxxwrap_julia -ljulia
julia> using CxxWrap

julia> @wrapmodule(() -> "greetings.so")  # Assuming you started Julia in the same folder

julia> greet()
"Hello World!"

works for me.

1 Like

If you want to call Julia from C++ then consider: GitHub - Clemapfel/jluna: Julia Wrapper for C++ with Focus on Safety, Elegance, and Ease of Use

It also supports Windows (and I assume also 1.11, it’s at least tested up to 1.10).

I’m on Linux, not Windows, so I can’t test, but assumed calling from Julia to C++ also worked with CppWrap (at least no less than from Linux, major upgrades have sometimes broken CxxWrap, I think so far always fixed, but haven’t looked into if .11 is an issue).

Thanks a lot, the only problem is that on my PC, the folders (such as the juliaup folder) you are referencing simply do not exist.

Well, you should use the folder where Julia is installed on your machine. Similar to in @sylvaticus 's post, you can obtain this via Sys.BINDIR and a modification where you replace bin by include\julia.

Same for the CxxWrap.jl directory, via

using CxxWrap
CxxWrap.prefix_path()

with \bin and \include appended (e.g. via joinpath).

Thank you so much, it works exactly like on your PC.

1 Like