1

I was trying to run this peace of code in R (credit to the author):

require(Rcpp)
require(RcppArmadillo)
require(inline)
cosineRcpp <- cxxfunction( 
  signature(Xs = "matrix"), 
  plugin = c("RcppArmadillo"),
  body='
    Rcpp::NumericMatrix Xr(Xs);  // creates Rcpp matrix from SEXP
    int n = Xr.nrow(), k = Xr.ncol();
    arma::mat X(Xr.begin(), n, k, false); // reuses memory and avoids extra copy
    arma::mat Y = arma::trans(X) * X; // matrix product
    arma::mat res = (1 - Y / (arma::sqrt(arma::diagvec(Y)) * arma::trans(arma::sqrt(arma::diagvec(Y)))));
    return Rcpp::wrap(res);
')

And got, after few fixes, the following error:

Error in compileCode(f, code, language = language, verbose = verbose) : 
  Compilation ERROR, function(s)/method(s) not created! 
clang: error: no such file or directory: '/usr/local/lib/libfontconfig.a'
clang: error: no such file or directory: '/usr/local/lib/libreadline.a'
make: *** [file5a681e35ebe1.so] Error 1
In addition: Warning message:
running command '/Library/Frameworks/R.framework/Resources/bin/R CMD SHLIB file5a681e35ebe1.cpp 2> file5a681e35ebe1.cpp.err.txt' had status 1 

I used to use Rcpp a lot in the past. But between now and then my computer has been reformatted and all the installation re-done using homebrew.

I installed cairo with brew: brew install cairo

the libreadline.a error was solved with:

brew link --force readline

But the same did not work for libfontconfig.a since was already linked:

brew link --force fontconfig
Warning: Already linked: /usr/local/Cellar/fontconfig/2.11.1
To relink: brew unlink fontconfig && brew link fontconfig

I would have assumed that fontconfig is within cairo. In fact, when I type

brew install fontconfig
Warning: fontconfig-2.11.1 already installed

But the truth is that there is no libfontconfig.a at /usr/local/lib/:

ls /usr/local/lib/libfont*
/usr/local/lib/libfontconfig.1.dylib 
/usr/local/lib/libfontconfig.dylib

Using the very questionable approach of going here and download it, the code runs, but still gives a the corresponding warning, since the file corresponds to a different os.x architecture (I did not found one for 10.9):

+ . + ld: warning: ignoring file /usr/local/lib/libfontconfig.a, missing required architecture x86_64 in file /usr/local/lib/libfontconfig.a (2 slices)

So at this stage I am a little lost.

How do I install libfontconfig.a or find the 10.9 version?

In case is of any use, I have Xcode installed, I am on a Mac 10.9.5, and based on this very nice and detailed answer my ~/.R/Makevars file looks like:

CC=clang
CXX=clang++
FLIBS=-L/usr/local/bin/
Community
  • 1
  • 1
Javier
  • 1,530
  • 4
  • 21
  • 48
  • I found this link: https://github.com/olarivain/iTunesServer/blob/master/libraries/HandBrake/contrib/libfontconfig.a , and the file in here works. Still, I would be interested in knowing if can be directly installed with Homebrew. – Javier Feb 25 '16 at 17:12

1 Answers1

4

Your system setup is broken. Neither R nor Rcpp have anything to do with clang (unless you chose clang as your system compiler) or fontconfig.

So start simpler:

R> library(Rcpp)
R> evalCpp("2 + 2")
[1] 4
R>

This just showed that my system has a working compiler R (and Rcpp) can talk to. We can it more explicit:

R> evalCpp("2 + 2", verbose=TRUE)

Generated code for function definition: 
--------------------------------------------------------

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
SEXP get_value(){ return wrap( 2 + 2 ) ; }

No rebuild required (use rebuild = TRUE to force a rebuild)

[1] 4
R>

and R is clever enough not to rebuild. We can then force a build

R> evalCpp("2 + 2", verbose=TRUE, rebuild=TRUE)

Generated code for function definition: 
--------------------------------------------------------

#include <Rcpp.h>

using namespace Rcpp;

// [[Rcpp::export]]
SEXP get_value(){ return wrap( 2 + 2 ) ; }

Generated extern "C" functions 
--------------------------------------------------------


#include <Rcpp.h>
// get_value
SEXP get_value();
RcppExport SEXP sourceCpp_0_get_value() {
BEGIN_RCPP
    Rcpp::RObject __result;
    Rcpp::RNGScope __rngScope;
    __result = Rcpp::wrap(get_value());
    return __result;
END_RCPP
}

Generated R functions 
-------------------------------------------------------

`.sourceCpp_0_DLLInfo` <- dyn.load('/tmp/Rtmpeuaiu4/sourcecpp_6a7c7c8295fc/sourceCpp_2.so')

get_value <- Rcpp:::sourceCppFunction(function() {}, FALSE, `.sourceCpp_0_DLLInfo`, 'sourceCpp_0_get_value')

rm(`.sourceCpp_0_DLLInfo`)

Building shared library
--------------------------------------------------------

DIR: /tmp/Rtmpeuaiu4/sourcecpp_6a7c7c8295fc

/usr/lib/R/bin/R CMD SHLIB -o 'sourceCpp_2.so' --preclean  'file6a7c6d1fc2d6.cpp'  
ccache g++ -I/usr/share/R/include -DNDEBUG    -I"/usr/local/lib/R/site-library/Rcpp/include" -I"/tmp/Rtmpeuaiu4"    -fpic  -g -O3 -Wall -pipe -Wno-unused -pedantic -c file6a7c6d1fc2d6.cpp -o file6a7c6d1fc2d6.o
g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o sourceCpp_2.so file6a7c6d1fc2d6.o -L/usr/lib/R/lib -lR
[1] 4
R> 

and on that you see system details on my side (Linux, also using ccache) that will be different for you.

After that, try (Rcpp)Armadillo one-liners and so on.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
  • All that you wrote worked perfectly on my system. Perhaps I should have started by removing the CC=clang , CXX=clang++ in `~/.R/Makevars` (was there by default, since I never typed it. Anyway, Thanks (as always) and thankfully Rcpp is now running as fast and nice as always. (Still, if I remove the simlink to readline or the libfontconfig.a file, it will not work, but....I will leave it running for now) – Javier Feb 25 '16 at 18:12
  • Also, I read the following from here: http://r-pkgs.had.co.nz/src.html#make : "Use clang instead of gcc to compile your C++ code: it gives much better error messages. You can make clang the default by creating a .R/Makevars (linux and mac) or .R/Makevars.win (windows) file in your home directory that contains: `CXX=clang++` ". This is why I first assumed that clang had something to do with Rcpp :). – Javier Feb 25 '16 at 18:22
  • 5
    Dr Wickham has a very unfortunate tendency to make statements that sounds as if they were universally true. As this proposal appears to have broken your system, I suggest you complain to him. We make no such suggestion (but reference clang as one possible compiler on OS X). – Dirk Eddelbuettel Feb 25 '16 at 18:34
  • Also, R does NOT create `~/.R/Makevars` so you must have copied it at some point. Some time or other, we all fall for things said on these Interwebs ... – Dirk Eddelbuettel Feb 25 '16 at 18:38
  • :) !! ok Dirk, so, what do you suggest? All that you wrote in your reply works just fine, and at the moment all my Rcpp code that I have tried is running (not only the one shown in my question). However, I rather have a "non-broken" R-system, of course. Therefore, anything you might suggest I am happy to try it, I mean, you wrote the package, so... – Javier Feb 25 '16 at 18:49
  • Yes, and the package works for everybody including nightly CRAN tests. I suggests you follow the R documentation on how to work with R and compilers. We recently added some more text on to the [Rcpp FAQ](https://github.com/RcppCore/Rcpp/blob/master/vignettes/Rcpp-FAQ.Rnw) -- so try searching in that Rnw source for OS X and follow the links, the published pdf is behind. _Our package works, and we cannot set your system up for you_ but give you pointers. – Dirk Eddelbuettel Feb 25 '16 at 19:39
  • thanks, and no worries, I was not asking you to set my system, not at all. I will try to optimise the system. Thanks again – Javier Feb 25 '16 at 21:30