6

I've developing R packages that contain C++ code on Windows 10.

I wish to check for memory allocation errors. It sounds like the simplest path is to use clang++ with the -fsanitize=address option.

How do I configure R such that clang is the default compiler?

What I've done so far:

  • Install LLVM, adding binaries to the path

  • Set the following options in ~/.R/makevars:

CC = "path/to/llvm/bin/clang"
CXX = "path/to/llvm/bin/clang++"
CXX11 = "path/to/llvm/bin/clang++"
CXX14 = "path/to/llvm/bin/clang++"
CXXFLAGS = -O1 -g -fsanitize=address -fno-omit-frame-pointer
PKG_CXXFLAGS = -Og -fsanitize=address
LDFLAGS = -fsanitize=address
  • Within RStudio, run devtools::install(). I see:
-  installing *source* package 'TreeTools' ...
   ** using staged installation
   ** libs
   "path/to/llvm/bin/clang++" -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.0/include" -DNDEBUG  -I'C:/Users/ms609/Documents/R/win-library/4.0/Rcpp/include'     -Og -Wall -pedantic -mtune=generic -fsanitize=address -Wno-deprecated-declarations   -O2 -Wall -gdwarf-2 -mfpmath=sse -msse2 -mstackrealign -c as_newick.cpp -o as_newick.o
   "path/to/llvm/bin/clang++" -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.0/include" -DNDEBUG  -I'C:/Users/ms609/Documents/R/win-library/4.0/Rcpp/include'     -Og -Wall -pedantic -mtune=generic -fsanitize=address -Wno-deprecated-declarations   -O2 -Wall -gdwarf-2 -mfpmath=sse -msse2 -mstackrealign -c int_to_tree.cpp -o int_to_tree.o
   "path/to/llvm/bin/clang++" -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.0/include" -DNDEBUG  -I'C:/Users/ms609/Documents/R/win-library/4.0/Rcpp/include'     -Og -Wall -pedantic -mtune=generic -fsanitize=address -Wno-deprecated-declarations   -O2 -Wall -gdwarf-2 -mfpmath=sse -msse2 -mstackrealign -c phangorn_bip.cpp -o phangorn_bip.o
   "path/to/llvm/bin/clang++" -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.0/include" -DNDEBUG  -I'C:/Users/ms609/Documents/R/win-library/4.0/Rcpp/include'     -Og -Wall -pedantic -mtune=generic -fsanitize=address -Wno-deprecated-declarations   -O2 -Wall -gdwarf-2 -mfpmath=sse -msse2 -mstackrealign -c renumber_tree.cpp -o renumber_tree.o
   "path/to/llvm/bin/clang++" -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.0/include" -DNDEBUG  -I'C:/Users/ms609/Documents/R/win-library/4.0/Rcpp/include'     -Og -Wall -pedantic -mtune=generic -fsanitize=address -Wno-deprecated-declarations   -O2 -Wall -gdwarf-2 -mfpmath=sse -msse2 -mstackrealign -c splits.cpp -o splits.o
   "path/to/llvm/bin/clang++" -std=gnu++14 -I"C:/PROGRA~1/R/R-40~1.0/include" -DNDEBUG  -I'C:/Users/ms609/Documents/R/win-library/4.0/Rcpp/include'     -Og -Wall -pedantic -mtune=generic -fsanitize=address -Wno-deprecated-declarations   -O2 -Wall -gdwarf-2 -mfpmath=sse -msse2 -mstackrealign -c tree_shape.cpp -o tree_shape.o
   path/to/llvm/bin/clang++ -shared -o TreeTools.dll tmp.def RcppExports.o TreeTools-init.o as_newick.o int_to_tree.o phangorn_bip.o renumber_tree.o splits.o tree_shape.o -LC:/PROGRA~1/R/R-40~1.0/bin/i386 -lR
   tmp.def : fatal error LNK1107: invalid or corrupt file: cannot read at 0x1E50
   clang++: error: linker command failed with exit code 1107 (use -v to see invocation)
   no DLL was created
   ERROR: compilation failed for package 'TreeTools'

R seems to have successfully compiled individual source files successfully using clang, but is failing to create a .dll.

What have I missed: do I need to do anything to configure Rcmd or Rtools to run with clang rather than the default gcc?

Martin Smith
  • 3,687
  • 1
  • 24
  • 51

0 Answers0