[R] Building shared libraries in subdirectories

Fg Nu fgnu32 at yahoo.com
Mon Jun 17 15:20:47 CEST 2013

Hi all,

I tried SO for this, but got no response. Trying here in hope of better luck.


I am trying to build an R package that uses some C code. I have a C library that is compiled into an executable, that can be called from the command line. There is a Makefile associated with it. 

I am trying to grok the information [here][1], where it says

> If you want to create and then link to a library, say using code in a
> subdirectory, use something like

     .PHONY: all mylibs
     all: $(SHLIB)
     $(SHLIB): mylibs
             (cd subdir; make) 

> Be careful to create all the necessary dependencies, as there is a no
> guarantee that the dependencies of all
> will be run in a particular order (and some of the CRAN build machines
> use multiple CPUs and parallel makes).

If I create a new subdirectory of the `src` folder in my package, called `someLibrary`, with the code and Makefile unchanged, and in turn, in the original `Makevars` file for my package I add the above code unchanged, then I will be able to build that shared library to be exported using `useDynLib`?

Edit 1:
Following information [here][2], I changed the `Makefile` to create a shared library by adding 

    CFLAG = -fPIC -g -O3 
    LDFLAGS= -shared

However, this leads to the problem that the `.so` file is not exported directly to the `libs` directory of the package. If I hard code the path into the target, then the file is sent to the `libs` directory of the package (this is all by the way of calls to `R CMD INSTALL myPackage`). 

Edit 2:
Lastly, I would like to know how to make calls to the shared library, given that it has a `main()` method that I could call from the command line executable.

What is the procedure to expose this to the R `NAMESPACE`, so that it can be called via `.Call`?

PS.  Please let me know if I should make the last bit a separate question.

  [1]: http://cran.r-project.org/doc/manuals/R-exts.html#Using-Makevars
  [2]: http://stackoverflow.com/questions/8096015/creating-a-simple-make-file-to-create-a-shared-library-whats-wrong-with-this-m

More information about the R-help mailing list