Issue compiling Rcpp programs with user-defined header files on Windows - r

1. Issue
I was trying to compile an Rcpp program with a user-defined .hpp header file following an example in http://dirk.eddelbuettel.com/code/rcpp/Rcpp-attributes.pdf, p9, section 2.10.2. It compiles nicely in RStudio on my Mac and works well.
However, when I run exactly the same program on my Windows 64-bits laptop (Win 7), I get the following error message:
g++ -m64 -I"G:/R-32~1.3/include" -DNDEBUG -I"G:/R-3.2.3/library/Rcpp/include" -I"F:/_tst/rcpp" -I"d:/RCompile/r-compiling/local/local323/include" -O2 -Wall -mtune=core2 -c tst4.cpp -o tst4.o
G:\Rtools\gcc-4.6.3\bin\nm.exe: 'F:_tstrcpputilities.o': No such file
g++ -m64 -shared -s -static-libgcc -o sourceCpp_1.dll tmp.def F:_tstrcpputilities.o tst4.o -Ld:/RCompile/r-compiling/local/local323/lib/x64 -Ld:/RCompile/r-compiling/local/local323/lib -LG:/R-32~1.3/bin/x64 -lR
g++.exe: error: F:_tstrcpputilities.o: No such file or directory
Error in sourceCpp("tst4.cpp") : Error occurred building shared library.
It seems like the slashes in the file paths are lost (I assume instead of 'F:_tstrcpputilities.o', it should be 'F:\_tst\rcpp\utilities.o'?), but I have no idea how to fix this...
2. Tentative solution but unsuccessful
I tried adding my Rtools and Rtools/gcc-x.x.x/bin directory to PATH by using devtool as follows:
add_path("G:\\Rtools");
add_path("G:\\Rtools\\bin");
add_path("G:\\Rtools\\gcc-4.6.3\\bin");
Unfortunately it did not help. The same error remains.
So...
Well, I'm stuck here... Any help would be appreciated.
=========================================================================
S1. Supplementary info
Some information about my laptop/R/Rtools:
OS: Win 7 64-bits
R version: 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree"
Rtools version: Rtools3.3
The test cpp code (filename: tst4.cpp) I used was:
#include <Rcpp.h>
#include "utilities.hpp"
using namespace Rcpp;
// [[Rcpp::export]]
double timesTwenty(double x) {
return timesTwo(x) * 10;
}
All .cpp and .hpp files are in the same folder.

Related

'RcppArmadillo.h' file not found when installing RcppArmadillo from source

