13

Such as boost, where can I specify the following:

1.External c++ header file include path 
2.External c++ source file 
3.External c++ link library file path
Xiaobo Gu
  • 199
  • 1
  • 10

3 Answers3

14

It all goes into src/Makevars as explained in

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • The title of the question brought me here; your paper https://arxiv.org/abs/1911.06416 may also be a useful link – user2957945 Feb 22 '20 at 19:06
  • I think more could be said in this answer. I don't find useful information in any of the links, or in the documentation for `sourceCpp`. Although I haven't downloaded the book, the question itself is pretty concise and I would expect a concise answer. Something like: "here are the arguments to `sourceCpp` that will allow you to specify paths to look for headers and libraries". I'm not seeing that information. Is it necessary to adopt the package structure with DESCRIPTION and so on, in order to use an external library with Rcpp? Thanks. – Metamorphic May 09 '20 at 22:10
  • 1) The answer is almost seven years old. 2) I have written many times on the topic, and one could go on forever. 3) There is newer writeup of mine [on arXiv](https://arxiv.org/abs/1911.06416) that will be a vignette in the next Rcpp release. – Dirk Eddelbuettel May 09 '20 at 22:14
  • @Dirk Thanks for your reply. The 2019 writeup doesn't have the information I'm looking for, it uses `src/Makevars`. I'm not sure what is so difficult about the question: Is it possible to specify a library to link against when using `sourceCpp`? If yes, how? And if no, why not, and why not make that part of the documentation? It's hard to think of a question which is more relevant to your product, given that `sourceCpp` is the easiest way to use Rcpp, and that linking to an external library to R is probably the top application of Rcpp. – Metamorphic May 11 '20 at 08:12
  • Very simple: If it were so simple, lots of people would do it. In short, it's not. Unless we're all wrong and maybe you know / can teach us how to do this is way that naturally works on all three OS over whose details `sourceCpp` provides a unified interface. – Dirk Eddelbuettel May 11 '20 at 11:10
  • I updated your answer with some of the information that I desire to be there. If those changes work for you, then we can delete these comments. Additionally, I am willing to submit an issue on your Rcpp GitHub project asking for `sourceCpp` documentation to acknowledge its more pertinent limitations. Perhaps that would also be a good place to discuss whether those limitations are necessary. – Metamorphic May 12 '20 at 00:57
  • I tend to reject other's people's edits to my answer. If I sign an answer with my name it is my answer. If you feel something is unsaid, the site allows you to say what you want to say _as a new answer_. No need to muddle two distinct answers. Thanks for your understanding. Now, as for additional documentation to Rcpp I would suggest to create a new GitHub repo or gist and just write up what you think needs to be said and then invite Rcpp users (eg via the rcpp-devel list) for feedback and input. – Dirk Eddelbuettel May 12 '20 at 01:45
  • 1
    I just put it into another answer. Thanks for explaining. – Metamorphic May 12 '20 at 21:49
  • 2
    @DirkEddelbuettel I'd strongly suggest taking the time to review [the SO "how to answer" faq](https://stackoverflow.com/help/how-to-answer). Neither your original answer nor your recent comments provide context for the links; Metamorphic did. The [expected behavior](https://stackoverflow.com/help/behavior) and [code of conduct](https://stackoverflow.com/conduct) might be helpful as well. As the primary SME on this topic, you may have forgotten that others do not have your intimate knowledge of the tool you wrote. If your answer is outdated as you suggest, why not update or delete it? – CJ Harries May 30 '20 at 14:49
5

Dirk's paper "Thirteen Simple Steps for Creating An R Package with an External C++ Library" gives an example src/Makevars:

CXX_STD = CXX11
PKG_CFLAGS = -I. -DGMP -DSKIP_MAIN
PKG_LIBS = $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS) -lgmpxx -lgmp

As you can see, additional libraries are specified in PKG_LIBS in this file. The src/Makevars approach assumes that you incorporate C++ code into your project using a standard package layout, as produced by Rcpp.package.skeleton(), with NAMESPACE and DESCRIPTION and so on.

According to Dirk's comments above, there is currently no way to specify an external library when C++ code is incorporated using the sourceCpp function, because that function provides an interface which is supposed to be multi-platform.

Metamorphic
  • 732
  • 6
  • 16
  • The answer is correct, but covers only a portion of the possible settings: what if the library you want to work against is _not_ a system library? Common case, and no "general answer" (for all cases and OSs), sadly. – Dirk Eddelbuettel May 12 '20 at 22:17
0

Where I work, researchers are using Rcpp not to create packages but to incorporate C++ scripts in non-standard locations into their work in R 'on the fly' in a Linux OS.

We wanted to include multiple external C++ libraries but the following did not work:

Sys.setenv("PKG_LIBS = -lglpk -lsuperlu") 

For Rcpp to find them, we had to add -L for the paths and put them ahead of the -l statements:

Sys.setenv("PKG_LIBS= -L/PATH/TO/GLPK/LIB -L/PATH/TO/SUPERLU -lglpk -lsuperlu")  
user20650
  • 24,654
  • 5
  • 56
  • 91
John
  • 1
  • 1