I have installed gsl library using sudo apt install libgsl2 command.However when I run the following program using gcc,
#include<stdio.h>
#include<math.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
double x;
double nu;
int main(){
x=4;
nu=2;
double chi;
chi=gsl_cdf_chisq_P (double x, double nu);
printf("%lf",chi);
}
The following message is displayed when I compile using gcc -o file filename.c
gsl/gsl_rng.h: No such file or directory
compilation terminated.
Have I missed any step during installation?Or is it required to use any flag during compilation?
You need to install the libgsl-dev package.
This package contains the include files necessary for your development.
If in doubt checkout the list of files in the package
$ dpkg -L libgsl2
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/libgsl.so.19.0.0
/usr/lib/x86_64-linux-gnu/libgslcblas.so.0.0.0
/usr/lib/x86_64-linux-gnu/libgslcblas.so.0
/usr/lib/x86_64-linux-gnu/libgsl.so.19
/usr/share/doc
/usr/share/doc/libgsl2
...
just doc files and nothing more
And with the dev package you get
$ dpkg -L libgsl-dev
/usr/lib/x86_64-linux-gnu
/usr/lib/x86_64-linux-gnu/libgsl.a
/usr/lib/x86_64-linux-gnu/libgslcblas.a
/usr/lib/x86_64-linux-gnu/pkgconfig
/usr/lib/x86_64-linux-gnu/pkgconfig/gsl.pc
/usr/include
/usr/include/gsl
...
and all the header files here
That is the dev package gives you all the necessary headers and the static import libraries useful in case of a static build.
P.S. Do not be put off by the lack of 2 in the name of libgsl-dev package compared to libgsl2. Both would be of the same 2.x version on your Ubuntu system or a derivative thereof.
P.P.S. There are a few more issues with your code.
The function that you are using gsl_cdf_chisq_P() belongs to the collection of CDF functions so you need to include <gsl/gsl_cdf.h>. And you need to call the function correctly as gsl_cdf_chisq_P (x, nu) instead of gsl_cdf_chisq_P (double x, double nu).
The resulting code could look like this:
#include<stdio.h>
#include<math.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_cdf.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
int main(){
double x=0.05;
double nu=9;
double chicdf, chi;
chi=gsl_cdf_chisq_Pinv (1-x, nu);
chicdf=gsl_cdf_chisq_P (chi, nu);
printf("ICDF[Chisquared<%d>](1 - %lf) = %lf\n"
"CDF[Chisquared<%d>](%f) = %lf\n",
(int)nu,x,chi,(int)nu,chi,chicdf);
return 0;
}
To compile it run
gcc -Wall -O2 testchisq.c -o testchisq -lgsl -lgslcblas -lm
It should print
ICDF[Chisquared<9>](1 - 0.050000) = 16.918978
CDF[Chisquared<9>](16.918978) = 0.950000
Related
I just started with c development and I need to compile and link a program which uses the Accelerate Framework from Apple:
Simple example accelerate.c:
#include <stdio.h>
#include <Accelerate/Accelerate.h>
double vectorvector_product(double * a, double * b, int dim){
// This function returns in res the elementwiseproduct between a and b,
// a and b must have the same dimension dim.
return cblas_ddot(dim,a,1,b,1);
}
int main(){
double a[4] = {1.0,2.0,3.0,4.0};
double b[4] = {1.0,2.0,3.0,4.0};
double res = vectorvector_product(a,b,4);
printf("Res: %f",res);
}
I compiled it with clang:
>>> cc -Wall -g -c accelerate.c
And obtained a new file accelerate.o
What would I do now in order to properly link it?
All I know is that this Accelerate framework is located at /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Accelerate.framework
>>> ls /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Accelerate.framework
Accelerate.tbd Frameworks Headers Modules Versions
p.s.: If I Run this program with Xcode it magically works, but I need to do it from the command line and I would like to know what I'm doing.
Apparently the correct way to link Accelerate.h is by passing -framework Accelerate as argument e.g.
>>> cc -framework Accelerate accelerate.c
will compile and link accelerate.c by generating an executable a.out.
I am trying to wrap the GLIBC math functions including summation, division, multiplication, Cosine, log, and etc. I found a similar post here.
I have created a wrapper script for the log function in which prints something before calculation. Something like this for log function:
#include <dlfcn.h>
#include <stdio.h>
#include <math.h>
static double (*real_log)(double dbl);
double log(double dbl)
{
printf("value is %0.16f\n" , dbl);
real_log = dlsym(RTLD_NEXT, "log");
real_log(dbl);
}
And the test file is something like this:
#include <stdio.h>
#include <math.h>
int main () {
double var;
var = log(2.7);
printf("log is equal to %0.16f\n" , var);
return 0;
}
I have re-compiled the wrapper to create the shared library by this command: gcc -fPIC -shared -o libpreload.so wrapper.c -ldl.
I compiled test file: gcc -fno-builtin-log test.c -o test -lm.
Then I run my test script using: LD_PRELOAD=/path/to/libpreload.so ./test
The output now prints
value is 2.7000000000000002
log is equal to 0.0000000000000000
The log returns zero value while I expect to get the real calculation like this:
value is 2.7000000000000002
log is equal to 0.9932517730102834
I'm so beginner on C programming and also Glibc functionality.
I would appreciate any ideas.
I'm trying to compile a shared library (.so) with the following code:
libreceive.h:
#include <stddef.h>
int receive(int sockfd, void *buf, size_t len, int flags);
libreceive.c
#include <stddef.h>
#include <libreceive/libreceive.h>
int receive(int sockfd, void *buf, size_t len, int flags){
return recv(sockfd, buf, len, flags);
}
the problem here is that I'm trying to include the .h in the library that I'm building and using it in the same time from the same library in the .c .
I know that what I'm trying to do is possible, but I can't manage to do it.
How can I do that please.
the code I'm trying is:
gcc -o libreceive.o -c -include libreceive.h libreceive.c
I get the following error:
fatal error: libreceive/libreceive.h: No such file or directory
compilation terminated.
the problem here is that I'm trying to include the .h in the library that I'm building and using it in the same time from the same library in the .c .
I know that what I'm trying to do is possible, but I can't manage to do it.
How can I do that please.
Since libreceive.h and libreceive.c appear to be in the same directory (judging from your compiler call), the normal way is
#include "libreceive.h"
In order to use
#include <libreceive/libreceive.h>
libreceive.h would have to lie in a directory called libreceive, and that directory would have to be part of the include path. It is possible to achieve this, but I believe it is neither necessary nor useful here.
You are missing out a few steps here.
Consider the following setup.
File: add.c
#include "header.h"
int add(int a, int b)
{
printf("SIZE: %d\n", SIZE);
return a+b;
}
File: sub.c
#include "header.h"
int sub(int a, int b)
{
printf("SIZE: %d\n", SIZE);
return a-b;
}
File: header.h, located in directory called include.
#include <stdio.h>
#define SIZE 100
int add(int a, int b);
int sub(int a, int b);
So to step by step build a .so file.
/* Build `.o` files first */
$ gcc -fPIC -c sub.c -I path/to/include/
$ gcc -fPIC -c add.c -I path/to/include/
/* Build shared library called libsample.so */
$ gcc -shared -o libsample.so add.o sub.o
The above command will build a .so by name libsample.so.
Where all definition from .c(like functions) and .h(like #defines) will get included in your library.
How to use this in your code:
Consider the file
File: main.c
#include <stdio.h>
int main()
{
int a = 3, b = 4;
printf("Return : %d\n", add(a, b));
return 0;
}
To make use of your library libsample.so.
$ export LD_LIBRARY_PATH=/path/to/direc/containing/.so/file
$ gcc -o exe main.c -lsample -L/path/to/direc/containing/.so/file
The above command should create a binary called exe.
$./exe
SIZE : 100 /* SIZE Defined in .h file */
Return : 7 /* Defined in add.c */
You can refer this guide : http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html
Finaly I decided to use #include "libreceive.h" as suggested by the guys. the probleme I had is that the compiler was looking for my so in /usr/lib wich is the default when id do sudo gcc and my usr had the $LD_LIBRARY_PATH at /usr/local/lib and therefore gcc coudn't find my library at compile time
another problem was that the program that call thos .so was looking fro the .h in some folder that doesn't exist and I had to add it.
thanks guys for you answers
I started implementing a large program. But I ran into a massive issue. So here is very simplified code of my program. I have a separate .c file for my functions which is normal.c the main program is main.c and I have linked those two with cal.h header file.
main.c
#include <stdio.h>
#include "cal.h"
void main()
{
int num1, num2, ans;
num1=5;
num2=5;
ans=add(num1, num2);
printf("%d" ,ans);
}
normal.c
#include "cal.h"
int add(int num1, int num2)
{
return num1+num2;
}
cal.h
#ifndef CAL_H_INCLUDED
#define CAL_H_INCLUDED
#include <errno.h>
int add(int num1, int num2);
#endif // CAL_H_INCLUDED
but when I compile this, it gives out the error
..\main.c|10|undefined reference to `add'|
I'm using CodeBlocks v.13.12 in Windows 8.1 Any answer for this question is much appreciated. I tried with CodeLite as well, but the same error occurs. Thank you!
Complete compilation of the code is required in ubuntu terminal
use the following
gcc normal.c main.c -o out -lm
Code blocks have automatic linking but for that you need to have your source and header files under a project.
I had the same issue when I made individual .c & .h files and expected the IDE to link the object files but it failed. I put them under a project and it worked!
Use complete compilation of your code.
if your c codefiles main.c and normal.c are in ./src and header file cal.h is in ./inc follow below method from current dir(.)
g++ ./src/main.c ./src/normal.c -I ./inc -o main
now main is out binary file to execute.
"R.h" and "Rmath.h" are header files for an interface between R.app and C. But, they seems to be readable only through a R command 'R CMD SHLIB something.c'
I wish to compile my native C program to include them using gcc. I'm using Snow Leopard where I'm not able to locate those header files!
Any help?
Please see the 'Writing R Extensions' manual about details, you can easily compile and link against Rmath.h and the standalone R Math library -- but not R.h. (Which you can use via Rcpp / RInside but that is a different story.)
There are a number of examples floating around for use of libRmath, one is in the manual itself. Here is one I ship in the Debian package r-mathlib containing this standalone math library:
/* copyright header omitted here for brevity */
#define MATHLIB_STANDALONE 1
#include <Rmath.h>
#include <stdio.h>
typedef enum {
BUGGY_KINDERMAN_RAMAGE,
AHRENS_DIETER,
BOX_MULLER,
USER_NORM,
INVERSION,
KINDERMAN_RAMAGE
} N01type;
int
main(int argc, char** argv)
{
/* something to force the library to be included */
qnorm(0.7, 0.0, 1.0, 0, 0);
printf("*** loaded '%s'\n", argv[0]);
set_seed(123, 456);
N01_kind = AHRENS_DIETER;
printf("one normal %f\n", norm_rand());
set_seed(123, 456);
N01_kind = BOX_MULLER;
printf("normal via BM %f\n", norm_rand());
return 0;
}
and on Linux you simply build like this (as I place the library and header in standard locations in the package; add -I and -L as needed on OS X)
/tmp $ cp -vax /usr/share/doc/r-mathlib/examples/test.c mathlibtest.c
`/usr/share/doc/r-mathlib/examples/test.c' -> `mathlibtest.c'
/tmp $ gcc -o mathlibtest mathlibtest.c -lRmath -lm
/tmp $ ./mathlibtest
*** loaded '/tmp/mathlibtest'
one normal 1.119638
normal via BM -1.734578
/tmp $