无法在 macOS 上使用 GSL 库 - ld: 找不到架构 x86_64 的符号

ber*_*kie 4 c gcc gsl osx-yosemite

我已按照提供的安装 GSL-1.16 库的说明进行操作,并且我认为我已成功安装了该库。但是,当我尝试编译并运行网站(http://www.gnu.org/software/gsl/manual/html_node/An-Example-Program.html#An-Example-Program)中的示例程序时,我得到以下消息:

$ 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)
Run Code Online (Sandbox Code Playgroud)

操作系统为 macOS X Yosemite,gcc --version 的输出如下:

$ 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
Run Code Online (Sandbox Code Playgroud)

myr*_*dio 5

确实,正如@trojanfoe 和@bergercookie 所说,您必须编译文件,然后将其链接到库。如compilelink 中所述,对于该特定示例:

首先编译文件:

gcc -Wall -I/usr/local/include -c example.c -o example.o
Run Code Online (Sandbox Code Playgroud)

其次,将其链接到库:

 gcc -L/usr/local/lib example.o -lgsl -o example.e
Run Code Online (Sandbox Code Playgroud)

当然, /usr/local/lib 应该替换为您安装 gsl 的路径。

编辑:在 macOS 中,从优胜美地安装的默认位置是/opt/local/lib(和/opt/local/include