Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error in model fit: C++14 standard requested but CXX14 is not defined #569

Open
RichardIanBarnes opened this issue Oct 15, 2018 · 26 comments

Comments

@RichardIanBarnes
Copy link

@RichardIanBarnes RichardIanBarnes commented Oct 15, 2018

Summary:

Error message relating to C++compiler when running a linear model fit.

Description:

I am new to Stan and RStan and trying out the tutorial by Max Farrell and Isla Myers-Smith on:
https://ourcodingclub.github.io/2018/04/17/stan-intro.html

The step by step process is shown below under 'Reproducible Steps'.

When I run the stanc() it compiles fine. But when I fit the linear model I receive the following error message:
Error in compileCode(f, code, language = language, verbose = verbose) :
Compilation ERROR, function(s)/method(s) not created! Error in .shlib_internal(commandArgs(TRUE)) :
C++14 standard requested but CXX14 is not defined
Calls: -> .shlib_internal
Execution halted
In addition: Warning message:
In system(cmd, intern = !verbose) :
running command 'C:/PROGRA1/R/R-351.1/bin/x64/R CMD SHLIB file55c71d73e8e.cpp 2> file55c71d73e8e.cpp.err.txt' had status 1

I've searched everywhere for possible solutions. I reviewed the following issue posted by yanys7
#565

As suggested by bgoodri I created a ~/.R/Makevars file and inserted the following:
CXX14 = g++

However the problem still persists.

I don't know if this is a bug or if I'm doing something wrong.
Thanks in advance for any help you can provide.

Reproducible Steps:

This follows almost exactly the steps shown in the tutorial.

Sys.setenv(USE_CXX14 = 1)
library(rstan)
library(gdata)
library(bayesplot)
rstan_options(auto_write = TRUE)
options(mc.cores = parallel::detectCores())
seaice <- read.csv("seaice.csv", stringsAsFactors = F)
colnames(seaice)<-c("year", "extent_north", "extent_south")
x <- I(seaice$year - 1978)
y <- seaice$extent_north
N <- length(seaice$year)
stan_data <- list(N = N, x = x, y = y)
write("// Stan model for simple linear regression
data {
      int <lower = 1> N;
      vector[N] x; 
      vector[N] y;
      }
      
      parameters {
      real alpha; 
      real beta; 
      real<lower = 0> sigma;
      }
      
      model {
      y ~ normal(alpha + x * beta, sigma);
      }
      generated quantities {
      }, 
"stan_model1.stan")
stanc("stan_model1.stan")
stan_model1 <- "stan_model1.stan"
fit <- stan(file = stan_model1, data = stan_data, warmup = 500, iter = 1000, chains = 4, thin = 1)

Current Output:

The output from stanc("stan_model1.stan") is below:
$status
[1] TRUE

$model_cppname
[1] "model55c23451a60_stan_model1"

$cppcode
[1] "// Code generated by Stan version 2.17.0\n\n#include <stan/model/model_header.hpp>\n\nnamespace model55c23451a60_stan_model1_namespace {\n\nusing std::istream;\nusing std::string;\nusing std::stringstream;\nusing std::vector;\nusing stan::io::dump;\nusing stan::math::lgamma;\nusing stan::model::prob_grad;\nusing namespace stan::math;\n\ntypedef Eigen::Matrix<double,Eigen::Dynamic,1> vector_d;\ntypedef Eigen::Matrix<double,1,Eigen::Dynamic> row_vector_d;\ntypedef Eigen::Matrix<double,Eigen::Dynamic,Eigen::Dynamic> matrix_d;\n\nstatic int current_statement_begin__;\n\nstan::io::program_reader prog_reader__() {\n stan::io::program_reader reader;\n reader.add_event(0, 0, "start", "model55c23451a60_stan_model1");\n reader.add_event(19, 19, "end", "model55c23451a60_stan_model1");\n return reader;\n}\n\nclass model55c23451a60_stan_model1 : public prob_grad {\nprivate:\n int N;\n vector_d x;\n vector_d y;\npublic:\n model55c23451a60_stan_model1(stan::io::va...

$model_name
[1] "stan_model1"

$model_code
[1] "// Stan model for simple linear regression\ndata {\n int <lower = 1> N; // Sample size\n vector[N] x; //Predictor\n vector[N] y; // Outcome\n}\n\nparameters {\n real alpha; //Intercept\n real beta; // Slope\n real<lower = 0> sigma; //Error SD\n}\n\nmodel {\n y ~ normal(alpha + x * beta, sigma);\n}\n\ngenerated quantities {\n} // The posterior predictive distribution"
attr(,"model_name2")
[1] "stan_model1"

Expected Output:

If applicable, the output you expected from RStan.

