<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/9-compacting-your-shared-libraries/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://dirk.eddelbuettel.com/blog/2017/08/14#009_compact_shared_libraries"> Thinking inside the box </a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Welcome to the nineth post in the <em>recognisably rancid R randomness</em> series, or <span><em>R</em><sup>4</sup></span> for short. Following on the heels of <a rel="nofollow" href="http://dirk.eddelbuettel.com/blog/2017/08/10#008_aspell_cran_incoming" target="_blank">last week’s post</a>, we aim to look into the shared libraries created by R.</p>
<p>We love the R build process. It is robust, cross-platform, reliable and rather predicatable. It. Just. Works.</p>
<p>One minor issue, though, which has come up once or twice in the past is the (in)ability to fully control all compilation options. R will always recall <code>CFLAGS</code>, <code>CXXFLAGS</code>, … etc as used when <em>it</em> was compiled. Which often entails the <code>-g</code> flag for debugging which can seriously inflate the size of the generated object code. And once stored in <code>${RHOME}/etc/Makeconf</code> we <em>cannot</em> on the fly override these values.</p>
<p>But there is always a way. Sometimes even two.</p>
<p>The first is <em>local</em> and can be used via the (personal) <code>~/.R/Makevars</code> file (about which I will have to say more in another post). But something I have been using quite a bite lately uses the flags for the shared library linker. Given that we can have different code flavours and compilation choices—between C, Fortran and the different C++ standards—one can end up with a few lines. I currently use this which uses <code>-Wl,</code> to pass an the <code>-S</code> (or <code>--strip-debug</code>) option to the linker (and also reiterates the desire for a shared library, presumably superfluous):</p>
<pre><code>SHLIB_CXXLDFLAGS = -Wl,-S -shared
SHLIB_CXX11LDFLAGS = -Wl,-S -shared
SHLIB_CXX14LDFLAGS = -Wl,-S -shared
SHLIB_FCLDFLAGS = -Wl,-S -shared
SHLIB_LDFLAGS = -Wl,-S -shared</code></pre>
<p>Let’s consider an example: my most <a rel="nofollow" href="http://dirk.eddelbuettel.com/blog/2017/08/13#rprotobuf_0.4.10" target="_blank">recently</a> uploaded package <a rel="nofollow" href="http://dirk.eddelbuettel.com/code/rprotobuf.html" target="_blank">RProtoBuf</a>. Built under a standard 64-bit Linux setup (Ubuntu 17.04, g++ 6.3) and <em>not</em> using the above, we end up with library containing 12 megabytes (!!) of object code:</p>
<pre><code>edd@brad:~/git/rprotobuf(feature/fewer_warnings)$ ls -lh src/RProtoBuf.so
-rwxr-xr-x 1 edd edd 12M Aug 14 20:22 src/RProtoBuf.so
edd@brad:~/git/rprotobuf(feature/fewer_warnings)$ </code></pre>
<p>However, if we use the flags shown above in <code>.R/Makevars</code>, we end up with <em>much</em> less:</p>
<pre><code>edd@brad:~/git/rprotobuf(feature/fewer_warnings)$ ls -lh src/RProtoBuf.so 
-rwxr-xr-x 1 edd edd 626K Aug 14 20:29 src/RProtoBuf.so
edd@brad:~/git/rprotobuf(feature/fewer_warnings)$ </code></pre>
<p>So we reduced the size from 12mb to 0.6mb, an 18-fold decrease. And the <code>file</code> tool still shows the file as ‘not stripped’ as it still contains the symbols. Only debugging information was removed.</p>
<p>What reduction in size can one expect, generally speaking? I have seen <em>substantial</em> reductions for C++ code, particularly when using tenmplated code. More old-fashioned C code will be less affected. It seems a little difficult to tell—but this method is my new build default as I continually find rather substantial reductions in size (as I tend to work mostly with C++-based packages).</p>
<p>The second option only occured to me this evening, and complements the first which is after all only applicable <em>locally</em> via the <code>~/.R/Makevars</code> file. What if we wanted it affect each installation of a package? The following addition to its <code>src/Makevars</code> should do:</p>
<pre><code>strippedLib: $(SHLIB)
        if test -e &quot;/usr/bin/strip&quot;; then /usr/bin/strip --strip-debug $(SHLIB); fi

.phony: strippedLib</code></pre>
<p>We declare a new <code>Makefile</code> target <code>strippedLib</code>. But making it dependent on <code>$(SHLIB)</code>, we ensure the standard target of this <code>Makefile</code> is built. And by making the target <code>.phony</code> we ensure it will <em>always</em> be executed. And it simply tests for the <code>strip</code> tool, and invokes it on the library after it has been built. Needless to say we get the same reduction is size. And this scheme may even pass muster with CRAN, but I have not yet tried.</p>
<p>Lastly, and acknowledgement. Everything in this post has benefited from discussion with my former colleague <a rel="nofollow" href="https://github.com/dcdillon" target="_blank">Dan Dillon</a> who went as far as setting up tooling in his <a rel="nofollow" href="https://github.com/dcdillon/r-stripper" target="_blank">r-stripper</a> repository. What we have here may be simpler, but it would not have happened with what Dan had put together earlier.</p>
<p>
This post by <a rel="nofollow" href="http://dirk.eddelbuettel.com/" target="_blank">Dirk Eddelbuettel</a> originated on his <a rel="nofollow" href="http://dirk.eddelbuettel.com/blog/" target="_blank">Thinking inside the box</a> blog. Please report excessive re-aggregation in third-party for-profit settings.</p>
<p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://dirk.eddelbuettel.com/blog/2017/08/14#009_compact_shared_libraries"> Thinking inside the box </a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/3rJcd-4miq8" height="1"></a></div>