I'm running MinGW for the first time and I have trouble understanding where to put the external packages like the gsl library.
I'm showing first the program that I'm trying to run (it's in C:\Projects\C\)
#include <gsl/gsl_cdf.h>
#include <stdio.h>
int main() {
double bottom_tail = gsl_cdf_gaussian_P(-1.96, 1);
printf("A between [-1.96, 1.96]: %f\n", 1 - 2 * bottom_tail);
return 0;
}
this example is from the book 21st Century C
I've installed correctly pkg-config in order to obtain the flags for the compiler automatically. This is my makefile
CFLAGS= -g -Wall -O3
LDLIBS= `pkg-config --libs gsl`
CC=gcc
program:
I've downloaded the gsl library and put it in c:\gsl, and set $PKG_CONFIG_PATH to this location (where the .pc file is)
just running the command pkg-config --libs gsl I get the correct flags, but the make command crashes, telling me that
>>#include <gsl/gsl_cdf.h> No such file or directory
now I'm guessing that the install location is bad, but where should i put it?
Related
I've been trying all day to build and run a simple Ruby inside of C program.
This is a recurring topic here, and none of them are identical to my issue nor have any of the solutions helped me. I have the ruby-dev installed.
The pkg-config command gives this:
$ pkg-config --cflags --libs ruby-2.7
-I/usr/include/x86_64-linux-gnu/ruby-2.7.0 -I/usr/include/ruby-2.7.0 -lruby-2.7 -lm
The compile command gives this:
$ gcc -I/usr/include/x86_64-linux-gnu/ruby-2.7.0 -I/usr/include/ruby-2.7.0 -I. -lruby-2.7 -o hello *.c
/usr/bin/ld: /tmp/ccdKXtnU.o: in function 'main':
hello.c:(.text+0x9): undefined reference to 'ruby_setup'
collect2: error: ld returned 1 exit status
I have tried switching up the order of the includes. I have tried removing one then the other include. I have tried using a Makefile and running it thru make. I have tried breaking the program up into multiple files. I have tried symbolically linking the architecture relative config.h file into the main header file directory.
The only thing I can think of that I haven't tried is putting the name of the ruby object library that needs to be linked in on the command line, but I don't know the name, or location, of that file.
Here's the latest rendition of the program:
#include <stdio.h>
#include <ruby.h>
int main(void)
{
if (ruby_setup()){
puts("Hola!");
}
else{
printf("Hello World\n");
}
return(0);
}
One of the reasons that pkg-config separates cflags and libs is that they go in different places in the command-line (and sometimes different commands).
If you're going to compile and link in one command, it goes like this:
c99 -o hello $(pkg-config --cflags ruby-2.7) *.c $(pkg-config --libs ruby-2.7)
There's a certain logic to this arrangement. First, we tell the compiler where to look for header files (which it must see before it compiles your program), then where to find the program to compile, and finally where to find the libraries which are referred to by the program.
I just cloned libsndfile and created program.c file with the following contents:
#include <stdio.h>
#include <sndfile.h>
main() {
printf("hello world");
}
My goal is to get this file to compile using gcc (if indeed sndfile.h is the right header to include), however, I am not competent with c code nor the gcc compiler.
So far I've been referencing the gcc docs and the libsndfile FAQ and haven't been able to come up with a solution. I'm not sure if I even have a 'library' yet to feed the gcc -l option. Do I need to go through some sort of build process with the libsndfile source code first or can I 'link' to .c files?
So far, with program.c and the libsndfile clone directory in the same directory, I've tried the following:
gcc 'pkg-config --cflags libsndfile' program.c -o hello
gcc 'pkg-config --cflags ./libsndfile/sndfile.pc.in' program.c -o hello
I'm coding on my raspberry pi model B and did not install libsndfile, so, nothing is on my path... maybe I should rename sndfile.pc.in to sndfile.pc and somehow put it on my path? (not a linux guru either!)
If there are some resources you think I should study please comment! I'm perfectly willing to accept answers that simply push me enough to figure things out. Any help would be much appreciated.
First make sure you have gcc installed:
sudo apt-get install gcc
Then, install libsndfile1-dev:
sudo apt-get install libsndfile1-dev
I slightly fixed you code to avoid some warnings:
#include <stdio.h>
#include <sndfile.h>
int main(void) {
printf("hello world\n");
return 0;
}
Now you can compile:
gcc -o hello program.c
Execution:
bebs#rasp:~$ ./hello
hello world
bebs#rasp:~$
A small supplement of #alpereira7's answer.
It would cause linking failure without its lib. gcc -lsndfile -o hello program.c should be a 100% solution.
I keep getting the error "undefined reference to WInMain#16" when I include SDL2/SDL.h in my C file. It's a simple "Hello" program with the SDL include, and if I remove the SDL include it compiles just fine (as expected).
The problem is I'm new with the compile flags for C (and SDL) and I'm not sure how I link(?) the files together (or if that's necessary). I'm coding using Sublime Text 3 so I'm not sure how you would link SDL as you would when using an IDE.
(D:\CODE\Privata Projekt\C\test.c)
#include <stdio.h>
#include "SDL2/SDL.h"
int main(int argc, char *argv[]) {
printf("hello\n");
return 0;
}
My paths to MinGW and SDL2 is:
C:\MinGW\include\SDL2 (all my sdl header files reside in here too)
C:\MinGW\include\SDL2\bin
C:\MinGW\include\SDL2\lib
C:\MinGW\include\SDL2\share
And I build the program with
gcc test.c -o test
EDIT:
What worked for me was to use these flags, in this exact same order
-lmingw32 -LC:\MinGW\include\SDL2\lib -lSDL2main -lSDL2
You need to link with the library as well. You can do it by passing the correct options on the command line: -L to tell the linker where to find the library, and -l (lowercase L) to tell the linker to link to the library.
Like
> gcc test.c -o test -LC:\MinGW\include\SDL2\lib -lSDL2
(I don't know the name of the library, so change SDL2 to the appropriate name.)
If there is problem running your program due to the loader not finding the SDL2 library, you may have to add another option which tells linker the place of the dynamic library:
> gcc test.c -o test -LC:\MinGW\include\SDL2\lib -Wl,-rpath=C:\MinGW\include\SDL2\lib -lSDL2
I don't know if it's needed or even used on Windows though. You might have to copy the DLL to the directory where the executable is.
I got a problem while installing the GNU Scientific Library (gsl).
I put the package on my desktop, and did "./configure", "make", and "sudo make install", according to the document included. I checked the /usr/local/include directory, there is a newly created "gsl" folder in there. But When I tried to use the functions provided by the library, the "undefined reference to 'gsl_sf_beta_inc'" error occurred. Here is my code.
#include <stdio.h>
#include <gsl/gsl_sf_gamma.h>
int main (void)
{
double a = 20;
double b = 1000;
double x = 0.5;
double result = gsl_sf_beta_inc(a, b, x);
printf("%f/d", result);
return 0;
}
I sensed that the problem might be caused by the fact I put the package on the desktop, so the binary code generated by the "make" command goes there, which is wrong.
So, is my guess correct? If it is, where should I put them? If it is not, what should I do?
Thanks.
You need to link the library, assuming the make install was successful.
The gsl's documentation says this should work
(note the two necessary linking options for gsl to work: "-lgsl -lgslcblas"):
gcc -I/usr/local/include -L/usr/local/lib main.c -o main -lgsl -lgslcblas -lm
Alternative "cblas" instead of gsl's cblas is also possible as per: alternate cblas for gsl
Use pkg-config --libs gsl to find out what the necessary linkers are to be and then just link them. An optional thing would be to check pkg-config --cflags gsl. The second gives you the directory of the include files if they are not installed in the default /usr/include/ directory. If you've installed it there you can just ignore that.
The output of pkg-config --libs gsl would be
-lgsl -lgslcblas -lm
That means that those three have to be linked. So while compiling your program you do that by,
gcc name.c -lgsl -lgslcblas -lm
I am having trouble installing a dependency for a program that itself depends on pcre.h. I have this installed to /opt/local/include, but the C compiler does not see it and thus gives me:
error: pcre.h: No such file or directory
I have confirmed this by writing a hello world program that tries to include it:
#include <pcre.h>
#include <stdio.h>
int main(void)
{
printf("hello, world\n");
return 0;
}
This also gives the error unless I specify the path as </opt/local/include/pcre.h>.
I would like the C compiler to find this by default but I do not know where this is configured. Tab completion hasn't revealed any HEADER_PATH environment variables and I cannot find anything like it that isn't specific to XCode. I am, however, using Mac OSX Snow Leopard on the off chance that makes a difference.
Use -I /opt/local/include on the command line or C_INCLUDE_PATH=/opt/local/include in the environment.
Use the pcre-config utility to get the right flags:
$ pcre-config --libs --cflags
-L/opt/local/lib -lpcre
-I/opt/local/include
If you're compiling via the command line,
$ gcc -Wall -g `pcre-config --libs --cflags` main.c