I have a c script
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
int main(void)
{
const gsl_rng_type * T;
gsl_rng * r;
int i;
double a,b;
double num;
a=10;
b=7.2;
gsl_rng_env_setup();
T=gsl_rng_default;
r=gsl_rng_alloc (T);
for(i=0; i<10; i++)
{
num = gsl_ran_gamma(r,a,b);
printf("%.8f \n",num);
}
gsl_rng_free(r);
return 0;
}
Which I have successfully compiled on a linux machine. I want to use the gsl library for other applications on my mac. So I first installed gsl using homebrew which seemed to be successful. To make sure everything was working right I tried to compile and run this script as follows
[ACC-259-imac:GDSC Gene Expression Modeling jmannhei$ gcc -Wall gamma.c -o gamma.out -lm -lgsl -lgslcblas
which resulted in the following output
gamma.c:5:10: fatal error: 'gsl/gsl_rng.h' file not found
#include <gsl/gsl_rng.h>
^
1 error generated.
Which is exactly how I compiled it on Linux so I am not sure what is awry as I have compile c scripts in the past using this format from terminal on a mac before. My guess is it is not linking properly but I am not sure what I need to do to fix it. Thanks
Find your header file directory with brew list gsl or locate gsl/gsl_rng.h then tell your compiler where the headers are. I would need the following, for example,
gcc -Wall -I/usr/local/Cellar/gsl/1.16/include/ gamma.c -o gamma.out -lm -lgsl -lgslcblas
Related
The linker keeps telling me that clock_gettime() is undefined.
I have tried using -lrt but then gcc says that it can't find that either.
Am I missing some extra library I needed to download?
// test.c
#include <time.h>
#include <stdio.h>
int main()
{
struct timespec x;
clock_gettime(CLOCK_REALTIME, &x);
printf("%d\n", x.tv_sec);
return 1;
}
When I try to compile this using GCC with MinGW, it puts out this:
gcc test.c
undefined reference to `clock_gettime'
or
gcc test.c -lrt
cannot find -lrt: No such file or directory
I have these five source
main.c src_print1.c src_print2.c header_print1.h header_print2.h
the contents are simple and are as following for respective files:
main.c
#include "header_print1.h"
#include "header_print2.h"
int main(int argc, char** argv) {
print1();
print2();
return 0;
}
header_print1.h
#ifndef PRINT_1
#define PRINT_1
#include <stdio.h>
#include <stdlib.h>
void print1();
#endif
header_print2.h
#ifndef PRINT_2
#define PRINT_2
#include <stdio.h>
#include <stdlib.h>
void print2();
#endif
src_print1.c
#include "header_print1.h"
void print1() {
printf("Hello 1\n");
}
src_print2.c
#include "header_print2.h"
void print2() {
printf("Hello 2\n");
}
Using gcc I have tried to compile using the following command line:
gcc -I ./ -o test -c main.c src_print1.c src_print2.c
Everything is in the same folder.
The error I get is:
gcc: cannot specify -o with -c or -S with multiple files
I looked up at gcc manual, but actually I don't understand what to do in this case, since usually I use IDE and not the command line.
IMHO, if you rewrite your compilation statement like
gcc -I./ -o test main.c src_print1.c src_print2.c
You'll be good to go. There is no need for -c flag [NOTE] when you're specifying the output binary using -o.
Also, as mentioned here, all the files are in same directory, you can even shorten the statement as
gcc -o test main.c src_print1.c src_print2.c
Suggestion: While the above change(s) will do the job, this is not considered an elegant way of doing so. Please consider creating a makefile which will make your life easier.
[Note]:
Regarding the -c option, as per the online gcc manual, (emphasis mine)
-c
Compile or assemble the source files, but do not link. The linking stage simply is not done. The ultimate output is in the form of an object file for each source file.
So, it should be clear by now, why you got the error.
The following code compiles on other systems, but not on my Ubuntu 12.04 64bit guest in Virtualbox 4.3.10 on a Windows 7 64bit host.
hello.c
#include "header.h"
int main(int argc, char *argv[]){
int i;
for(i=0; i<argc; i++)
printf("%s\n", argv[i]);
double x;
x = testfunction();
printf("%f \n", x);
return 0;
}
hello2.c
#include "header.h"
double testfunction ()
{
int i = 1;
double j = 0;
for(i=0; i<1000000; i++)
j += sin(i/M_PI);
return j;
}
header.h
#include <math.h>
#include <stdio.h>
double testfunction();
When I attempt to compile using
gcc -lm -o hello hello.c hello2.c
I receive the error
/tmp/ccirukEU.o: In function testfunction':
hello2.c:(.text+0x33): undefined reference tosin'
collect2: ld returned 1 exit status
The error remains even if I include math.h directly in hello2.c. Calculating sin(2/M_Pi) rather than sin(i/M_Pi) removes the error, possibly because gcc then works out the sine itself rather than using the math library.
Use -lm in the end, as in:
gcc -o hello hello.c hello2.c -lm
This ensures that the linker can realize that there are dependencies missing. Using -lm in the beginning is known to raise problems, because by the time the linker looks at the math library, it hasn't looked at your code yet, so there are no dependencies unresolved.
I followed the instructions to install GNU Readline, as well as Curses, however I get some linker issues that I am unsure how to resolve. The following is my program:
main.c
#include <stdio.h>
#include <stdlib.h>
#include <curses.h>
#include <term.h>
#include <readline/readline.h>
#include <readline/history.h>
int main()
{
char * line = readline ("Enter a line: ");
free (line);
return 0;
}
I compiled using: gcc -o main {,.c} -lreadline -lncurses (and the readline includes were where they were supposed to be, in usr/includes...
Running main gave me:
./main: symbol lookup error: /usr/local/lib/libreadline.so.6: undefined symbol: UP
Any direction as to go about resolving this would be greatly appreciated.
sudo apt-get install libreadline6-dev
gcc -o main {,.c} -lreadline -lncurses
I'm trying to write a c function(that later will be used in R scripts) that uses BLAS lib from R
#include <stdio.h>
#include <R.h>
#include <R_ext/BLAS.h>
void foo(int *dimension, double *vect1, double *vect2)
{
const int dim = dimension[0];
const int incxy = 1;
//swaps two vectors
F77_NAME(dswap)(&dim,vect1,&incxy,vect2,&incxy);
}
I compile the code using :
R CMD SHLIB foo.c
I get the error :
foo.o:foo.c:(.text+0x41): undefined reference to `dswap_'
What am i missing ?
Including a header file is not the same as linking a library.
I don't actually know anything about R, but I did some searching and found http://cran.r-project.org/doc/manuals/R-exts.html#Creating-shared-objects which indicates you need the BLAS_LIBS variable - if you're not using a makefile it looks like you can just get the output of the command "R CMD config BLAS_LIBS" and then include the output on the command line. You might also be able to just add it to the PKG_LIBS variable, but I don't know enough about R to be sure.
EDIT:
have set file Makevars.win to include
PKG_LIBS=$(BLAS_LIBS)
PKG_LIBS=$(LAPAK_LIBS)
That replaces the PKG_LIBS variable with LAPAK_LIBS. Try it with += instead of =.
have set file
Makevars.win to include
PKG_LIBS=$(BLAS_LIBS)
PKG_LIBS=$(LAPAK_LIBS)
but error persists.
BINGO : added to
"R CMD SHLIB foo.c"
the output from
"R CMD config BLAS_LIBS"
as in
"R CMD SHLIB foo.c -LC:/PROGRA~1/R/R-212~1.2/bin/i386 -lRblas"
and now it works.
It needs to be linked to whatever provides the dswap function. I would guess that's in the R library.
It works here (and please ignore that my ~/.R/Makevars selects a particular CC and CFLAGS; and also ignore minor whitespace edits):
edd#max:/tmp$ R CMD SHLIB foo.c
gcc-4.5 -I/usr/share/R/include -fpic -O3 -g0 -Wall -pipe -pedantic -std=gnu99 \
-c foo.c -o foo.o
gcc -shared -o foo.so foo.o -L/usr/lib64/R/lib -lR
edd#max:/tmp$ cat foo.c
#include <stdio.h>
#include <R.h>
#include <R_ext/BLAS.h>
void foo(int *dimension, double *vect1, double *vect2)
{
const int dim = dimension[0];
const int incxy = 1;
//swaps two vectors
F77_NAME(dswap)(&dim,vect1,&incxy,vect2,&incxy);
}
edd#max:/tmp$
Ubuntu 10.10, stock packages for R, gcc etc. Maybe your R is locally built in a static library configuration?