Skip to content

Instantly share code, notes, and snippets.

@ck37
Last active August 31, 2025 19:10
Show Gist options
  • Save ck37/e464712aeef1bf00f4285126024f36e1 to your computer and use it in GitHub Desktop.
Save ck37/e464712aeef1bf00f4285126024f36e1 to your computer and use it in GitHub Desktop.
Fix R package compilation issues on Apple Silicon Macs (M1/M2/M3) - Solves gnu23 C standard and C++ linking errors

Fix R Package Compilation Issues on Apple Silicon Macs (M1/M2/M3)

Problem

When installing R packages like xgboost, data.table, farver, or other packages that compile from source on Apple Silicon Macs, you may encounter errors like:

error: invalid value 'gnu23' in '-std=gnu23'

Or C++ symbol linking errors like:

symbol not found in flat namespace '__ZNSt3__113__hash_memoryEPKvm'

This happens because R 4.5.0+ uses the gnu23 C standard that Apple's default clang compiler doesn't support, and C++ packages may have standard library linking issues.

Quick Fix

1. Install Required Software

# Install Homebrew if you don't have it
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install LLVM, gettext, and libomp
brew install llvm gettext libomp

2. Create R Configuration

# Create .R directory if it doesn't exist
mkdir -p ~/.R

# Create or edit the Makevars file
nano ~/.R/Makevars

3. Add This Configuration to ~/.R/Makevars

# Configuration for R 4.5.0+ with LLVM clang support for gnu23
# Updated 2025-08-31

# Use LLVM clang which supports gnu23 C standard
LLVM_LOC = /opt/homebrew/opt/llvm
CC=$(LLVM_LOC)/bin/clang
CXX=$(LLVM_LOC)/bin/clang++

# Include paths for gettext (libintl.h) and other dependencies
CPPFLAGS=-I/opt/homebrew/include -I/opt/homebrew/opt/gettext/include -I$(LLVM_LOC)/include -I/opt/homebrew/opt/libomp/include

# C++ standard library configuration for LLVM
CXXFLAGS=-stdlib=libc++
LDFLAGS=-L/opt/homebrew/lib -L/opt/homebrew/opt/gettext/lib -L$(LLVM_LOC)/lib -L/opt/homebrew/opt/libomp/lib -stdlib=libc++ -Wl,-rpath,$(LLVM_LOC)/lib -L$(LLVM_LOC)/lib/c++ -lc++

# OpenMP support
SHLIB_OPENMP_CFLAGS=-Xclang -fopenmp
SHLIB_OPENMP_CXXFLAGS=-Xclang -fopenmp

# PKG_CONFIG path
PKG_CONFIG_PATH=/opt/homebrew/lib/pkgconfig:/opt/homebrew/opt/libomp/lib/pkgconfig

Save with Ctrl+O, Enter, then Ctrl+X.

4. Test Installation

install.packages('xgboost', repos='https://cran.r-project.org')
library(xgboost)

What This Fixes

  • Compilation errors with packages that compile from source
  • C standard compatibility issues between R 4.5.0+ and Apple's compiler
  • Missing header files like libintl.h
  • C++ standard library linking issues (symbol not found errors)
  • Runtime loading failures for C++ packages
  • OpenMP support for parallel processing

Packages This Helps

  • xgboost - Machine learning library
  • data.table - Fast data manipulation
  • jsonlite - JSON parsing
  • farver - Color space conversion
  • Rcpp-based packages - C++ integration packages
  • Most R packages requiring compilation from source

Troubleshooting

"Command not found" errors

  • Ensure Homebrew is properly installed
  • Restart Terminal after installing Homebrew

Packages still fail to compile

  • Verify the ~/.R/Makevars file was created correctly
  • Copy the configuration exactly as shown
  • Restart R/RStudio after making changes

Warnings about "newer macOS version"

  • These are harmless and can be ignored
  • Packages will still work correctly

C++ symbol errors like "symbol not found"

  • The configuration with -stdlib=libc++ and RPATH should resolve this
  • Common error pattern: symbol not found in flat namespace '__ZNSt3__1...'

"Ignoring duplicate libraries: '-lc++'" warnings

  • This is expected and harmless
  • Means libc++ is being linked twice (implicitly and explicitly)

Technical Background

This issue occurs because:

  1. R 4.5.0+ defaults to using the gnu23 C standard
  2. Apple's clang compiler (version 15.0.0) doesn't support gnu23
  3. LLVM clang (version 21.1.0+) does support gnu23
  4. C++ packages may use LLVM's libc++ standard library needing proper runtime linking
  5. Some packages need additional libraries like gettext for internationalization

Solution Details

The configuration:

  • Compiler: Uses LLVM clang instead of Apple's clang
  • C++ Standard Library: Explicitly uses LLVM's libc++ with -stdlib=libc++
  • Runtime Paths: Adds RPATH so packages can find symbols when loading
  • Include/Library Paths: Provides access to all required headers and libraries
  • OpenMP: Enables parallel processing support

System Requirements

  • macOS: Big Sur 11.0+ (Apple Silicon)
  • Hardware: M1, M2, M3, or newer Apple Silicon Macs
  • R: Version 4.5.0 or higher
  • Homebrew: For package management

One-Time Setup

Once configured, this setup persists and works for all future R package installations requiring compilation. No need to repeat the process.


Tested with: R 4.5.1, macOS Sequoia, M1 Max MacBook Pro
Last updated: August 2025

If this helps you, please ⭐ star this gist and share it with others facing the same issue!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment