15

I have a new iMac and I'm trying to run code using the Rcpp library that has been working on both my old iMac and Macbook Pro without issue. I have tried everything I can't seem to figure out what the issue is.

Xcode 5.0 downloaded. Command line Tools then installed. R3.0.2 is installed. I downloaded a gcc compiler. When I type gcc in terminal - I get "clang:" - which is good, I think.

The error I get is copied below. Thanks in advance for any ideas and advice.

Error (in R console):
llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include  -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include"    -fPIC  -mtune=core2 -g -O2  -c SBM-Ccode.cpp -o SBM-Ccode.o 
Error in sourceCpp("SBM-Ccode.cpp") : 
  Error 1 occurred building shared library.

WARNING: The tools required to build C++ code for R were not found.

Please install Command Line Tools for XCode (or equivalent).

/bin/sh: llvm-g++-4.2: command not found
make: *** [SBM-Ccode.o] Error 127
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
user2904609
  • 151
  • 1
  • 1
  • 3
  • 1
    What version of Mac OS / XCode where you using, and what versions are you using right now? – Paul Hiemstra Oct 21 '13 at 20:35
  • You type `gcc` and you get `clang:`? That doesn't make much sense; `gcc` and `clang` are separate toolchains for compilation. XCode comes with an (old, but compatible) version of `gcc` for compilation; where did you get your new version of `gcc`? Are you able to run `llvm-g++-4.2` (which should be the XCode version of `g++`) from the command line? – Kevin Ushey Oct 21 '13 at 21:29
  • Kevin: I *think* R remembers g++-4.2.1 from its build. The OP states that he added clang to his sytem (via XCode 5.0). The question now is now how to tell R to use clang; see my answers and Romain's earlier answer. – Dirk Eddelbuettel Oct 21 '13 at 21:51
  • I have `CXX = llvm-g++-4.2 -arch x86_64` in the `Makeconf` file Dirk mentionned. This is an R installed right out of the binary from CRAN. Nothing fancy. – Romain Francois Oct 21 '13 at 22:02
  • But apparently one must override it to make R work out of the box. I score one for my assertion that OS X ain't as easy as its fans make it out to be. And where do people get their Fortran compilers and libraries (needed for some CRAN packages) from? Homebrew? – Dirk Eddelbuettel Oct 21 '13 at 22:05
  • You need not modify anything to have R work out of the box. Install Xcode and its command line tools. That's it. No #bs, no need for homebrew. If you want to compile packages with fortran, follow what is indicated here http://cran.r-project.org/bin/macosx/ i.e. the page you visit when you install R for OSX. This will lead you here: http://cran.r-project.org/bin/macosx/tools/ where you can find a nicely packed `gfortran`. But when you don't need to compile a package with fortran code (this is most of the time), you don't need this. – Romain Francois Oct 21 '13 at 22:13
  • So why doesn't it work for the OP then? Not to be pedantic, but if you must override CXX then it by definition does not work out of the box. – Dirk Eddelbuettel Oct 21 '13 at 22:26
  • You don't "must". I do because I want clang and not llvm-g++-4.2. My guess is as good as the error message: "The tools required to build C++ code for R were not found". Perhaps the OP can come back to this question. – Romain Francois Oct 22 '13 at 08:19
  • @Romain we are *not* talking about your case. It is the OP's question. And to recap: He a) has a fresh mac, b) got XCode (AFAIK the recommended dev.env. on OS X), c) installed R and d) cannot compile packages. Hence R *does not work out of the box* for package development as one must make adjustments. – Dirk Eddelbuettel Oct 22 '13 at 11:46
  • The OP does not seem to reply to any questions on comments or answers. My guess is that his "I downloaded a gcc compiler" means he has installed something he should not have. This sentence was not part of your abcd proof. But who knows, the OP has disappeared. For all I know, if Xcode and its command line tools are installed, you can compile packages out of the box. – Romain Francois Oct 22 '13 at 12:09
  • Anyway SO is tired of these comments and so am I. So unless the OP has something to share, I'm no longer commenting on this thread. – Romain Francois Oct 22 '13 at 12:10
  • You too state that you too have `CXX = llvm-g++-4.2` in Makeconf. Explain to me how this is useable with XCode providing clang 3.*. To me, it looks like a fail and *not* out-of-the box usability (and has nothing to do with whether the OP did or did not install gcc as well as XCode). Now, our documentation (Rcpp FAQ, other places) says "install XCode and you are done" which no longer seems true. – Dirk Eddelbuettel Oct 22 '13 at 12:15
  • OP here. Thank you all very much for these comments. I am using OS X 10.8.5 and Xcode 5.0. My laptop is using 10.7.5 and Xcode 4.6.3 (and this works without me doing anything special). I'm sorry I didn't realize anyone had seen this thread. I'm very new to all of this. I was googling to try to find out what was happening which lead me to download the gcc compiler. I erased everything this morning (xcode and gcc compiler) and downloaded Xcode 4.6.3. I am still having the same issue. – user2904609 Oct 22 '13 at 15:46
  • OP again - when I type in gcc - I still get clang. And if I type in llvm-g++-4.2, I get "command not found". I hope this helps. – user2904609 Oct 22 '13 at 15:49
  • OP again - On my old laptop, I don't have clang. So perhaps the best thing for me to do is to not have clang. – user2904609 Oct 22 '13 at 16:01
  • That's not entirely true. `clang` is now the default compiler toolchain on Mac with XCode 5.0. The unfortunate consequence at this point (?) seems to be that Mac users need to set the `~/.R/Makevars` file; see Romain's answer. If you want `gcc` now on Mac, you need to get it through Homebrew, Macports, etc... – Kevin Ushey Oct 22 '13 at 16:48

