Can someone provide a simple example of using CxxWrap?

I think I got it.

This compiles the “lib”, loads it and calls it in the simplest way:

using Pkg
cd(@__DIR__)
Pkg.activate(".")
using CxxWrap

write("hello.cpp",
"""
#include <string>
#include "jlcxx/jlcxx.hpp"

std::string greet()
{
   return "hello, world";
}

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

cxx_include_path   = joinpath(CxxWrap.prefix_path(),"include")
julia_include_path = joinpath(Sys.BINDIR,"..","include","julia")

# Compile 
cmd = `g++ -shared -fPIC -o libhello.so -I $julia_include_path -I $cxx_include_path  hello.cpp`
run(cmd)
# Generate the function for Julia
@wrapmodule(() -> joinpath(pwd(),"libhello"))
# Call greet and show the result
@show greet()
3 Likes