GSL示例代码的未定义符号编译错误

时间:2020-02-12 12:37:00

标签: c++ compiler-errors homebrew gsl

我正在尝试编译此代码

#include <stdio.h>
#include <gsl/gsl_rng.h>

gsl_rng * r;  /* global generator */

int
main (void)
{
  const gsl_rng_type * T;

  gsl_rng_env_setup();

  T = gsl_rng_default;
  r = gsl_rng_alloc (T);

  printf ("generator type: %s\n", gsl_rng_name (r));
  printf ("seed = %lu\n", gsl_rng_default_seed);
  printf ("first value = %lu\n", gsl_rng_get (r));

  gsl_rng_free (r);
  return 0;
}

找到了here on gnu.org。代码无法在我的机器上编译

$g++ test.cpp -o test
Undefined symbols for architecture x86_64:
  "_gsl_rng_alloc", referenced from:
      _main in test-03eafc.o
  "_gsl_rng_default", referenced from:
      _main in test-03eafc.o
  "_gsl_rng_default_seed", referenced from:
      _main in test-03eafc.o
  "_gsl_rng_env_setup", referenced from:
      _main in test-03eafc.o
  "_gsl_rng_free", referenced from:
      _main in test-03eafc.o
  "_gsl_rng_get", referenced from:
      _main in test-03eafc.o
  "_gsl_rng_name", referenced from:
      _main in test-03eafc.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

我尝试重新安装gsl

$ brew reinstall gsl
==> Reinstalling gsl 
==> Downloading https://homebrew.bintray.com/bottles/gsl-2.6.mojave.bottle.tar.g
Already downloaded: /Users/remi/Library/Caches/Homebrew/downloads/2c7ec6b02a46e1f03b0ea0899e0a0594a6a2cceb17c9d6f6589d2f5d674d7f11--gsl-2.6.mojave.bottle.tar.gz
==> Pouring gsl-2.6.mojave.bottle.tar.gz
?  /usr/local/Cellar/gsl/2.6: 289 files, 9.8MB
==> `brew cleanup` has not been run in 30 days, running now...
Removing: /Users/remi/Library/Caches/Homebrew/gsl--2.6.mojave.bottle.tar.gz... (2.8MB)
Removing: /Users/remi/Library/Logs/Homebrew/coreutils... (64B)

我做到了

$ brew unlink gsl && brew link gsl
Unlinking /usr/local/Cellar/gsl/2.6... 17 symlinks removed
Linking /usr/local/Cellar/gsl/2.6... 17 symlinks created

但是没有帮助。这是我的gcc(默认为Clang)版本

$ g++ --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

您能帮我吗?

0 个答案:

没有答案