C++ ld:找不到架构 x86_64 的符号,clang:链接器命令失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32425361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ld: symbols not found for architecture x86_64, clang: linker command failed
提问by bzcheeseman
I'm trying to use homebrew to download and build packages like boost, ceres-solver, stuff like that. What happens is that I will try and compile code, without any special flags (g++ foo.cpp -o foo -I /usr/local/...
and I've tried clang++ too) and I get this error consistently:
我正在尝试使用自制软件来下载和构建诸如 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)
So I looked around and the solution is to use the -stdlib=libstdc++
flag. I tried it, and now it gives me errors related to the fact that apple ships an old version of the libstdc++
, doesn't get fixed by invoking -std=c++11
or -std=c++14
. It throws up objections to C++11 syntax like the shared_ptr:
所以我环顾四周,解决方案是使用-stdlib=libstdc++
标志。我试过了,现在它给了我一些错误,因为苹果发布了一个旧版本的libstdc++
, 不能通过调用-std=c++11
或来修复-std=c++14
。它对像 shared_ptr 这样的 C++11 语法提出了反对意见:
/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.
I'd rather not edit the library source code if I don't have to, I'd hope the devs have done a pretty good job?
如果不需要,我宁愿不编辑库源代码,我希望开发人员做得很好?
Is there a way to build libraries (with or without homebrew) that will give the correct linking? Currently I just brew install <package>
am I missing something obvious? Or am I screwing something up when I compile the code itself?
有没有办法构建可以提供正确链接的库(有或没有自制软件)?目前我只是brew install <package>
缺少一些明显的东西?或者我在编译代码时搞砸了什么?
I'm on Mac OS X 10.10.5, and brew --config gives this:
我在 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
Running $arch
gives: i386
跑步$arch
给出:i386
and $clang++ -v
gives:
并$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
I should add that I've tried cleaning out my computer in case there was an old version or something mucking things up. Everything I've seen says this error is caused by improper linking at build time, but it seems like brew does most of that properly, at least as far as I can tell? Everything installs and builds fine.
我应该补充一点,我已经尝试清理我的计算机,以防万一有旧版本或某些东西搞砸了。我所看到的一切都说这个错误是由构建时的不正确链接引起的,但似乎 brew 正确地完成了大部分工作,至少据我所知?一切都安装和构建良好。
I've installed gcc using brew and tried to compile the library using the g++-5 and the gcc-5 command to avoid clang and I get the same problem when I try to pass the -std=c++11
flag at compile time - g++-5 foo.cpp -o foo -I /usr/local/bar -std=c++11
. None of these work.
我已经使用 brew 安装了 gcc 并尝试使用 g++-5 和 gcc-5 命令编译库以避免 clang 并且当我尝试-std=c++11
在编译时传递标志时遇到同样的问题- g++-5 foo.cpp -o foo -I /usr/local/bar -std=c++11
。这些都不起作用。
I should also emphasize that these happen when I try to compile and run the example scripts that come with, for example, boost, not even my own scripts. I'm just sort of hoping that the libraries are at least mostly correct.
我还应该强调,当我尝试编译和运行随附的示例脚本(例如,boost,甚至不是我自己的脚本)时,会发生这些情况。我只是希望图书馆至少大部分是正确的。
I've also looked at a lot of similar questions here, many are unanswered and the ones that have solutions, well, I've tried many of the solutions and they don't help either. I've tried -lstdc++.6
, I've tried -stdlib=libstdc++
. The -l doesn't change anything, the -stdlib
causes the issues I described above.
我也在这里查看了很多类似的问题,很多都没有答案,还有那些有解决方案的,嗯,我已经尝试了很多解决方案,但它们也无济于事。我试过了-lstdc++.6
,我试过了-stdlib=libstdc++
。-l 不会改变任何东西,-stdlib
导致我上面描述的问题。
Any help I can get would be great, I've been working on this for weeks now and it's driving me nuts.
我能得到的任何帮助都会很棒,我已经为此工作了几个星期,这让我发疯了。
Quick Edit for clarity:
为清晰起见,快速编辑:
So when I input:
所以当我输入:
$g++ cerestest.cpp -o ceres -I /usr/local/include/eigen3
or
或者
$clang++ cerestest.cpp -o ceres -I /usr/local/include/eigen3
I get:
我得到:
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)
And this is not just ceres, similar errors occur for boost also.
这不仅是 ceres,boost 也会发生类似的错误。
回答by bzcheeseman
Thanks to Mark Setchell:
感谢马克·塞切尔:
Keeping an eye on /usr/local/lib
is important when using homebrew, the linking libraries need to be specified.
/usr/local/lib
使用自制软件时注意很重要,需要指定链接库。
The final code that worked was this:
最终有效的代码是这样的:
g++ cerestest.cpp -o ceres -lglog -lceres -I /usr/local/include
The same answer seems to work for the boost libraries, as well as all the others I've tried (Qt included, so far).
相同的答案似乎适用于 boost 库,以及我尝试过的所有其他库(到目前为止,包括 Qt)。