RcppArmadillo 0.8.400.0.0 works just fine on my computer, both from sourceCpp and cxxfunction. Now I want to upgrade to 0.8.500.0, which requires compilation from source. Then I got the error
ccache /usr/local/clang6/bin/clang++ -std=gnu++11 -I"/Library/Frameworks/R.framework/Resources/include" -DNDEBUG -I"/Library/Frameworks/R.framework/Versions/3.5/Resources/library/Rcpp/include" -I/usr/local/include -I/Applications/CPLEX_Studio128/cplex/include -I/Applications/CPLEX_Studio128/concert/include -DIL_STD -fPIC -Wall -g -O2 -c RcppArmadillo.cpp -o RcppArmadillo.o
RcppArmadillo.cpp:22:10: fatal error: 'RcppArmadillo.h' file not found
#include <RcppArmadillo.h>
^~~~~~~~~~~~~~~~~
1 error generated.
I got this error whether I ran install.packages("RcppArmadillo") in RStudio or in R console outside RStudio. My compiler is clang4, and I have no problem installing RcppParallel or compiling my own Rcpp code that uses RcppArmadillo and RcppParallel. R version: 3.5.0, MacOS High Sierra.
Edit: I looked at https://github.com/RcppCore/RcppArmadillo/issues/143, which is very relevant. I did install clang6 and gfortran 6.1, both downloaded from the CRAN website. I also changed the .R/Makevars file to reflect the changes to clang6. I have also installed ccache as suggested, to make compilation faster. Here's my Makevars:
F77 = /usr/local/gfortran/bin/gfortran
FC = $F77
FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin16/6.3.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm
CC=ccache /usr/local/clang6/bin/clang
SHLIB_CXXLD=ccache /usr/local/clang6/bin/clang++
CXX= ccache /usr/local/clang6/bin/clang++ -Wall
CXX1X= ccache /usr/local/clang6/bin/clang++
CXX98= ccache /usr/local/clang6/bin/clang++
CXX11= ccache /usr/local/clang6/bin/clang++
CXX14= ccache /usr/local/clang6/bin/clang++
CXX17= ccache /usr/local/clang6/bin/clang++
LDFLAGS=-L/usr/local/clang6/lib
From the outputs in the R console when I was installing the package (added to the error message above), I can tell that clang6 was used for compilation. I also found the downloaded RcppArmadillo tarball, unpacked it, and ran ./configure from the command line. Nothing seemed wrong there; no config.log file was generated. This doesn't seem to be a configuration problem. Then I still got the exact same error when trying to install the package in R, after configuration, in libs. I also didn't get any message about -fopenmp. It seems that my problem is a bit different from that in the GitHub ticket.
R 3.5.x requires clang6 and adding clang6 to your system PATH variable.
Steps:
x Download and install the clang-6.0.0.pkg official package from CRAN.
x Delete your ~/.R/Makevars as it is likely set to use clang4. To do this, open Terminal and type:
sudo rm ~/.R/Makevars
x Then add to your ~/.bash_profile the following:
if [ -d "/usr/local/clang6/bin" ] ; then
export PATH="/usr/local/clang6/bin:$PATH"
fi
Note: If you are using a shell other than bash you will need to add the above into the appropriate file, e.g. for zsh add to ~/.zshrc, as this was not implemented in the official installer.
I should have a few cycles to finish writing a new unofficial Rtools installer a bit later. See: https://github.com/coatless/r-macos-rtools/issues/7
Note: Alternatively, you can remove the paths individual with
sudo vi ~/.R/Makevars
Pressing I to enter insert mode, deleting lines with clang4 in the path, and then pressing ESC + :wq to write changes to file.
Never mind, this problem went away with version 0.8.600. Later, this problem appeared again when I upgraded to RcppArmadillo 0.9.100.5.0. Somehow I only got this error when building from source; I downloaded the binary manually from the CRAN website, and used R CMD INSTALL to install the prebuilt version, and it worked; somehow R doesn't seem to know that there is a binary available for the newest version.
I did install clang6 and modified .R/Makevars to use clang6, but the same problem persisted; probably something else caused the problem.

understanding RCpp compile

