[R] Using C library in R
Ganz, Carl
carlganz at ucla.edu
Fri Jul 22 20:12:57 CEST 2016
Hello everyone,
I am attempting to link to a C library named libxlsxwriter (http://libxlsxwriter.github.io/) that creates and styles XLSX files, but after several days of repeatedly reading "Writing R Extensions", and I am stuck and hoping someone can help me.
The C library is easy to use and works with C++ as you would expect.
For example, here is test.cpp:
#include <xlsxwriter.h>
int main() {
lxw_workbook *workbook = workbook_new("myexcel.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
int row = 0;
int col = 0;
worksheet_write_string(worksheet, row, col, "Hello me!", NULL);
return workbook_close(workbook);
}
To compile this I need to run the makefile in the libxlsxwriter folder, which compiles the libxlsxwriter library, and then call:
cc test.cpp -o test -Ipath.to.xlsxwriter.h path.to.libxlsxwriter.a -lz
This generates an executable that creates an excel document. I understand this command says to compile test.cpp and specifies where to search for headers, and what libraries to link to so to get this to work with R all I should need to do is make the library, point to the header, and link to the .a file.
To get this to work with R, I created a libxlsxwriter folder in my /src folder with the libxlsxwriter library in it, and added this makevars to my /src:
PKG_CFLAGS=
# specify header location
PKG_CPPFLAGS=-Ilibxlsxwriter/include
# specify libs to link to
PKG_LIBS=liblsxwriter/lib/libxlsxwriter.a -lz
# make libxlsxwriter
libxlsxwriter/lib/libxlsxwriter.a:
cd libxlsxwriter;$(MAKE)
When I build the package I can see that this runs the makevars, and generates the .a and .dll files in libxlsxwriter. I get no errors associated with headers or the libraries so I thought I would be good to go, but I can't seem to get anything to run.
Here is my test.cpp code I am trying to run in R with Rcpp:
#include <Rcpp.h>
#include <xlsxwriter.h>
using namespace Rcpp;
//' @useDynLib libxlsxwriter
//' @export
//' @import Rcpp
// [[Rcpp::export]]
void test() {
lxw_workbook *workbook = workbook_new("myexcel.xlsx");
lxw_worksheet *worksheet = workbook_add_worksheet(workbook, NULL);
int row = 0;
int col = 0;
worksheet_write_string(worksheet, row, col, "Hello me!", NULL);
workbook_close(workbook);
}
I am fairly certain I am doing something wrong with my makevars that is preventing test.cpp from being compiled. I tried running the makevars for libxlsxwriter from the command line and then building using just this makevars:
PKG_CFLAGS=
# specify header location
PKG_CPPFLAGS=-Ilibxlsxwriter/include
# specify libs to link to
PKG_LIBS=liblsxwriter/lib/libxlsxwriter.a -lz
I see the .o files for test.cpp being built, which is a good sign, and indicates there was a problem with my previous makevars, but I get errors saying undefined reference for all the functions from libxlsxwriter, so clearly thinks aren't linking like I need them to. I am out of ideas at this point, so any guidance would be greatly appreciated.
A github repo with my code is here: https://github.com/carlganz/lwritexlsx
I was using this webpage as a guide for how to include a C library in an R package: http://mazamascience.com/WorkingWithData/?p=1151
Kind Regards,
Carl Ganz
[[alternative HTML version deleted]]
More information about the R-help
mailing list