4

I use igraph in R. Now I am trying to move to Rcpp. igraph is written in C, but can be called from R. Now from C++ I would like to call it directly.

Currently my Rcpp C++ code calls R, which calls C again. That seems stupid. Instead, I wan't to call the C library directly from my Rcpp C++ code. One way, seems to be to simply download the igraph headers and add this in Rcpp:

#include <igraph-0.7.1\include\igraph.h>

but then the compiler complains that it can't find the actual code (only headers):

...
functions.o:functions.cpp:(.text+0x2fd): undefined reference to `igraph_matrix_set'
...

Compiling all of ipgraph as a Rcpp dependency seems to be a difficult, or at least I couldn't find no good documentation on how this could be done.

Given that igraph is called from R, i.e. is running on my system, I suspect that there should be a way to call it from C++ without going the detour through R, someting like

// [[Rcpp::depends(igraph)]]

Any pointers would be greatly appreaciated.

sheß
  • 484
  • 4
  • 20
  • 2
    Your include path looks fishy, it should just be `#include ` instead. You probably have to fix it in your CMakeLists or whatever you used to include the headers. – nada Sep 09 '19 at 11:53
  • Thanks for pointing this out, I changed it to what you suggested and added `Sys.setenv("PKG_CXXFLAGS"="-I'D:/path/to/folder/igraph-0.7.1/include/'")` in R, which works as bevofer, but doesn't solve the original issue – sheß Sep 09 '19 at 12:06
  • 1
    Seeing the whole build script might help us locate your problem. – nada Sep 09 '19 at 13:05
  • 1
    You mustn’t use backslashes in include paths: its meaning is not defined by the C++ standard, and in practice it won’t work on non-Windows systems. This is in addition to what nada said. – Konrad Rudolph Sep 09 '19 at 13:56
  • @nada, there is no explicit build script, as I'm trying to set this up to run "out of the box" with Rcpp. So there's only what Rcpp generates automatically in the background. Not sure what of this to post to make locating the problem easier. – sheß Sep 09 '19 at 14:02

1 Answers1

5

While it is possible for an R package to provide C functions that other R packages can call, the igraph package does not do this. I see the following possibilities:

  • Lobby for the igraph package to provide its functions also at the C level, preferably by providing a patch. See WRE for details.

  • Link with a system installed igraph C library, i.e. what you are doing now.

  • Include the igraph C library in your package.

  • Write an additional package that only includes the igraph library plus the necessary code for a C/C++ API. This should be relatively straight forward when using Rcpp.

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75