RStan Version:

RStan version 2.17.3

R Version:

R Version 3.5.1

Operating System:

Windows 8 64bit

@bgoodri
Copy link
Contributor

@bgoodri bgoodri commented Oct 15, 2018

@RichardIanBarnes
Copy link
Author

@RichardIanBarnes RichardIanBarnes commented Oct 15, 2018

Hi

Thank you very much for replying so soon!
The ~/.R/Makevars is attached - I wasn't sure what file extension it should be - I found a similar one in one of the package folders that had a .win extension.

The Sys.getenv("R_MAKEVARS_USER") returns an empty string so it looks like, as you said, it isn't able to find it.

Makevars.txt
I must admit I need to do a bit of reading up to understand this aspect but if you could help with any pointers as to what I should be doing that would be great - thanks again.

@bgoodri
Copy link
Contributor

@bgoodri bgoodri commented Oct 17, 2018

Try it after changing ~/.R/Makevars to

CXX14 = g++ -std=c++1y

You may also want to add -Wno-unused-variable -Wno-unused-function to the CXX14FLAGS line.

@RichardIanBarnes
Copy link
Author

@RichardIanBarnes RichardIanBarnes commented Oct 18, 2018

Hi

I added those lines to the ~/.R/Makevars and it seems to have resolved this particular issue. Thank you very much for your help.

In fact - much to my shame - I just realised that there was a section on RStan installation for Windows which sets out the contents of the Makevars file! I hadn't followed these steps when I originally installed RStan (I just went straight to install.packages("rstan")) so that would explain how I ended up with this problem. Thank you for your patience and sorry I hadn't fully set out the steps that led to this error.

Having resolved this though I encountered another error with StanHeaders which I thought I had seen in one of the open issues but can't find it at the moment. I will try to find it again and then post on there.

@pescobar
Copy link

@pescobar pescobar commented Jan 17, 2019

I was also getting this error:

ld: chains.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
chains.o: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [rstan.so] Error 1
ERROR: compilation failed for package ‘rstan’

and I had to modify my ~/.R/Makevars like this:

CXX14 = g++ -std=c++1y -Wno-unused-variable -Wno-unused-function -fPIC

@colemonnahan
Copy link

@colemonnahan colemonnahan commented Mar 15, 2019

I'm having this issue, not locally but when I do a TravisCI build on my package that depends on rstan. Is there a recommended way to setup an rstan dependency to avoid this? Below is the output I get:

* installing *source* package ‘rstan’ ...
** package ‘rstan’ successfully unpacked and MD5 sums checked
** libs
Error in .shlib_internal(args) : 
  C++14 standard requested but CXX14 is not defined
* removing ‘/home/travis/R/Library/rstan’
Error in i.p(...) : 
  (converted from warning) installation of package ‘rstan’ had non-zero exit status
Calls: <Anonymous> ... with_rprofile_user -> with_envvar -> force -> force -> i.p
Execution halted
The command "Rscript -e 'deps <- devtools::dev_package_deps(dependencies = NA);devtools::install_deps(dependencies = TRUE);if (!all(deps$package %in% installed.packages())) { message("missing: ", paste(setdiff(deps$package, installed.packages()), collapse=", ")); q(status = 1, save = "no")}'" failed and exited with 1 during .
Your build has been stopped.

@colemonnahan
Copy link

@colemonnahan colemonnahan commented Mar 15, 2019

Nevermind, after much digging I needed to add this to my travis config file:

# from https://discourse.mc-stan.org/t/building-rstanarm-like-packagee-on-travis-with-c-14/6054/10?
before_install:
  - mkdir -p ~/.R
  - echo "CXX14FLAGS=-O3 -mtune=native -march=native -Wno-unused-variable -Wno-unused-function  -Wno-macro-redefined" >> ~/.R/Makevars
  - echo "CXX14=g++ -std=c++1y -fPIC" >> ~/.R/Makevars

This was necessary even though I'm not packaging a compiled Stan model.

@willtudorevans
Copy link

@willtudorevans willtudorevans commented Jun 11, 2019

I'm afraid I'm getting the same error message and can't resolve the issue.

Sys.getenv("R_MAKEVARS_USER") returns blank but the only makevars I can find is in RccpEigen/skeleton, where there are two of them. I've tried inserting:

CXX14 = g++ -std=c++1y

in both of them, but no luck. I'm afraid I don't have a computer science background so I may not be looking in the right place, or doing the right thing. Any additional guidance would be much appreciated.

@RichardIanBarnes
Copy link
Author

@RichardIanBarnes RichardIanBarnes commented Jun 11, 2019

Hi there - I need to rack my memory on this. Let me have a look later on and I'll try to figure out what I did to resolve it and get back to you.