I was looking at the verbose=TRUE when I tried to sourceCpp a Rcpp file. The last output is:
DIR: C:/Users/xyz/AppData/Local/Temp/RtmpmielLn/sourcecpp_226416891d0e
C:/PROGRA~1/R/R-31~1.0/bin/x64/R CMD SHLIB -o "sourceCpp_22129.dll" --preclean "myfile.cpp"
g++ -m64 -I"C:/PROGRA~1/R/R-31~1.0/include" -DNDEBUG -I"C:/Users/xyz/Documents/R/win-library/3.1/Rcpp/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c myfile.cpp -o myfile.o
g++ -m64 -shared -s -static-libgcc -o sourceCpp_22129.dll tmp.def myfile.o -Ld:/RCompile/CRANpkg/extralibs64/local/lib/x64 -Ld:/RCompile/CRANpkg/extralibs64/local/lib -LC:/PROGRA~1/R/R-31~1.0/bin/x64 -lR
I have a few questions regarding this:
the 1nd g++ command refers to -I"d:/RCompile/CRANpkg/extralibs64/local/include" and the 2nd command refers to -Ld:/RCompile/CRANpkg/extralibs64/local/lib/x64 and -Ld:/RCompile/CRANpkg/extralibs64/local/lib . But I don't have a D: drive, or a RCompile folder anywhere. What do these things refer to?
I tried to manually run the 1st g++ which ran file and created myfun.o file, but when I tried to manually run the 2nd g++ it gave me an error saying that it couldn't find the tmp.def file. I couldn't find the tmp.def file anywhere on my drives. Where would this tmp.def file located?
I looked under the hood of sourceCpp function. if I directly run the definition of cmd in the sourceCpp function: C:/PROGRA~1/R/R-31~1.0/bin/x64/R CMD SHLIB -o "sourceCpp_22129.dll" --preclean "myfile.cpp" on Windows' command window , I noticed that it does not include -I"C:/Users/xyz/Documents/R/win-library/3.1/Rcpp/include" and the R CMD SHLIB gives me an error.
How does the system(cmd, ..) within the sourceCpp function include this? The value of the cmd variable in the sourceCpp didn't include -I"C:/Users/xyz/Documents/R/win-library/3.1/Rcpp/include"
the 1nd g++ command refers to -I"d:/RCompile/CRANpkg/extralibs64/local/include" and the 2nd command refers to -Ld:/RCompile/CRANpkg/extralibs64/local/lib/x64 and -Ld:/RCompile/CRANpkg/extralibs64/local/lib . But I don't have a D: drive, or a RCompile folder anywhere. What do these things refer to?
AFAIK these are left in as part of the CRAN R Windows distribution; when R binaries are built on Windows they use something in these library paths on the build servers (but stay baked into R anyhow). You can safely ignore it, but it is a bit odd. Unused / non-existent directories passed through gcc / g++ are just ignored.
I tried to manually run the 1st g++ which ran file and created myfun.o file, but when I tried to manually run the 2nd g++ it gave me an error saying that it couldn't find the tmp.def file. I couldn't find the tmp.def file anywhere on my drives. Where would this tmp.def file located?
tmp.def, as it sounds, is a temporary definition file created by R CMD SHLIB on Windows. If you just re-run what you see it does not get generated, so I suppose R does something behind the curtains to generate it. If you are curious about where it's generated, see share/make/winshlib.mk in the R sources.
I looked under the hood of sourceCpp function. if I directly run the definition of cmd in the sourceCpp function: C:/PROGRA~1/R/R-31~1.0/bin/x64/R CMD SHLIB -o "sourceCpp_22129.dll" --preclean "myfile.cpp" on Windows' command window , I noticed that it does not include -I"C:/Users/xyz/Documents/R/win-library/3.1/Rcpp/include" and the R CMD SHLIB gives me an error.
This is because sourceCpp is setting the appropriate environment flags behind the scenes for you as well -- in this case, the CXXFLAGS environment variable. This gets automatically done on package installs as well when the LinkingTo: entry is specified in the DESCRIPTION file.
I had a very similar g++ compilation command as posted in the question, and the compilation of a very simple C function didn't work for me.
The reason it didn't work is the following option shown in the g++ command:
-I"d:/RCompile/CRANpkg/extralibs64/local/include"
which adds an include directory on a drive that does not exist on my computer. Apparently non-existent directories listed in the -I option are ignored by g++ (as stated by Kevin Ushey) but this seems not to be the case for non-existent drives.
The error message I received was that the sdtlib.h header file was not found:
C:/PROGRA~1/R/R-32~1.0/include/R.h:28:20: fatal error:
d:/RCompile/r-compiling/local/local320/include/stdlib.h: Input/output
error #include
In order to remove the offending -I option from the g++ command I had to edit the Makeconf file located in $(R_HOME}/etc$(R_ARCH) (in my case C:\Program Files\R\R-3.2.0\etc\x64) and comment out the line:
LOCAL_SOFT = d:/RCompile/r-compiling/local/local320

Rcpp compilation on windows fails (64bit)

