<p>That is a non-trivial question. In the simplest case, use a 'hook' offered by <code>configure</code> and <code>configure.win</code> to pre-build a (static) library you ship in your sources and then link your package to that.</p>
<p>That said, the <a href="https://rstudio.github.io/r-manuals/r-exts/" rel="nofollow noreferrer"><em>Writing R Extensions</em></a> manual and/or the <a href="https://cran.r-project.org/web/packages/policies.html" rel="nofollow noreferrer"><em>CRAN Repository Policy</em></a> (both of which are <em>the</em> references here) expressed more of a preference for an <em>external</em> library -- which may not be an option here if PCL is too exotic.</p>
<p>As the topic comes up with Rcpp, I wrote a short paper about it (at arXiv <a href="https://arxiv.org/abs/1911.06416" rel="nofollow noreferrer">here</a>) which is also included as a <a href="https://cloud.r-project.org/web/packages/Rcpp/vignettes/Rcpp-libraries.pdf" rel="nofollow noreferrer">vignette in the package</a>. It requires a few pages to cover the common cases but even then it cannot cover all.</p>
<p>Your main source of reference may be CRAN. The are <em>lots</em> of packages in this space. A few of mine use external libraries, I contributed to package <code>nloptr</code> which uses a hybrid approach (&quot;use system library if found, else build&quot;) and some like <code>httpuv</code> always build (a small-ish library).</p>
Rcpp with structured library in /src
<p>I'm trying to write a wrapper for a C++ function I've written, making use of the Point Clouds Library (PCL). This is my first try interfacing R and C++, so I apologise if any solution is too trivial. My goal is to make a few functions available for myself and my colleagues directly in R, on <strong>mac and windows</strong>. My example function <code>cloudSize</code> is included at the bottom of the text. I will try to be as clear as possible.</p>
<p>I've installed PCL with the vcpkg package manager for winx64 at <code>C:\src\vcpkg\vcpkg</code>.
This is added to my Environmental Variable Path for my user.</p>
<p>I created an empty R-package with <code>Rcpp.package.skeleton()</code>:
<code>C:/User/csvi0001/Desktop/GitHub/RPCLpackage/PCLR</code></p>
<p>PCL is a massive library, but thankfully modular,and so I only #include the headers that are needed to compile the executable: <code>pcl/io/pcd_io.h</code>, <code>pcl/point_types.h</code>, <code>pcl/registration/icp.h</code>.</p>
<p>Now, since I'd like this to work on more than one OS - and therefore compile on install (?) - I should use a dynamic library? I'll presume that the person installing my package already has a compiled copy of pcl. However, I do not know how to find a flag showing that pcl is installed - how do I find these for inclusion in Makevars(?). CMake must find them when testing the C++ function in VSCode after adding an include path. In lieu of this:</p>
<p>I copy the pcl folder installed by vcpkg to ./src . When I tried copying all the .h files, they seemed to lose track of one another as they refer to eachother through which module they are placed in, e.g. &lt;pcl/memory.h&gt; cannot be found if memory.h is placed directly in ./src. However, flattening the structure of the modules means that every single dependency and #include must be manually changed, in some cases there are also files with the same name in different folders. e.g. pcl/kdtree.h and pcl/search/kdtree.h. After this, it must be done again when replacing &lt; &gt; with &quot; &quot; for each header.</p>
<p><strong>Is there any way of telling Rcpp that the library included in /src is structured?</strong></p>
<p>I'm working on Win 10 winx64.</p>
<p>Since I'm making use of the depends RcppEigen and BH; and I must have C++14 or higher (choice: C++17) I add to my DESCRIPTION file:</p>
<pre><code>LinkingTo: Rcpp, RcppEigen, BH
SystemRequirements: C++17
</code></pre>
<p>My actual C++ function:</p>
<pre><code>//PCL requires at least C++14
//[[Rcpp::plugins(cpp17)]]
//[[Rcpp::depends(RcppEigen)]]
//[[Rcpp::depends(BH)]] 

#include &lt;Rcpp.h&gt;
#include &lt;iostream&gt;
#include &quot;pcl/io/pcd_io.h&quot;
#include &quot;pcl/point_types.h&quot;
#include &quot;pcl/registration/icp.h&quot;

//[[Rcpp::export]]
int cloudSize(Rcpp::DataFrame x)
{
  
  pcl::PointCloud&lt;pcl::PointXYZ&gt; sourceCloud;
  for(int i=0;i&lt;x.nrows();i++)
  {
    sourceCloud.push_back(pcl::PointXYZ(x[0][i],x[1][i],x[2][i])); //This way of referring to elements in a Rcpp::DataFrame may be erroneous.
  }
  
  int cloudSize = sourceCloud-&gt;size();
        
    
 return (cloudSize);

}
</code></pre>