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

fatal error: 'cmath' file not found #1333

Closed
sarahsamorodnitsky opened this issue Sep 25, 2024 · 26 comments
Closed

fatal error: 'cmath' file not found #1333

sarahsamorodnitsky opened this issue Sep 25, 2024 · 26 comments

Comments

@sarahsamorodnitsky
Copy link

Hi there,

I have a .cpp script I wrote several months ago that I haven't touched in a bit. I source that script using sourceCpp() in my R script. I am now getting the error: fatal error: 'cmath' file not found

I am working from a Mac with OS Sonoma 14.6.1. I just updated R to version 4.4.1 and redownloaded Rcpp and RcppArmadillo. I have copied below a minimally-reproducible example that I found through Hadley Wickham's book:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
NumericMatrix gibbs_cpp(int N, int thin) {
  NumericMatrix mat(N, 2);
  double x = 0, y = 0;

  for(int i = 0; i < N; i++) {
    for(int j = 0; j < thin; j++) {
      x = rgamma(1, 3, 1 / (y * y + 4))[0];
      y = rnorm(1, 1 / (x + 1), 1 / sqrt(2 * (x + 1)))[0];
    }
    mat(i, 0) = x;
    mat(i, 1) = y;
  }

  return(mat);
}

If I save the above code in a file called text.cpp and source it using sourceCpp(test.cpp) I get the following error:

> sourceCpp("test.cpp")
using C++ compiler: ‘Apple clang version 16.0.0 (clang-1600.0.26.3)’
using SDK: ‘MacOSX15.0.sdk’
clang++ -arch arm64 -std=gnu++17 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG   -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include" -I"/Users/ssamorod/Downloads" -I/opt/R/arm64/include    -fPIC  -falign-functions=64 -Wall -g -O2   -c test.cpp -o test.o
In file included from test.cpp:1:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/Rcpp.h:27:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/RcppCommon.h:30:
In file included from /Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/Rcpp/r/headers.h:66:
/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/library/Rcpp/include/Rcpp/platform/compiler.h:100:10: fatal error: 'cmath' file not found
  100 | #include <cmath>
      |          ^~~~~~~
1 error generated.
make: *** [test.o] Error 1
Error in sourceCpp("test.cpp") : 
  Error 1 occurred building shared library.

I wasn't able to find this specific error looking through previous Github issues and the listserv, but from similar ones I gathered it has something to do with my local machine, maybe with Xcode? I do not feel qualified to diagnose this myself since I'm very new to using Rcpp and C++. Hoping to get a bit of help or insight. Thank you in advance.

Sarah

@eddelbuettel
Copy link
Member

eddelbuettel commented Sep 25, 2024

This is almost surely at your end, and we have for example this issue pinned for RcppArmadillo (where a Fortran compiler is needed too). You may also try asking on r-sig-mac for macOS specific questions.

Our package is in good standing at CRAN and that includes tests on different macOS hardware choices and R versions.

The Rcpp FAQ also has an entry. I am a little removed from this as I only use Linux these days, but as you alluded to, Xcode is a requirement. @coatless has provided nice resources such as the one linked above for all these years.

@eddelbuettel
Copy link
Member

Also see https://mac.r-project.org/ and its

Tools

In order to compile R and R packages you will need Xcode Developer Tools and a Fortran compiler. For details and download, please read the Tools page. The R 4.3.0 and higer binaries are built using Xcode 14.3 and universal GNU Fortran 12.2 from here.

@coatless
Copy link
Contributor

@sarahsamorodnitsky try updating xcode cli.

A screenshot showing the Settings panel on macOS and the update to Xcode CLI option.

@eddelbuettel
Copy link
Member

Good call. Maybe the Sonomoa upgrade requires that as follow-up.

@sarahsamorodnitsky
Copy link
Author

Thank you both for the prompt responses.

I don't see a software update for Xcode available. @coatless, I've taken a look at some of your resources (this and this). macrtools::is_xcode_cli_installed() and macrtools::is_gfortran_installed() both returned TRUE. macrtools::is_xcode_app_installed() returned FALSE, not sure if that is important. Still getting the same error. When I ran gcc --version I got th efollowing output:

Apple clang version 16.0.0 (clang-1600.0.26.3)
Target: arm64-apple-darwin23.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Not sure if that is relevant or helpful. Do you have any other suggestions? I really appreciate the help.

@coatless
Copy link
Contributor

coatless commented Sep 25, 2024

Okidokies, so Xcode should be installed.

Is there anything in your ~/.Renviron or ~/.Rprofile?

Open terminal and type:

cat ~/.Renviron 

cat ~/.Rprofile

Check if there is any clang modifications present.

@sarahsamorodnitsky
Copy link
Author

sarahsamorodnitsky commented Sep 25, 2024

No files were found when I ran the above code:

(base) ssamorod ~ % cat ~/.Renviron 
cat: /Users/ssamorod/.Renviron: No such file or directory
(base) ssamorod ~ % cat ~/.Rprofile
cat: /Users/ssamorod/.Rprofile: No such file or directory

Is that a problem? Somewhere along the way of Googling around for solutions I think I might have removed these files.

@coatless
Copy link
Contributor

Whoops, I needed one more:

cat ~/.R/Makevars

^ What's in that file?

@sarahsamorodnitsky
Copy link
Author

Also could not find that file:

(base) ssamorod ~ % cat ~/.R/Makevars
cat: /Users/ssamorod/.R/Makevars: No such file or directory

@coatless
Copy link
Contributor

coatless commented Sep 25, 2024

Mmm, that's interesting. Are you running anything to switch between R versions? Do you have conda on the path? This looks like there is a mixup between the internal mac headers and what R thinks is present.