@bob-carpenter
Copy link

@bob-carpenter bob-carpenter commented Jun 12, 2019

Sorry the doc's so confusing. We're certainly open to suggestions in improving it if you have any. It's really hard with all the platform-specific config that's shared across R applications.

You want the top-level Makevars for R. On my mac, that's in ~/.R/Makevars. You'll need to create it if it doesn't already exist. Mine looks like this:

CXX=g++ -arch x86_64  -ftemplate-depth-256 -stdlib=libstdc++
CXXFLAGS= -O3 -mtune=native $(LTO) -w #set_by_rstan (and Bob)
R_XTRA_CPPFLAGS =  -I$(R_INCLUDE_DIR)      #set_by_rstan
CXX=clang++
CC=clang

I set the -O3 -mtune-native config myself, but that's not the critical part.

@willtudorevans
Copy link

@willtudorevans willtudorevans commented Jun 18, 2019

Sorry unfortunately I'm still not having any luck with this.

I'm on Windows, I understand that the ~ means home directory, so home directory for R? I've tried putting the Makevars file in both:

C:\Program Files\R and
C:\Users\User\Documents\R

but I still get the same error message when trying to run a stan model:

Error in compileCode(f, code, language = language, verbose = verbose) : Compilation ERROR, function(s)/method(s) not created! Error in .shlib_internal(commandArgs(TRUE)) : C++14 standard requested but CXX14 is not defined Calls: <Anonymous> -> .shlib_internal Execution halted In addition: Warning message: In system(cmd, intern = !verbose) : running command 'C:/PROGRA~1/R/R-36~1.0/bin/x64/R CMD SHLIB file227c6da617cf.cpp 2> file227c6da617cf.cpp.err.txt' had status 1 Error in sink(type = "output") : invalid connection

Stan was working ok on my computer until I upgraded RStudio and R to version 3.6.0.

I've also tried searching for a makevars file on a computer where stan is working, to try and copy that structure for my computer, but I can't locate the file.

@bob-carpenter
Copy link

@bob-carpenter bob-carpenter commented Jun 18, 2019

