How to make a C library accessible to the Csnippet #156
-
Hi again, I'm trying to sample from a truncated normal distribution and found a C library that does just that. The source code is available at: But I'm not sure how to make that C library available to my Csnippet. I've tried a few things. For example, giving the the name of the file in the pomp argument shlib.args and having a line in a global C function that mentions to include that library. Also tried compiling it with r cmd shlib. Nothing appears to work. I can provide a specific example if that's helpful, but I was wondering first whether there was a specific way in which I should try to include a C library. I feel like I'm so off base that my reproducible example might not be useful. If possible, it would be great if that way was done directly from R, so I can distribute the R code easily. Any tips would be great! Many thanks, Marie |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
Sorry to be so slow in replying. Here is an example I cobbled together to demonstrate two different ways of accomplishing this. The first compiles the auxiliary C code into an object file, "truncated_normal.o", and then links this with the pomp-generated C snippet codes. Note the use of the
Note that it isn't necessary to first compile "truncated_normal.c", and it is possible to do everything in the default place in the temporary directory. In this case, since it uses
The second method just inserts the entire contents of the header file and the C file into the pomp-generated C snippet file via the Note that the first two lines read the .c and .h files into the R session. The third line actually inserts the contents of the .h file into the appropriate place in the .c file (which is what the C preprocessor normally does). The fourth line just concatenates all the lines into one long string.
I hope this helps. Let me know if it does not, or if you have more questions. I want to thank you for bringing the question up: it will help make a useful entry to the FAQ. To make this issue self-contained, I've upload the .c and .h files here: truncated_normal.c (Somewhat annoyingly, they're treated as .txt files, so after downloading, the .txt file extension will have to be removed manually.) |
Beta Was this translation helpful? Give feedback.
-
@kingaa Fantastic, thank you! All three of these work. I'm not sure I fully understand why the first option has to create the .o file and then the specific .so .c files for pomp... but it works. I'm also not fully clear on the third option... but I think it's just because I don't fully understand C. I've created two completely artificial examples based on your two first examples that add more to the global file (because in my real model I have pretty extensive global files). It's pretty straightforward, but it took me a few minutes to figure it out. So here it is for posterity. Thank you! Marie
|
Beta Was this translation helpful? Give feedback.
-
Am re-opening this to remind me to use this material in an FAQ entry. Thanks again, @MarieAugerMethe! |
Beta Was this translation helpful? Give feedback.
-
Hi, |
Beta Was this translation helpful? Give feedback.
-
But of course! |
Beta Was this translation helpful? Give feedback.
-
The following updates the codes above for pomp versions ≥ 4. The codes below link the
The two files mentioned ( |
Beta Was this translation helpful? Give feedback.
The following updates the codes above for pomp versions ≥ 4. The codes below link the
truncated_normal
library using C snippets. This corresponds to my option two above. Note that things have gotten a bit simpler by version 4 of pomp.The two files mentioned (
trunc…