3 Answers3

18

I'm not sure what you mean by "I downloaded a gcc compiler". You don't need to download your own gcc. You can use either the default or use clang++ by having something like this in your ~/.R/Makevars file:

CC=clang
CXX=clang++
CXXFLAGS= -O3 -pedantic

What happens when you try devtools::has_devel:

> require(devtools)
> has_devel()
'/Library/Frameworks/R.framework/Resources/bin/R' --vanilla CMD SHLIB foo.c

clang -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG  -I/usr/local/include    -fPIC  -mtune=core2 -g -O2  -c foo.c -o foo.o
clang -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -L/usr/local/lib -o foo.so foo.o -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
[1] TRUE

or Rcpp::evalCpp:

> require(Rcpp)
> evalCpp( "1+1")
[1] 2    
Romain Francois
  • 17,432
  • 3
  • 51
  • 77
  • When I do the first, I get the error: '/Library/Frameworks/R.framework/Resources/bin/R' --vanilla CMD SHLIB foo.c llvm-gcc-4.2 -arch x86_64 -std=gnu99 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -fPIC -mtune=core2 -g -O2 -c foo.c -o foo.o make: llvm-gcc-4.2: No such file or directory make: *** [foo.o] Error 1 Error: Command failed (1) – user2904609 Oct 22 '13 at 15:54
  • When I do the second, I get the same error as in my OP. llvm-g++-4.2 -arch x86_64 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -fPIC -mtune=core2 -g -O2 -c file1818236400cd.cpp -o file1818236400cd.o – user2904609 Oct 22 '13 at 15:55
  • Error in sourceCpp(code = code, env = env, rebuild = rebuild, showOutput = showOutput, : Error 1 occurred building shared library. WARNING: The tools required to build C++ code for R were not found. Please install Command Line Tools for XCode (or equivalent). /bin/sh: llvm-g++-4.2: command not found make: *** [file1818236400cd.o] Error 127 – user2904609 Oct 22 '13 at 15:56
  • 1
    Did you restart your R session? This should work for you; the fact that `llvm-g++-4.2` is still being searched for is strange. In fact, `clang` is now the only compiler in XCode 5.0, so `llvm-g++-4.2` is no longer available. This explains the somewhat strange error in the OP. – Kevin Ushey Oct 22 '13 at 16:45
  • 1
    THANK YOU ALL! I restarted and things are now working. I really appreciate the help. – user2904609 Oct 22 '13 at 17:51
8

Quick guess:

  1. You are running the pre-built R binary which Simon built / CRAN provides.

  2. R stores its configuration options from its compile time, those influence its run-time.

  3. Check via the file $R_HOME/etc/Makeconf and look at CC and CXX.

  4. As Romain suggested, override CC and CXX via a file ~/.R/Makevars.

  5. Try again.

Edit: I just confirmed with a colleague who has the exact same issue on a Mac OS X which he just upgrades to XCode 5 -- one now needs to override CC and CXX as R was built with the previous version of XCode.

Dirk Eddelbuettel
  • 360,940
  • 56
  • 644
  • 725
5

A easier solution would be the following. You should soft link the llvm compiler, in the terminal type:

cd /usr/bin
sudo ln -fs clang llvm-gcc-4.2
sudo ln -fs clang++ llvm-g++-4.2

Note: This also works for mex in Matlab.

Mobiletainment
  • 22,201
  • 9
  • 82
  • 98
Housen
  • 106
  • 3