I am trying to set up Rcpp on my Windows machine (have not had issues on my unix box).
I have installed R in C:/opt/R/current (a Google search suggested that spaces in installation paths could give trouble), and I have installed Rtools.
I have the simple cpp file:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
int timesTwo(int x) {
return x * 2;
}
and an R file:
library(Rcpp)
sourceCpp("cppfile.cpp")
I get the following error:
C:/opt/R/current/etc/x64/Makeconf:196: warning: overriding recipe for target `.m.o' C:/opt/R/current/etc/x64/Makeconf:189: warning: ignoring old recipe for target `.m.o' C:\MINGW3~1\bin\make.exe: Interrupt/Exception caught (code = 0xc0000005, addr = 0x0x750343f9) Warning message: running command 'make -f "C:/opt/R/current/etc/x64/Makeconf" -f "C:/opt/R/current/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="sourceCpp_67515.dll" WIN=64 TCLBIN=64 OBJECTS="cppfile.o"' had status 255
Error in sourceCpp("cppfile.cpp") :
Error 1 occurred building shared library.
Any help would be appreciated.
PS: Install e.g. dplyr from github via devtools works fine (and does quite a lot of compiling Rcpp stuff).
UPDATE: I added the Rtools bin dir to PATH, and now I get another error which seems to relate to 32/64bit:
g++ -m64 -I"C:/opt/R/current/include" -DNDEBUG -I"C:/opt/R/current/library/Rcpp/include" -I"d:/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c cppfile.cpp -o cppfile.o cppfile.cpp:1:0: sorry, unimplemented: 64-bit mode not compiled in make: *** [cppfile.o] Error 1 Warning message: running command 'make -f "C:/opt/R/current/etc/x64/Makeconf" -f "C:/opt/R/current/share/make/winshlib.mk" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="sourceCpp_41862.dll" WIN=64 TCLBIN=64 OBJECTS="cppfile.o"' had status 2
Error in sourceCpp("cppfile.cpp") :
Error 1 occurred building shared library.
Turned out that I needed to add both the RTools/bin and the Rtools/gcc-x.y.z/bin to PATH. Now things work smoothly.

compiling package with Rcpp outside of R-Studio in Windows

I've successfully build my own package with Rcpp in R-Studio. However, when building the package in the Windows console, there are some error messages complaining file missing of R.h.
I set the path and R_Home environments with
SET PATH=D:\RTools\gcc-4.6.3\bin;D:\R3\bin;D:\RTools\bin;
SET R_HOME=D:\R3\
And the build command is
R CMD INSTALL --byte-compile --build mypkg
Below are the compiling error messages:
g++ -m32 -I"/include" -DNDEBUG -I"D:/R3/library/Rcpp/include" -I"d:
/RCompile/CRANpkg/extralibs64/local/include" -O2 -Wall -mtune=core2 -c Rcpp
Exports.cpp -o RcppExports.o
In file included from D:/R3/library/Rcpp/include/Rcpp.h:27:0,
from RcppExports.cpp:4:
D:/R3/library/Rcpp/include/RcppCommon.h:35:15: fatal error: R.h: No su
ch file or directory
compilation terminated.
You are missing an include for R.h, as the error says.
And if you look at your compile line, the statement
-I"/include"
is wrong relative to your stated R_HOME in D:/R3
You need to check your setup, somehow you confused R from using the correct include directory.
Rcpp builds just fine on Windows, you can even check by submitting your package to the win-builder service.

Rcpp install fails on Mac 10.8

Rcpp is failing to install for me. When I run the following within the R console (R 2.15.1 on Mac 10.8):
install.packages("Rcpp")
I get the following error:
/usr/bin/clang++ -I/usr/local/Cellar/r/2.15.1/R.framework/Resources/include \
-DNDEBUG -I../inst/include/ -I/usr/local/Cellar/readline/6.2.4/include \
-isystem /usr/local/include -I/opt/X11/include -fPIC -Os -w -pipe - \
march=native -Qunused-arguments -mmacosx-version-min=10.8 \
-c exceptions.cpp -o exceptions.o
exceptions.cpp:82:14: fatal error: 'bits/exception_defines.h' file not found
#include <bits/exception_defines.h>
^
1 error generated.
make: *** [exceptions.o] Error 1
ERROR: compilation failed for package ‘Rcpp’
What am I doing wrong?
Which version of Rcpp? This is fixed in SVN:
2012-07-06 Dirk Eddelbuettel <edd#debian.org>
* inst/include/Rcpp/config.h: In order to not attempt to include
exception_defines.h if on OS X (as the clang runtime may not have
predictable access to g+++ headers providing these), do not define
RCPP_HAS_DEMANGLING which is used in src/exceptions.cpp
and was discussed on the rcpp-devel list.
I tried to accomodate OS X and clang >= 3.0, but the interaction of clang and g++ is a little tricky. Try to fetch the file from SVN or just edit the section in it to yield
#ifdef __GNUC__
// from http://sourceforge.net/apps/mediawiki/predef/index.php?\
// title=Operating_Systems#MacOS
#ifndef __APPLE__
#ifndef __MACH__
#define RCPP_HAS_DEMANGLING
#endif
#endif
#endif
which turns this off for all OS X instances.

Resources