'cmath' file not found running brms models on Windows

Short summary of the problem

When I run this code:

banova <-brm(differenceinfavorabilityscore ~ group, anova2, prior=c(set_prior("normal(0,2.06)", class= "b")), cores= 4)

I get this error message:

## WARNING: Rtools is required to build R packages, but is not currently installed.
## 
## Please download and install the appropriate version of Rtools for 4.5.1 from
## https://cran.r-project.org/bin/windows/Rtools/.
## Trying to compile a simple C file
## Running "C:/PROGRA~1/R/R-45~1.1/bin/x64/Rcmd.exe" SHLIB foo.c
## using C compiler: 'gcc.exe (GCC) 14.2.0'
## gcc  -I"C:/PROGRA~1/R/R-45~1.1/include" -DNDEBUG   -I"C:/Users/Julie/AppData/Local/R/win-library/4.5/Rcpp/include/"  -I"C:/Users/Julie/AppData/Local/R/win-library/4.5/RcppEigen/include/"  -I"C:/Users/Julie/AppData/Local/R/win-library/4.5/RcppEigen/include/unsupported"  -I"C:/Users/Julie/AppData/Local/R/win-library/4.5/BH/include" -I"C:/Users/Julie/AppData/Local/R/win-library/4.5/StanHeaders/include/src/"  -I"C:/Users/Julie/AppData/Local/R/win-library/4.5/StanHeaders/include/"  -I"C:/Users/Julie/AppData/Local/R/win-library/4.5/RcppParallel/include/" -DRCPP_PARALLEL_USE_TBB=1 -I"C:/Users/Julie/AppData/Local/R/win-library/4.5/rstan/include" -DEIGEN_NO_DEBUG  -DBOOST_DISABLE_ASSERTS  -DBOOST_PENDING_INTEGER_LOG2_HPP  -DSTAN_THREADS  -DUSE_STANC3 -DSTRICT_R_HEADERS  -DBOOST_PHOENIX_NO_VARIADIC_EXPRESSION  -D_HAS_AUTO_PTR_ETC=0  -include "C:/Users/Julie/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp"  -std=c++1y    -I"C:/rtools45/x86_64-w64-mingw32.static.posix/include"      -O2 -Wall -std=gnu2x  -mfpmath=sse -msse2 -mstackrealign   -c foo.c -o foo.o
## cc1.exe: warning: command-line option '-std=c++14' is valid for C++/ObjC++ but not for C
## In file included from C:/Users/Julie/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Core:19,
##                  from C:/Users/Julie/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/Dense:1,
##                  from C:/Users/Julie/AppData/Local/R/win-library/4.5/StanHeaders/include/stan/math/prim/fun/Eigen.hpp:22,
##                  from <command-line>:
## C:/Users/Julie/AppData/Local/R/win-library/4.5/RcppEigen/include/Eigen/src/Core/util/Macros.h:679:10: fatal error: cmath: No such file or directory
##   679 | #include <cmath>
##       |          ^~~~~~~
## compilation terminated.
## make: *** [C:/PROGRA~1/R/R-45~1.1/etc/x64/Makeconf:289: foo.o] Error 1
## WARNING: Rtools is required to build R packages, but is not currently installed.
## 
## Please download and install the appropriate version of Rtools for 4.5.1 from
## https://cran.r-project.org/bin/windows/Rtools/.

I do have RTools installed, though - the latest version, installed to the default directory C:\rtools45.

After this error, the code keeps running anyway. I’m not quite getting the results I expected, but I think that’s an entirely different issue. I’d still like to get rid of this error.

I searched the forums and found identical threads from many Mac users, but no Windows users. (There’s one similar but not identical Windows problem 5 years ago…) The solutions in those threads were all Mac-specific.

Operating System: Windows 11 Pro, Version 24H2, Build 26100.4652
R version 4.5.1
RStudio version 2025.05.1 Build 513
RTools version 4.5
brms version 2.22.0

This is also my personal computer at home - no admin rights issues, network drives, permissions, etc.

When I run

pkgbuild::check_build_tools(debug = TRUE)

it passes the check, and says “Your system is ready to build packages!”

Any ideas? Thank you so much!!

I too have had this issue (i.e., brms works in console etc. but gives cmath error when knitting Rmarkdown), unresolved for months after many attempts to fix. I’ve repeatedly asked AI over the months and it has given many different solutions that don’t work. Finally today it offered this, which seems to do the trick!

The text below is copied from Gemini output, so there might be better ways…


Switching to the cmdstanr package is the recommended way to bypass the “missing cmath” and other toolchain errors often found in rstan. Because cmdstanr compiles models into standalone executables outside of the R session, it is much less prone to header conflicts.

1. Install the cmdstanr R Package

Since cmdstanr is not yet on CRAN, install it from the Stan R-universe:

install.packages("cmdstanr", repos = c('https://stan-dev.r-universe.dev', getOption("repos")))

2. Install the CmdStan Toolchain

Once the R package is installed, you must install the actual Stan C++ binaries.

  1. Check Toolchain: Verify your Rtools configuration is correct.

    cmdstanr::check_cmdstan_toolchain(fix = TRUE)
    
    

    Install Binaries: Run the installation function. This will download and build the latest version of CmdStan in your user folder.

    cmdstanr::install_cmdstan()
    
    

    (John interjects: When I did this, it spewed hundreds of warnings that scrolled by, but the result seems to work anyway… Very mysterious.)

3. Configure brms to use cmdstanr

You can switch the backend for a single model or for your entire session.

  • For a Single Model: Add the backend argument to your brm() call.

    r

    fit <- brm(..., backend = "cmdstanr")
    
    

    Use code with caution.

  • Global Setting (Recommended): Set this at the top of your script or in your Rmarkdown setup chunk to apply it to all models automatically.

    r

    options(brms.backend = "cmdstanr")