Skip to content

Instantly share code, notes, and snippets.

@terasakisatoshi
Last active December 8, 2020 04:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terasakisatoshi/b6a7121cd570f6739992345095b07d62 to your computer and use it in GitHub Desktop.
Save terasakisatoshi/b6a7121cd570f6739992345095b07d62 to your computer and use it in GitHub Desktop.
CxxWrap.jl
JL="$(dirname "$(dirname `which julia`)")"
PREFIX=`julia -e 'using CxxWrap; CxxWrap.prefix_path() |> print'`
g++ -fPIC -shared -std=c++17 \
-I${PREFIX}/include/ \
-L${PREFIX}/lib/ \
-I${JL}/include/julia \
-L${JL}/lib \
hello.cpp -o libhello.so
# Load the module and generate the functions
module CppHello
using CxxWrap
@wrapmodule(joinpath(".","libhello.so"))
function __init__()
@initcxx
end
end
# Call greet and show the result
@show CppHello.greet()
FROM julia:1.5.3
RUN apt-get update && apt-get install -y \
build-essential
RUN julia -e 'using Pkg; Pkg.add("CxxWrap"); Pkg.precompile()'
#include <string>
#include "jlcxx/jlcxx.hpp"
std::string greet()
{
return "hello, world";
}
JLCXX_MODULE define_julia_module(jlcxx::Module& mod)
{
mod.method("greet", &greet);
}
@terasakisatoshi
Copy link
Author

terasakisatoshi commented Aug 11, 2020

Usage

Install CxxWrap.jl via Julia's REPL: julia> using Pkg; Pkg.add("CxxWrap").
Then, simply run:
bash build.sh && julia callcxx.jl

Related Topics

https://gist.github.com/terasakisatoshi/163ab1fc3ff1adab340d221eae33d218

@terasakisatoshi
Copy link
Author

terasakisatoshi commented Dec 7, 2020

Usage (using Docker)

$ docker build -t jl .
$ docker run --rm -it -v $PWD:/work -w /work jl  bash -c 'bash build.sh && julia callcxx.jl'
CppHello.greet() = "hello, world"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment