Building 64-Bit R Packages with Rtools and External Library/DLL
Introduction
As an R developer, you’re likely familiar with creating packages using the Rcpp skeleton. When building a package on Windows, one common issue is linking external libraries or DLLs for different architectures. In this article, we’ll explore how to build 64-bit R packages using Rtools and external library/DLLs.
Understanding R’s Multi-Arch Support
Before diving into the solution, it’s essential to understand how R handles multi-architecture support. By default, R uses a “multi-arch” approach, which allows you to link against both 32-bit and 64-bit libraries depending on the architecture of your system.
To achieve this, you need to specify the correct architecture when building your package using the --no-multiarch flag. This flag tells R to use the first R version in your system’s PATH, which determines the target architecture for the build process.
Example Build Process
Here’s an example of how you can build a 64-bit R package on Windows:
## Step 1: Create two separate packages for each architecture
Create two new directories for your package, e.g., `x86` and `x64`. Move the source files and dependencies to these directories.
## Step 2: Update Makevars for each package
Update the `Makevars` file in each directory to link against the correct library for that architecture. For example:
```markdown
## x86
PKG_CPPFLAGS = -I"D:/projects/source/my_project"
PKG_LIBS = -L"D:/projects/source/my_project/Release" -lproject
## x64
PKG_CPPFLAGS = -I"D:/projects/source/my_project"
PKG_LIBS = -L"D:/projects/source/my_project/Release" -lproject -lprojectx64
Step 3: Build and install packages
Build and install each package separately using the --no-multiarch flag. This will ensure that R uses the correct architecture for the build process.
R CMD BUILD --no-multiarch Package_1.0.tar.gz
R CMD INSTALL --no-multiarch Package_1.0.tar.gz
Windows Specific Setup
On Windows, you’ll need to add a manifest file to your Visual Studio project to resolve the common controls DLL issue. Here’s an example of how to do this:
#if defined _M_IX86
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_IA64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='ia64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#elif defined _M_X64
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='amd64' publicKeyToken='6595b64144ccf1df' language='*'\"")
#else
#pragma comment(linker,"/manifestdependency:\"type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif
Conclusion
In this article, we’ve explored how to build 64-bit R packages using Rtools and external library/DLLs on Windows. By creating separate packages for each architecture and updating the Makevars files accordingly, you can ensure that your package builds correctly for both 32-bit and 64-bit systems.
Remember to add a manifest file to your Visual Studio project to resolve the common controls DLL issue, which is a known bug in Windows LoadLibrary function. With these steps, you’ll be able to create robust R packages that work seamlessly across different architectures.
Last modified on 2025-03-05