c++ - ld : symbols not found for architecture x86_64, clang: 链接器命令失败

标签 c++ macos c++11 homebrew clang++

我正在尝试使用 Homebrew 软件下载和构建诸如 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.

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

有没有一种方法可以构建提供正确链接的库(有或没有 Homebrew 软件)?目前我只是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 .这些都不起作用。

我还应该强调,当我尝试编译和运行示例脚本时会发生这些情况,例如,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 也会出现类似的错误。

最佳答案

感谢 Mark Setchell:

在使用 homebrew 时,密切关注 /usr/local/lib 很重要,需要指定链接库。

最终的代码是这样的:

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

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

关于c++ - ld : symbols not found for architecture x86_64, clang: 链接器命令失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32425361/

相关文章:

c++ - 无法打开已编译的项目

c++ - 从 ' ' 类型的右值初始化 ' ' 类型的非常量引用无效

macos - 有人可以在 Xojo 2014 中展示已知的良好工作功能吗?

macos - 如何在编辑后自动将 "source"~/.bashrc?

C++ 跨边界使用 std::vector

c++ - 标准::原子 | compare_exchange_weak 与 compare_exchange_strong

c++ - 使用 constexpr yield 递减吗?

macos - Rsync 错误 : The source and destination cannot both be remote

c++ - C++11 中的 vector <字符> VS vector < bool >

c++ - 编译器是否应该忽略导致运行构造函数或析构函数的未使用变量?