简体   繁体   中英

Using zeroin.c in a cpp program

I'm writing a small Cpp program and I'd like to include a function I normally use in R (the uniroot function). This R function is actually a wrapper to a function written in C called zeroin and I'd like to use it in my Cpp file. Trouble is I've not done anything like this before so I'm unsure on how to do this or even where to start. The folder for zeroin is here: Zeroin . I guess I need to include the zeroin.c file somehow. I've read here that to use such a function I need to

Use one of the following notations to declare that an object or function has the linkage of language language_name:

extern
"language_name" declaration ; 
extern "language_name" { declaration ;
declaration ; ... }

The first notation indicates that the declaration (or definition) that immediately follows has the linkage of language_name. The second notation indicates that everything between the curly braces has the linkage of language_name, unless declared otherwise. Notice that you do not use a semicolon after the closing curly brace in the second notation.

But I'm still unsure which of these, if either I need to do or how to then go about compiling and linking the files to get the proper output. Just compiling and linking Cpp is fine but I've not tried Cpp and C before.

To make matters more confusing the downloadable folder containing zeroin.c contains a load of other files as well and I'm unsure if they are needed or relevant.

Does anybody have any advice what I should be doing with my cpp file?

Just to give more information on what I'm wanting to do in Cpp is compute the root of the cdf of a binomial probability distribution (I haven't yet gotten around to finding such a function for the binomial dist in Cpp yet). Incase anyone knows R and Cpp - in RI would do it like this:

binomcalc <- function(p, p0, N, B){pbinom(B,N,p)-p0}
soln <- uniroot(binomcalc, c(0,1), p0=0.95, B=10, N=20)

And soln would contain the value of p at which p0 = 0 ie the root.

Now I could use RInside or something like that to embed R in my Cpp program, but it strikes me as maybe overkill.

Thanks, Ben.

-- EDIT -- I tried the following tutorial/explanation and copy the Cfile.c and the main.cpp file and do the following as it instructs:

yrq12edu@env-12bw:~/Desktop/example$ gcc -c -Wall -Werror -fPIC Cfile.c

Ok that's the C file compiled.

yrq12edu@env-12bw:~/Desktop/example$ gcc -shared -o libCfile.so Cfile.o

Now it's compiled as a shared library. I have the files:

yrq12edu@env-12bw:~/Desktop/example$ ls
Cfile.c  Cfile.o  libCfile.so  main.cpp

But next if I do the next instruction:

$ g++ -L/home/himanshu/practice/ -Wall main.cpp -o main -lCfile

on my machine as:

yrq12edu@env-12bw:~/Desktop/example$ g++ -L/local/yrq12edu/Desktop/example/ -Wall main.cpp -o main -lCfile

Now this does work so long as the .so file is in my LD_LIBRARY_PATH. I wonder is there a way to do this as a static library or does it have to be a shared library? I'm going to give it a go with zeroin.c

-- EDIT --

Trying to compile zeroin.c results in an error:

yrq12edu@env-12bw:~/Desktop/example$ gcc -c -Wall -Werror -o zeroin.o zeroin.c
zeroin.c: In function 'zeroin':
zeroin.c:80:17: error: 'EPSILON' undeclared (first use in this function)
zeroin.c:80:17: note: each undeclared identifier is reported only once for each function it appears in
zeroin.c:118:7: error: suggest explicit braces to avoid ambiguous 'else' [-Werror=parentheses]
cc1: all warnings being treated as errors

I think it's because math.h is not included, I would have thought it would be like a lot of the common headers in Cpp where the compiler find it and includes it - maybe I need to download math.h for C and put it in the same directory.

The include line in zeroin.c is #include "math.h"

Assuming you have one c source file zeroin.c and a corresponding header file zeroin.h, you would include zeroin.h from your C++ source file like so:

extern "C" {
#include "zeroin.h"
}

You can then compile zeroin.c with your C-compiler, compile your C++ code with your C++-compiler and link the resulting object files together.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM