[R] Using C library in R

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sat Jul 23 02:43:59 CEST 2016


Read the Posting Guide. This will tell you at least two important things:

1) Post using plain text. HTML mangles code.

2) Interfacing R with other languages is off-topic on this list. There are other lists where such issues are on-topic. Your post is a bit like walking into a bowling alley and asking if anyone there can solve your chess problem... someone might be able to, but it isn't very efficient and disturbs the bowlers unnecessarily.
-- 
Sent from my phone. Please excuse my brevity.

On July 22, 2016 11:12:57 AM PDT, "Ganz, Carl" <carlganz at ucla.edu> wrote:
>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]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide
>http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list