flags for compiling source code: pkg-config command

Mac OS X

Vinh Nguyen


October 30, 2009

so i was trying to compile some C code on my server at school that involves the math and the GNU Scientific Library (gsl) libraries. that is, the source has the following lines:

{c} #include <stdio.h> #include <math.h> #include <gsl/gsl_integration.h>

those link the header files. however, when compiling, gcc would complain that they can\'t see functions such as sqrt, abs, etc, from the math library. it also complains about my call to a function from the gsl library. i googled the math library stuff and realized i need to pass the \'-lm\' argument in gcc for the math library. similarly, i saw somewhere that refers to the \'-gsl\' argument. however, this did not work for me. i tried many things. some more googling, i found [this](http://ubuntuforums.org/showthread.php?t=270924) thread that shows the \'pkg-config\' command that shows exactly what arguments u need for which library.

So, i get:

$ pkg-config --libs gsl
-lgsl -lgslcblas -lm

so i just need to execute:

gcc -lgsl -lgslcblas -lm myprogram.c

i really don't understand how the GNU compilers work. on my mac, i don't have to pass those arguments. on my debian server, i need to. at first i thought that including the header was enough. will need to learn more about this stuff.

PS I also remember using this command when i had a hard time compiling shell-fm on my mac os x.

UPDATE: so on my mac os x, after installing gsl, i need to use 'gcc -lgsl myprog.c' to use the gsl library. things are fine now. I also want to note that i could not compile GSL form source on my debian server…had to rely on apt-get.