Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to replicate the example in the readme #445

Open
sylvaticus opened this issue Jun 20, 2024 · 1 comment
Open

Unable to replicate the example in the readme #445

sylvaticus opened this issue Jun 20, 2024 · 1 comment

Comments

@sylvaticus
Copy link

sylvaticus commented Jun 20, 2024

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
Copy link
Author

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....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant