JuliaInterop / CxxWrap.jl

Package to make C++ libraries available in Julia
Other
417 stars 67 forks source link

Unable to replicate the example in the readme #445

Open sylvaticus opened 3 months ago

sylvaticus commented 3 months ago

Hello, I am trying to replicate the example in the readme on a Linux Ubuntu system with gcc/cmake installed but I am getting the following error:

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

using CxxWrap
cxxpath    = CxxWrap.prefix_path()
srcpath    = pwd()
buildpath  = joinpath(sourcepath,"build")

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

write("hello.h",
"""
#include "jlcxx/jlcxx.hpp"

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

mkpath(buildpath)
cd(buildpath)

cmd1 = `cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH=$cxxpath $srcpath`
cmd2 = `cmake --build . --config Release`
run(cmd1) # error

When I run the command from the terminal I get make: invalid option -- 'D'

I have tried to remove the D and to use two hypens (--), but still no success...

By the way, if the examples are given as made from within Julia (using write/run), they would be easier to replicate....

sylvaticus commented 3 months ago

Crosspost from the discourse thread: https://discourse.julialang.org/t/can-someone-provide-a-simple-example-of-using-cxxwrap/115908/6

In the end I found it:

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

I think it could be useful to start the documentation/readme with something on this line because of its reproducibility (in Linux at least) and focus on the sole CxxWrap API, without mixing it with the issues on how to compile it with CMake, that could be discussed later on....