Search code examples
cgccosx-yosemitegsl

Cannot use GSL library on macOS - ld: symbol(s) not found for architecture x86_64


I have followed the instructions provided for installing the GSL-1.16 library and I think I have successfully installed the library. However when I try to compile & run the example program found in the website (http://www.gnu.org/software/gsl/manual/html_node/An-Example-Program.html#An-Example-Program) I get the following message:

$ gcc besel_exam.c
Undefined symbols for architecture x86_64:
  "_gsl_sf_bessel_J0", referenced from:
      _main in besel_exam-72d841.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

The operating system is macOS X Yosemite and the output of gcc --version is the following:

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin14.0.0
Thread model: posix

Solution

  • Indeed, as @trojanfoe and @bergercookie said you have to compile your file and then link it to the library. As explained in compile and link, for that particular example:

    First compile the file:

    gcc -Wall -I/usr/local/include -c example.c -o example.o
    

    second, link it to the library:

     gcc -L/usr/local/lib example.o -lgsl -o example.e
    

    where of course, /usr/local/lib should be replaced for the path where you have gsl installed.

    EDIT: in macOS, from Yosemite the default location for the installation is /opt/local/lib (and /opt/local/include)