我有一个小的 C++ 项目,在将 Xubuntu 升级到 15.04 之前,它编译得很好。当我尝试使用 cmake 重建它时,输出中间出现了以下消息:
/usr/bin/ld: cannot find -lstdc++
因此,我尝试编写一个简单的程序来看看是否cmake
对此负责:
int main() {
return 0;
}
有趣的事情:g++
编译:
$ g++ main.cpp
$ ls
a.out main.cpp
$ ./a.out
$
但当我尝试使用clang++
...
$ clang++ main.cpp
/usr/bin/ld: cannot find -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)
$
尝试clang++ main.cpp -lc++
(按照建议这里)或使用-static
标志(这里) 给出了完全相同的错误。按照其本身-v
的建议运行clang
,我得到了以下信息:
$ clang++ main.cpp -v
Ubuntu clang version 3.6.0-2ubuntu1 (tags/RELEASE_360/final) (based on LLVM 3.6.0)
Target: x86_64-pc-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/4.9.2
Found candidate GCC installation: /usr/bin/../lib/gcc/i686-linux-gnu/5.0.1
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.7
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.7.4
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/4.9.2
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/4.9.2
Found candidate GCC installation: /usr/lib/gcc/i686-linux-gnu/5.0.1
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.7.4
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/4.9.2
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/5.0.1
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1
Candidate multilib: .;@m64
Candidate multilib: 32;@m32
Candidate multilib: x32;@mx32
Selected multilib: .;@m64
"/usr/lib/llvm-3.6/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -main-file-name main.cpp -mrelocation-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -target-linker-version 2.25 -v -dwarf-column-info -resource-dir /usr/lib/llvm-3.6/bin/../lib/clang/3.6.0 -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/../../../../include/c++ -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/../../../../include/c++/x86_64-linux-gnu -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/../../../../include/c++/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-3.6/bin/../lib/clang/3.6.0/include -internal-externc-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdeprecated-macro -fdebug-compilation-dir /home/ryu/Dropbox/Aperture/working/C/learn-sdl/src -ferror-limit 19 -fmessage-length 100 -mstackrealign -fobjc-runtime=gcc -fcxx-exceptions -fexceptions -fdiagnostics-show-option -fcolor-diagnostics -o /tmp/main-efcd57.o -x c++ main.cpp
clang -cc1 version 3.6.0 based upon LLVM 3.6.0 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/../../../../include/c++/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/../../../../include/c++/backward"
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/../../../../include/c++
/usr/local/include
/usr/lib/llvm-3.6/bin/../lib/clang/3.6.0/include
/usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/include
/usr/include/x86_64-linux-gnu
/usr/include
End of search list.
"/usr/bin/ld" --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o a.out /usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/../../../x86_64-linux-gnu/crt1.o /usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/../../../x86_64-linux-gnu/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/crtbegin.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/../../../x86_64-linux-gnu -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/../../.. -L/usr/lib/llvm-3.6/bin/../lib -L/lib -L/usr/lib /tmp/main-efcd57.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/crtend.o /usr/bin/../lib/gcc/x86_64-linux-gnu/5.0.1/../../../x86_64-linux-gnu/crtn.o
/usr/bin/ld: cannot find -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)
$
我找不到错误的根源。
我尝试清除并重新安装build-essential
、clang
和g++
,但没有成功。我也在谷歌上搜索过,但只找到此链接从 2013 年开始(是的,我注意到这是针对 Red Hat 的)。
所以,我的问题是:
我该如何解决? (显然,第三次/第四次重新安装可以解决这个问题。)
我所做的唯一一件事就是升级系统,为什么会发生这种情况?
答案1
这很奇怪,但是删除gcc-go
数据包对我有帮助。
答案2
检查是否正确链接了 stdc++。例如,尝试使用 g++ 进行构建,并使用 ldd 检查库的位置。然后检查是否有完整的链接层次结构,例如(对于 stdc++):
lrwxrwxrwx 1 root root 40 cze 14 12:34 /usr/lib/x86_64-linux-gnu/libstdc++.so -> libstdc++.so.6
lrwxrwxrwx 1 root root 19 maj 11 02:36 /usr/lib/x86_64-linux-gnu/libstdc++.so.6 -> libstdc++.so.6.0.22
-rw-r--r-- 1 root root 1,6M maj 11 03:05 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.22
在 Ubuntu 上有时会缺失(可能您首先没有这个)。
答案3
我不知道为什么会出现这种情况(我使用的是全新安装的 Linux Mint,但出现了该错误)但是我做了这个肮脏的修复。
在我的.bashrc中:
stdcxxpath() { if [ -n "${stdcxxpath:-}" ]; then echo "$stdcxxpath"; return; fi; stdcxxpath=$(g++ -xc++ /dev/null -Wl,--print-map 2>&1 |awk '/libstdc\+\+.so/ { print $2; exit }'|sed 's|/[^/]*$||'); stdcxxpath; }
alias clang++='clang++ -L$(stdcxxpath)' #find where libstdc++.so is from gcc and use it
记忆化的 stdcxxpath() shell 函数找到 libstdc++ 并将其传递给使用的链接器clang++
(更简单:dirname $(locate '*/libstdc++.so')
虽然有点滞后,但也能工作。