Eisek
Eisek

Reputation: 31

How to change which C++ version compiler is used when installing an R package from source?

I'm trying to install an R package from source using remotes::install_github("pkgname"). I get the following error:

/bin/sh: /usr/local/Cellar/gcc/9.2.0/bin/g++-7: No such file or directory

I checked and I have g++-9 instead. I've been looking for a way to change the settings to direct to the correct path but so far have had no luck. I'm not even sure which package to look in or whether to approach R config files. Any help would be greatly appreciated.

I'm using R version 3.6.3 on Mac OSX 10.15.5 (Catalina). I have XCode developer tools installed but whenever I run checks RStudio suggests that I install them as if it can't find it.

Many thanks

Upvotes: 1

Views: 3378

Answers (3)

qwr
qwr

Reputation: 10929

Create .R/Makevars and put in your compiler path and options.

Example of what you can put:

CXX = /home/qwr/toolchains/bin/g++ -std=gnu++17
CXX17 = /home/qwr/toolchains/bin/g++ 
CXX17FLAGS = -g -O2 $(LTO)
CXX17PICFLAGS = -fpic 
CXX17STD = -std=gnu++17

Here are my defaults on RHEL 7.9 in $(R RHOME)/etc/Makeconf, which is missing newer compilers:

CXX = g++ -std=gnu++11
CXXCPP = $(CXX) -E
CXXFLAGS = -g -O2 $(LTO)
CXXPICFLAGS = -fpic
CXX98 = g++
CXX98FLAGS = -g -O2 $(LTO)
CXX98PICFLAGS = -fpic
CXX98STD = -std=gnu++98
CXX11 = g++
CXX11FLAGS = -g -O2 $(LTO)
CXX11PICFLAGS = -fpic
CXX11STD = -std=gnu++11
CXX14 = 
CXX14FLAGS =  $(LTO)
CXX14PICFLAGS = 
CXX14STD = 
CXX17 = 
CXX17FLAGS =  $(LTO)
CXX17PICFLAGS = 
CXX17STD = 

Upvotes: 0

bats_n_stats
bats_n_stats

Reputation: 51

It looks like R can't see your g++ compiler, as you suggest.

1 Checking if g++ is on PATH Did you install R with homebrew? Since the error path is pointing at /usr/local/Cellar, that seems to be the case.

You can see if the g++ compiler is on path by opening the Terminal program on Mac OS and typing:

echo $CXX

If it returns null (or nothing), there's nothing on path there.

2 Install g++ in brew Let's install g++ in brew. Open your Terminal and run the following, pressing enter after each line:

brew update
brew install gcc48
brew doctor

After this, please restart your computer.

3 Check if g++ is on PATH now

Run echo $CXX again on Terminal. If that still doesn't work, see this current issue:

https://github.com/r-lib/rlang/issues/754

I suggest if that didn't work to try to install XCode developer tools again. You can do that as below:

xcode-select --install

Upvotes: 4

Dirk is no longer here
Dirk is no longer here

Reputation: 368261

Besides what @bats_n_stats (great handle!) wrote, the following holds:

Each Rcpp use is driven by R itself

R can tell us what it uses via R CMD config calls. On my system

edd@rob:~$ R CMD config CXX
ccache g++
edd@rob:~$ 

meaning the (standard) value of g++ (no qualified version) is used in conjunction with the accelerating cache provided by ccache (different topic, I blogged about it in the past)

You can also look at that via grep in the actual conffile (which I look up directly below)

edd@rob:~$ grep ^CXX $(R RHOME)/etc/Makeconf
CXX = g++ -std=gnu++11
CXXCPP = $(CXX) -E
CXXFLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-Do_dS_/r-base-4.0.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g $(LTO)
CXXPICFLAGS = -fpic
CXX11 = g++
CXX11FLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-Do_dS_/r-base-4.0.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g $(LTO)
CXX11PICFLAGS = -fpic
CXX11STD = -std=gnu++11
CXX14 = g++
CXX14FLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-Do_dS_/r-base-4.0.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g $(LTO)
CXX14PICFLAGS = -fpic
CXX14STD = -std=gnu++14
CXX17 = g++
CXX17FLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-Do_dS_/r-base-4.0.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g $(LTO)
CXX17PICFLAGS = -fpic
CXX17STD = -std=gnu++17
CXX20 = g++
CXX20FLAGS = -g -O2 -fdebug-prefix-map=/build/r-base-Do_dS_/r-base-4.0.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g $(LTO)
CXX20PICFLAGS = -fpic
CXX20STD = -std=gnu++2a
CXX_VISIBILITY = -fvisibility=hidden
edd@rob:~$ 

This values all come from when R itself was built. So if that bites on your Catalina system you need to review where you got R from and what can possibly be wrong there. Use of Cellar suggests something macOS specific and likely not the suggested R from the https://mac.r-project.org/ page.

Upvotes: 5

Related Questions