You have to create a new Makevars.win from scratch (like everything else, the name's different on Windows). Have you tried running the script from section "Configuration of the C++ Toolchain":

The script will show you where it needs to go and I think will just create it all for you if you run it.

I'm re-opening this issue, as nobody's going to react to comments on a closed issue.

@bob-carpenter bob-carpenter reopened this Jun 18, 2019
@bgoodri
Copy link
Contributor

@bgoodri bgoodri commented Jun 18, 2019

@willtudorevans
Copy link

@willtudorevans willtudorevans commented Jun 19, 2019

Just do install.packages(" https://win-builder.r-project.org/1vUk5Gxm9QqM/rstan_2.19.1.zip", repos = NULL)

Unfortunately I receive the following error message:

Error in install.packages : zip file ‘ https://win-builder.r-project.org/1vUk5Gxm9QqM/rstan_2.19.1.zip’ not found

@willtudorevans
Copy link

@willtudorevans willtudorevans commented Jun 19, 2019

The main problem now seems to be R not being able to 'find' Rtools. Every time I run:

pkgbuild::has_build_tools(debug = TRUE)

it determines FALSE and asks me if I want to install 'additional tools'. It then recommends I uninstall the existing Rtools first (version 3.5) before installing Rtools. This process repeats itself now matter how many times i uninstall and reintsall Rtools. This seems to be a new problem only after I tried reinstalling Rtools to see if that solved the original problem. Originally this returned TRUE:

pkgbuild::has_build_tools(debug = TRUE)

Not sure what to try next!I should probably refer to the Rtools debug manual.

@willtudorevans
Copy link

@willtudorevans willtudorevans commented Jun 19, 2019

OK I seem to solve the Rtools issue by setting:

options(buildtools.check = NULL)

as detailed here rstudio/rstudio#3563. I'm still having the Makevars issue even after running the 'Configuration of the C++ Toolchain' script.. I can see the Makevars.win document in the ~/.R/ folder, and no matter if I populate it with the script's default settings or Bob's I get the same error message:

Error in compileCode(f, code, language = language, verbose = verbose) : Compilation ERROR, function(s)/method(s) not created! Error in .shlib_internal(commandArgs(TRUE)) : C++14 standard requested but CXX14 is not defined Calls: <Anonymous> -> .shlib_internal Execution halted In addition: Warning message: In system(cmd, intern = !verbose) : running command 'C:/PROGRA~1/R/R-36~1.0/bin/x64/R CMD SHLIB file22fc4479512d.cpp 2> file22fc4479512d.cpp.err.txt' had status 1 Error in sink(type = "output") : invalid connection

I'm not sure what next to try. I'm tempted to try a complete reinstallation of R, Rstudio, Rtools and Rstan to see if that helps.

@qdread
Copy link

@qdread qdread commented Jun 19, 2019

Just do install.packages(" https://win-builder.r-project.org/1vUk5Gxm9QqM/rstan_2.19.1.zip", repos = NULL)

Unfortunately I receive the following error message:

Error in install.packages : zip file ‘ https://win-builder.r-project.org/1vUk5Gxm9QqM/rstan_2.19.1.zip’ not found

There is a space at the beginning of the string (typo) in bgoodri's comment so just remove that.

@willtudorevans
Copy link

@willtudorevans willtudorevans commented Jun 19, 2019

There is a space at the beginning of the string (typo) in bgoodri's comment so just remove that.

I feel stupid for having missed this. Thanks so much qdread, Bob and Ben everything now seems to be working.

mrustl added a commit to KWB-R/kwb.flusshygiene.app that referenced this issue Jul 17, 2019
"Error in .shlib_internal(args) :
  C++14 standard requested but CXX14 is not defined
* removing ‘/home/travis/R/Library/rstan’"

by using solution: stan-dev/rstan#569 (comment)
mrustl added a commit to KWB-R/pkgmeta that referenced this issue Jul 17, 2019
"Error in .shlib_internal(args) : 
  C++14 standard requested but CXX14 is not defined
* removing ‘/home/travis/R/Library/rstan’"

by using solution: stan-dev/rstan#569 (comment)
@DiegoDVillacreses
Copy link

@DiegoDVillacreses DiegoDVillacreses commented Sep 25, 2019

I understand that this issue was closed based on:
install.packages("https://win-builder.r-project.org/1vUk5Gxm9QqM/rstan_2.19.1.zip", repos = NULL)
But that link isn't available anymore:
https://win-builder.r-project.org/1vUk5Gxm9QqM

@bgoodri
Copy link
Contributor

@bgoodri bgoodri commented Sep 25, 2019

@ngupta23
Copy link

@ngupta23 ngupta23 commented Sep 25, 2019

I am facing the same issue on Linux. Do you have a solution for Linux machines?

@bgoodri
Copy link
Contributor

@bgoodri bgoodri commented Sep 25, 2019

@DiegoDVillacreses
Copy link

@DiegoDVillacreses DiegoDVillacreses commented Sep 25, 2019

Thank you very much for your quick and accurate response @bgoodri . Actually my problem is when I try:
install.packages("OpenMx")
I get:

Installing package into ‘~/R/win-library/3.6’
(as ‘lib’ is unspecified)

  There is a binary version available but the source version is later:
       binary  source needs_compilation
OpenMx 2.13.2 2.14.11              TRUE

installing the source package ‘OpenMx’

trying URL 'https://cran.rstudio.com/src/contrib/OpenMx_2.14.11.tar.gz'
Content type 'application/x-gzip' length 3386317 bytes (3.2 MB)
downloaded 3.2 MB

* installing *source* package 'OpenMx' ...
** package 'OpenMx' successfully unpacked and MD5 sums checked
** using staged installation

   **********************************************
   WARNING: this package has a configure script
         It probably needs manual configuration
   **********************************************


** libs
Error in .shlib_internal(args) : 
  C++14 standard requested but CXX14 is not defined
* removing 'C:/Users/dvill/Documents/R/win-library/3.6/OpenMx'
Warning in install.packages :
  installation of package ‘OpenMx’ had non-zero exit status

Specifications:

  1. OS: Windows 10 home-64 bits-x64-based processor
  2. R: x86_64-w64-mingw32; arch x86_64; os mingw32; system x86_64, mingw32
  3. Rtools: 3.5.0.4

I am only having troubles with this package. For example, curl was installed using compilation perfectly.

I have tried everything and this issue is the best documented, it's possible to solve this here or should I open another issue?

Also, @ngupta23 I have in this same machine Ubuntu, and I have no problem whatsoever with any package there (I have the same packages in both OS). I believe you should:

sudo apt update
sudo apt upgrade

@bgoodri
Copy link
Contributor

@bgoodri bgoodri commented Sep 25, 2019

@thistleknot
Copy link

@thistleknot thistleknot commented Jan 9, 2021

`cat ~/.R/Makevars

CXX14 = g++
CXX14FLAGS = -O3
CXX14FLAGS += -fPIC
#LIBS += -lm
`

@NikKrieger
Copy link

@NikKrieger NikKrieger commented Aug 30, 2021

`cat ~/.R/Makevars

CXX14 = g++
CXX14FLAGS = -O3
CXX14FLAGS += -fPIC
#LIBS += -lm
`

This allowed me to install the xgboost R package

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests