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

Rcpp and Rust functions in same package #406

Open
Close-your-eyes opened this issue Nov 25, 2024 · 3 comments
Open

Rcpp and Rust functions in same package #406

Close-your-eyes opened this issue Nov 25, 2024 · 3 comments

Comments

@Close-your-eyes
Copy link

Close-your-eyes commented Nov 25, 2024

Hello, I tried to add functions in Rcpp and Rust to a package. When adding them separately, compilation works fine. In combination though, compilation fails. Is that expectable? What do I need to consider?

Here is the error message when running devtools::document or rextendr::document (igsc is the package's name):

── R CMD INSTALL ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
─ installing source package ‘igsc’ ... (412ms)
** using staged installation
** libs
using C compiler: ‘Apple clang version 11.0.3 (clang-1103.0.32.29)’
using C++ compiler: ‘Apple clang version 11.0.3 (clang-1103.0.32.29)’
using SDK: ‘MacOSX10.15.sdk’
rm -Rf igsc.so ./rust/target/release/libigsc.a RcppExports.o entrypoint.o proc_gtf_attr.o
clang++ -arch x86_64 -std=gnu++17 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I'/Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/Rcpp/include' -I/opt/R/x86_64/include -fPIC -falign-functions=64 -Wall -g -O2 -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c RcppExports.cpp -o RcppExports.o
clang -arch x86_64 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I'/Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/Rcpp/include' -I/opt/R/x86_64/include -fPIC -falign-functions=64 -Wall -g -O2 -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c entrypoint.c -o entrypoint.o
clang++ -arch x86_64 -std=gnu++17 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I'/Library/Frameworks/R.framework/Versions/4.3-x86_64/Resources/library/Rcpp/include' -I/opt/R/x86_64/include -fPIC -falign-functions=64 -Wall -g -O2 -UNDEBUG -Wall -pedantic -g -O0 -fdiagnostics-color=always -c proc_gtf_attr.cpp -o proc_gtf_attr.o
In some environments, ~/.cargo/bin might not be included in PATH, so we need
to set it here to ensure cargo can be invoked. It is appended to PATH and
therefore is only used if cargo is absent from the user's PATH.
if [ "true" != "true" ]; then
export CARGO_HOME=/Users/vonskopnik/Documents/R_packages/igsc/src/.cargo;
fi &&
export PATH="/usr/local/bin:/Users/vonskopnik/.cargo/bin:/Library/Frameworks/Python.framework/Versions/3.9/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Little Snitch.app/Contents/Components:/opt/X11/bin:/Users/vonskopnik/Applications/quarto/bin:/Library/TeX/texbin:/usr/texbin:/Applications/RStudio.app/Contents/Resources/app/quarto/bin:/Applications/RStudio.app/Contents/Resources/app/bin/postback:/Users/vonskopnik/.cargo/bin" &&
cargo build --lib --release --manifest-path=./rust/Cargo.toml --target-dir ./rust/target
Finished release profile [optimized] target(s) in 0.11s
if [ "true" != "true" ]; then
rm -Rf /Users/vonskopnik/Documents/R_packages/igsc/src/.cargo &&
rm -Rf ./rust/target/release/build;
fi
clang++ -arch x86_64 -std=gnu++17 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Library/Frameworks/R.framework/Resources/lib -L/opt/R/x86_64/lib -o igsc.so RcppExports.o entrypoint.o proc_gtf_attr.o -L./rust/target/release -ligsc -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
duplicate symbol '_R_init_igsc' in:
RcppExports.o
entrypoint.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [igsc.so] Error 1
ERROR: compilation failed for package ‘igsc’

@CGMossa
Copy link
Member

CGMossa commented Nov 25, 2024

Currently, we don't support rust and rcpp functions at the same time. The issue is actually very small, but we haven't gotten around to resolving it.

@JosiahParry
Copy link
Contributor

@CGMossa for my edification, what is that small problem? To me it looks like Rcpp is trying to export a symbol with the same name that extendr is also exporting

@Ilia-Kosenkov
Copy link
Member

I would not say it is that small. Currently, extendr generates the exports that are registered, IIRC, with R_registerRoutines. And we overwrite entrypoint.c. If I am correct, exports are not additive, and you can only have one function named after the packages exposed from the binary. Right now all of this is overtaken by extendr. One solution -- if you desperately need Rcpp there -- could be to try to register Rcpp code as FFI in Rust and then export this Rust function using extendr facilities. This will, however, require some non-trivial changes to makefiles.

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

4 participants