Building shared libraries in subdirectories

后端 未结 2 1906
梦谈多话
梦谈多话 2021-02-07 07:25

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 as

2条回答
  •  长情又很酷
    2021-02-07 08:16

    callable.c

    #include 
    
    int main(int argc, char **argv) {
        printf("Hello World\n");
        return 0;
    }
    

    To get a .so file from the C file callable.c use

    R CMD SHLIB callable.c
    

    So now we have callable.so

    R normally requires that all C arguments be pointers, but if your main method ignores argc, then we can get around this. So to call main from callable.so from R we can write a method like so.

    main <- function() {
        dyn.load("callable.so")
        out <- .C("main", argc=0, argv="")
    }
    

    Calling that main function will run the C main function.

提交回复
热议问题