ld:找不到体系结构 x86_64 的符号,clang:链接器命令失败

ld: symbols not found for architecture x86_64, clang: linker command failed

我正在尝试使用自制软件下载和构建诸如 boost、ceres-solver 之类的软件包。发生的事情是我将尝试编译代码,没有任何特殊标志(g++ foo.cpp -o foo -I /usr/local/... 并且我也尝试过 clang++)并且我始终收到此错误:

Undefined symbols for architecture x86_64:
  ...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

所以我环顾四周,解决方案是使用 -stdlib=libstdc++ 标志。我试过了,现在它给了我与以下事实相关的错误:苹果发布了旧版本的 libstdc++,无法通过调用 -std=c++11-std=c++14 得到修复。它引发了对 C++11 语法的反对,例如 shared_ptr:

/usr/local/include/ceres/internal/port.h:62:12: error: no member named
      'shared_ptr' in namespace 'std'
using std::shared_ptr;
      ~~~~~^
...
/usr/local/include/ceres/solver.h:629:15: error: expected member name or ';'
      after declaration specifiers
    shared_ptr<ParameterBlockOrdering> inner_iteration_ordering;
    ~~~~~~~~~~^
5 errors generated.

如果没有必要,我宁愿不编辑库源代码,我希望开发人员做得很好吗?

有没有一种方法可以构建提供正确链接的库(有或没有自制软件)?目前我只是 brew install <package> 我错过了一些明显的东西吗?还是我自己编译代码时搞砸了?

我在 Mac OS X 10.10.5 上,brew --config 给出了这个:

HOMEBREW_VERSION: 0.9.5
ORIGIN: https://github.com/Homebrew/homebrew
HEAD: 03ad27453de01adc29cbf941bd29a2dfb54a9960
Last commit: 69 minutes ago
HOMEBREW_PREFIX: /usr/local
HOMEBREW_REPOSITORY: /usr/local
HOMEBREW_CELLAR: /usr/local/Cellar
HOMEBREW_BOTTLE_DOMAIN: https://homebrew.bintray.com
CPU: 8-core 64-bit ivybridge
OS X: 10.10.5-x86_64
Xcode: 6.4
CLT: 6.4.0.0.1.1435007323
Clang: 6.1 build 602
X11: N/A
System Ruby: 2.0.0-p481
Perl: /usr/bin/perl
Python: /usr/local/bin/python => /Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7
Ruby: /usr/bin/ruby
Java: N/A

运行 $arch 给出:i386

$clang++ -v给出:

Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.5.0
Thread model: posix

我应该补充一点,我已经尝试清理我的计算机,以防出现旧版本或其他问题。我所看到的一切都说这个错误是由构建时不正确的链接引起的,但似乎 brew 正确地做了大部分,至少据我所知?一切安装和构建都很好。

我已经使用 brew 安装了 gcc,并尝试使用 g++-5 和 gcc-5 命令编译库以避免 clang,当我尝试传递 -std=c++11 标志时我遇到了同样的问题在编译时 - g++-5 foo.cpp -o foo -I /usr/local/bar -std=c++11。 None 这些作品。

我还应该强调,当我尝试编译和 运行 附带的示例脚本时,例如 boost,甚至我自己的脚本都不会发生这些情况。我只是希望图书馆至少大部分是正确的。

我在这里也看了很多类似的问题,很多都没有答案,还有一些有解决方案,好吧,我已经尝试了很多解决方案,但它们也没有帮助。我试过-lstdc++.6,我试过-stdlib=libstdc++。 -l 不会改变任何东西,-stdlib 会导致我上面描述的问题。

如果我能得到任何帮助,那将是非常好的,我已经为此工作了数周,这让我抓狂。

为清楚起见快速编辑:

所以当我输入:

$g++ cerestest.cpp -o ceres -I /usr/local/include/eigen3

$clang++ cerestest.cpp -o ceres -I /usr/local/include/eigen3

我得到:

Undefined symbols for architecture x86_64:
  "ceres::Solve(ceres::Solver::Options const&, ceres::Problem*, ceres::Solver::Summary*)", referenced from:
      _main in cerestest-ef733e.o
  "ceres::Solver::Summary::Summary()", referenced from:
      _main in cerestest-ef733e.o
  "ceres::Problem::AddResidualBlock(ceres::CostFunction*, ceres::LossFunction*, double*)", referenced from:
      _main in cerestest-ef733e.o
  "ceres::Problem::Problem()", referenced from:
      _main in cerestest-ef733e.o
  "ceres::Problem::~Problem()", referenced from:
      _main in cerestest-ef733e.o
  "google::LogMessage::stream()", referenced from:
      ceres::AutoDiffCostFunction<CostFunctor, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0>::AutoDiffCostFunction(CostFunctor*) in cerestest-ef733e.o
      ceres::internal::AutoDiff<CostFunctor, double, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0>::Differentiate(CostFunctor const&, double const* const*, int, double*, double**) in cerestest-ef733e.o
      void ceres::internal::Make1stOrderPerturbation<ceres::Jet<double, 1>, double, 1>(int, double const*, ceres::Jet<double, 1>*) in cerestest-ef733e.o
      void ceres::internal::Take0thOrderPart<ceres::Jet<double, 1>, double*>(int, ceres::Jet<double, 1> const*, double*) in cerestest-ef733e.o
      void ceres::internal::Take1stOrderPart<ceres::Jet<double, 1>, double, 0, 1>(int, ceres::Jet<double, 1> const*, double*) in cerestest-ef733e.o
  "google::LogMessageFatal::LogMessageFatal(char const*, int)", referenced from:
      void ceres::internal::Make1stOrderPerturbation<ceres::Jet<double, 1>, double, 1>(int, double const*, ceres::Jet<double, 1>*) in cerestest-ef733e.o
      void ceres::internal::Take0thOrderPart<ceres::Jet<double, 1>, double*>(int, ceres::Jet<double, 1> const*, double*) in cerestest-ef733e.o
      void ceres::internal::Take1stOrderPart<ceres::Jet<double, 1>, double, 0, 1>(int, ceres::Jet<double, 1> const*, double*) in cerestest-ef733e.o
  "google::LogMessageFatal::LogMessageFatal(char const*, int, google::CheckOpString const&)", referenced from:
      ceres::AutoDiffCostFunction<CostFunctor, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0>::AutoDiffCostFunction(CostFunctor*) in cerestest-ef733e.o
      ceres::internal::AutoDiff<CostFunctor, double, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0>::Differentiate(CostFunctor const&, double const* const*, int, double*, double**) in cerestest-ef733e.o
  "google::LogMessageFatal::~LogMessageFatal()", referenced from:
      ceres::AutoDiffCostFunction<CostFunctor, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0>::AutoDiffCostFunction(CostFunctor*) in cerestest-ef733e.o
      ceres::internal::AutoDiff<CostFunctor, double, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0>::Differentiate(CostFunctor const&, double const* const*, int, double*, double**) in cerestest-ef733e.o
      void ceres::internal::Make1stOrderPerturbation<ceres::Jet<double, 1>, double, 1>(int, double const*, ceres::Jet<double, 1>*) in cerestest-ef733e.o
      void ceres::internal::Take0thOrderPart<ceres::Jet<double, 1>, double*>(int, ceres::Jet<double, 1> const*, double*) in cerestest-ef733e.o
      void ceres::internal::Take1stOrderPart<ceres::Jet<double, 1>, double, 0, 1>(int, ceres::Jet<double, 1> const*, double*) in cerestest-ef733e.o
  "google::InitGoogleLogging(char const*)", referenced from:
      _main in cerestest-ef733e.o
  "google::base::CheckOpMessageBuilder::ForVar2()", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* google::MakeCheckOpString<int, int>(int const&, int const&, char const*) in cerestest-ef733e.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* google::MakeCheckOpString<int, ceres::DimensionType>(int const&, ceres::DimensionType const&, char const*) in cerestest-ef733e.o
  "google::base::CheckOpMessageBuilder::NewString()", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* google::MakeCheckOpString<int, int>(int const&, int const&, char const*) in cerestest-ef733e.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* google::MakeCheckOpString<int, ceres::DimensionType>(int const&, ceres::DimensionType const&, char const*) in cerestest-ef733e.o
  "google::base::CheckOpMessageBuilder::CheckOpMessageBuilder(char const*)", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* google::MakeCheckOpString<int, int>(int const&, int const&, char const*) in cerestest-ef733e.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* google::MakeCheckOpString<int, ceres::DimensionType>(int const&, ceres::DimensionType const&, char const*) in cerestest-ef733e.o
  "google::base::CheckOpMessageBuilder::~CheckOpMessageBuilder()", referenced from:
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* google::MakeCheckOpString<int, int>(int const&, int const&, char const*) in cerestest-ef733e.o
      std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >* google::MakeCheckOpString<int, ceres::DimensionType>(int const&, ceres::DimensionType const&, char const*) in cerestest-ef733e.o
  "ceres::Solver::Summary::BriefReport() const", referenced from:
      _main in cerestest-ef733e.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

这不仅仅是 ceres,boost 也会出现类似的错误。

感谢马克·塞切尔:

关注/usr/local/lib使用自制软件时很重要,需要指定链接库。

最终有效的代码是这样的:

g++ cerestest.cpp -o ceres -lglog -lceres -I /usr/local/include

同样的答案似乎适用于 boost 库,以及我尝试过的所有其他库(包括 Qt,到目前为止)。