(I need to duck out at this point. I'll be back later tonight.)

@sarahsamorodnitsky
Copy link
Author

I am not running anything to switch between R versions, other than redownloading some packages.

Here is what I get when I check the PATH:

(base) ssamorod ~ % echo $PATH
/opt/anaconda3/bin:/opt/anaconda3/condabin:/Library/Frameworks/Python.framework/Versions/3.12/bin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Applications/quarto/bin

It does look like anaconda3 is present. (Thanks again BTW for helping me figure this out)

@eddelbuettel
Copy link
Member

Ah..Most of us have better luck outside of Conda, and mixing can be tricky. If you are knowledgeable around Conda it can surely work .... but naively mixing may be a best idea.

Rcpp essentially "just" needs R to know about and find its compiler. The simplest litmus test is still

Rcpp::evalCpp("2 + 2")

@sarahsamorodnitsky
Copy link
Author

Ah, okay, I get the same error when I run Rcpp::evalCpp("2 + 2"). Should I remove conda from the path? How do I ensure R can find the Rcpp compiler?

@eddelbuettel
Copy link
Member

I would try that -- see if just R + Xcode will work,

@sarahsamorodnitsky
Copy link
Author

So I've tried deleting anaconda and have confirmed it is no longer in the path:

/Library/Frameworks/Python.framework/Versions/3.12/bin:/Library/Frameworks/Python.framework/Versions/3.11/bin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/opt/X11/bin:/Applications/quarto/bin

Still running into the same error. Tried restarting the computer and redownloading Rcpp.

@kevinushey
Copy link
Contributor

kevinushey commented Sep 26, 2024

I recall @jeroen bumped into something very similar that was resolved by reinstalling CLT:

The installer for the Command Line Tools for Xcode 16 does not delete a directory that was installed by some older versions of the CLTs, which unfortunately causes clang++ to look in the wrong place for C++ headers. The majority of ports that contain C++ code will fail to build as a result.

The fix is to delete the stray directory if it exists: sudo rm -rf /Library/Developer/CommandLineTools/usr/include/c++

For reference: https://trac.macports.org/wiki/SequoiaProblems#CompilingCfails

If that doesn't help, can you share the output of:

xcrun --show-sdk-path
xcode-select -p

It could be worth trying to reinstall command line tools, e.g.

sudo rm -rf /Library/Developer/CommandLineTools
sudo xcode-select --install

You can also trace the include paths used by clang by adding the -H flag; one way to do this is:

dir.create("~/.R", showWarnings = FALSE)
cat("CPPFLAGS += -H", file = "~/.R/Makevars", sep = "\n", append = TRUE)

Then, try executing Rcpp::sourceCpp("<file>", verbose = TRUE, rebuild = TRUE). You should get a mountain of output like:

clang++ -arch arm64 -std=gnu++17 -I"/Library/Frameworks/R.framework/Versions/4.4-arm64/Resources/include" -DNDEBUG   -I"/Users/kevin/Library/R/arm64/4.4/library/Rcpp/include" -I"/Users/kevin/scratch" -I/opt/R/arm64/include -DSTRICT_R_HEADERS=1 -H -H    -fPIC  -fpermissive  -c Untitled.cpp -o Untitled.o
. /Users/kevin/Library/R/arm64/4.4/library/Rcpp/include/Rcpp.h
.. /Users/kevin/Library/R/arm64/4.4/library/Rcpp/include/RcppCommon.h
... /Users/kevin/Library/R/arm64/4.4/library/Rcpp/include/Rcpp/r/headers.h
.... /Users/kevin/Library/R/arm64/4.4/library/Rcpp/include/Rcpp/platform/compiler.h
..... /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/v1/cmath
< ... >

Note that cmath is found in this directory for my case.

@eddelbuettel
Copy link
Member

@sarahsamorodnitsky Any luck?

@sarahsamorodnitsky
Copy link
Author

@kevinushey Your first suggestion seems to have solved the problem! Thank you all so much for your help!!

@eddelbuettel
Copy link
Member

@sarahsamorodnitsky Sounds like we can close this then as the reinstallation provided you with a working setup?

@sjmgarnier
Copy link

I encountered a similar issue with RcppEigen (fatal error: 'iterator' file not found) which was resolved by reinstalling the command line tools as indicated in #1333 (comment) above.

@eddelbuettel
Copy link
Member

eddelbuettel commented Sep 28, 2024

I don't actually think these are issues with either Rcpp or RcppEigen as all these packages "merely" rely on R being able to call a compiler reliably.

Which in the year 2024 appears to still be a challenge on some OSs, alas. I wish we had means to make this more reliable on those OSs, I am not aware that we do.

@sjmgarnier
Copy link

@eddelbuettel They aren't Rcpp issues but I imagine that people will land here each time they have an issue installing/using Rcpp packages. I think the best you can do is document the problem and the solution/workaround. No one can change Apple's mind :-)

@eddelbuettel
Copy link
Member

eddelbuettel commented Sep 28, 2024

@sjmgarnier That is what I tried to say a minute ago:

I wish we had means to make this more reliable on those OSs, I am not aware that we do.

If you have a PR to test things, please do not hold back and help us make Rcpp (and friends) better! Where I live, things like Rcpp::evalCpp("2 + 2") work. Where you are, they may not on a naive setup. But how do we reliably get from naive to working?

@sjmgarnier
Copy link

@eddelbuettel If I can think of something, I will certainly let you know. Right now, I just manually test my packages every time I update the command line tools on my Mac because I know someone at some point will report problems on my repos.

@sjmgarnier
Copy link

@eddelbuettel It looks like Apple has fixed the issue somehow. The latest update of the CLT for Sequoia doesn't have the problem anymore.

@eddelbuettel
Copy link
Member

@sjmgarnier That is good to know so thank you for reporting back here!

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

5 participants