clang 找不到 libstdc++,但 libc++ 可以工作

问题描述 投票:0回答:1
  • 操作系统:ubuntu22.04
  • 目标:在 neovim 中将 LSP 与 clangd 一起使用
  • 问题:无法找到“iostream” ,我生成了一个compile_commands.json,但是也没用。
$ cat compile_commands.json
[
{
  "directory": "/home/stoair/cPlusPlus/c++/learn/testLSP/cmake",
  "command": "/usr/bin/cc -DSOME_DEFINITION   -o CMakeFiles/my_app.dir/CMakeFiles/3.22.1/CompilerIdC/CMakeCCompilerId.c.o -c /home/stoair/cPlusPlus/c++/learn/testLSP/cmake/CMakeFiles/3.22.1/CompilerIdC/CMakeCCompilerId.c",
  "file": "/home/stoair/cPlusPlus/c++/learn/testLSP/cmake/CMakeFiles/3.22.1/CompilerIdC/CMakeCCompilerId.c"
},
{
  "directory": "/home/stoair/cPlusPlus/c++/learn/testLSP/cmake",
  "command": "/usr/bin/c++ -DSOME_DEFINITION  -std=gnu++17 -o CMakeFiles/my_app.dir/CMakeFiles/3.22.1/CompilerIdCXX/CMakeCXXCompilerId.cpp.o -c /home/stoair/cPlusPlus/c++/learn/testLSP/cmake/CMakeFiles/3.22.1/CompilerIdCXX/CMakeCXXCompilerId.cpp",
  "file": "/home/stoair/cPlusPlus/c++/learn/testLSP/cmake/CMakeFiles/3.22.1/CompilerIdCXX/CMakeCXXCompilerId.cpp"
},
{
  "directory": "/home/stoair/cPlusPlus/c++/learn/testLSP/cmake",
  "command": "/usr/bin/cc -DSOME_DEFINITION   -o CMakeFiles/my_app.dir/main.c.o -c /home/stoair/cPlusPlus/c++/learn/testLSP/main.c",
  "file": "/home/stoair/cPlusPlus/c++/learn/testLSP/main.c"
},
{
  "directory": "/home/stoair/cPlusPlus/c++/learn/testLSP/cmake",
  "command": "/usr/bin/c++ -DSOME_DEFINITION  -std=gnu++17 -o CMakeFiles/my_app.dir/main.cpp.o -c /home/stoair/cPlusPlus/c++/learn/testLSP/main.cpp",
  "file": "/home/stoair/cPlusPlus/c++/learn/testLSP/main.cpp"
}

但是我发现clangd即使没有compile_commans.json也能找到c头文件

我想测试 clang 是否正常工作。 我发现 clang 找不到 libstdc++,但 libc++ 可以工作

$ clang++ -o main main.cpp 
main.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
         ^~~~~~~~~~
1 error generated.
$ clang++ -o main main.cpp -stdlib=libstdc++ 
main.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
         ^~~~~~~~~~
1 error generated.
$ clang++ -o main main.cpp -stdlib=libc++
$ ./main
hello world!

详情:

$ clang -v
Ubuntu clang version 14.0.0-1ubuntu1.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64
Found CUDA installation: /usr/local/cuda-12.1, version
$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 11.4.0-1ubuntu1~22.04' --with-bugurl=file:///usr/share/doc/gcc-11/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-11 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-11-XeT9lY/gcc-11-11.4.0/debian/tmp-gcn/usr --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-serialization=2
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.4.0 (Ubuntu 11.4.0-1ubuntu1~22.04)
$ clang++ -o main main.cpp -stdlib=libstdc++ -v
Ubuntu clang version 14.0.0-1ubuntu1.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64
Found CUDA installation: /usr/local/cuda-12.1, version 
 "/usr/lib/llvm-14/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -v -fcoverage-compilation-dir=/home/stoair/cPlusPlus/c++/learn/testLSP -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -I/usr/local/cuda-12.1/targets/x86_64-linux/lib -I/usr/local/cuda-12.1/targets/x86_64-linux/include -I. -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++ -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/x86_64-linux-gnu -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/backward -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/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/stoair/cPlusPlus/c++/learn/testLSP -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/main-93e0e4.o -x c++ main.cpp
clang -cc1 version 14.0.0 based upon LLVM 14.0.0 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++/backward"
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/cuda-12.1/targets/x86_64-linux/lib
 /usr/local/cuda-12.1/targets/x86_64-linux/include
 .
 /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../include/c++
 /usr/lib/llvm-14/lib/clang/14.0.0/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
main.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
         ^~~~~~~~~~
1 error generated.
$ clang++ -o main main.cpp -stdlib=libc++ -v
Ubuntu clang version 14.0.0-1ubuntu1.1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/bin/../lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64
Found CUDA installation: /usr/local/cuda-12.1, version 
 "/usr/lib/llvm-14/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name main.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -mllvm -treat-scalable-fixed-error-as-warning -debugger-tuning=gdb -v -fcoverage-compilation-dir=/home/stoair/cPlusPlus/c++/learn/testLSP -resource-dir /usr/lib/llvm-14/lib/clang/14.0.0 -I/usr/local/cuda-12.1/targets/x86_64-linux/lib -I/usr/local/cuda-12.1/targets/x86_64-linux/include -I. -internal-isystem /usr/lib/llvm-14/bin/../include/c++/v1 -internal-isystem /usr/lib/llvm-14/lib/clang/14.0.0/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/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/stoair/cPlusPlus/c++/learn/testLSP -ferror-limit 19 -fgnuc-version=4.2.1 -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/main-077ebf.o -x c++ main.cpp
clang -cc1 version 14.0.0 based upon LLVM 14.0.0 default target x86_64-pc-linux-gnu
ignoring nonexistent directory "/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../x86_64-linux-gnu/include"
ignoring nonexistent directory "/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/cuda-12.1/targets/x86_64-linux/lib
 /usr/local/cuda-12.1/targets/x86_64-linux/include
 .
 /usr/lib/llvm-14/bin/../include/c++/v1
 /usr/lib/llvm-14/lib/clang/14.0.0/include
 /usr/local/include
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
 "/usr/bin/ld" -pie --hash-style=both --build-id --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o main /lib/x86_64-linux-gnu/Scrt1.o /lib/x86_64-linux-gnu/crti.o /usr/bin/../lib/gcc/x86_64-linux-gnu/12/crtbeginS.o -L/usr/bin/../lib/gcc/x86_64-linux-gnu/12 -L/usr/bin/../lib/gcc/x86_64-linux-gnu/12/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/usr/lib/llvm-14/bin/../lib -L/lib -L/usr/lib /tmp/main-077ebf.o -lc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/bin/../lib/gcc/x86_64-linux-gnu/12/crtendS.o /lib/x86_64-linux-gnu/crtn.o
ubuntu clang neovim clang++ clangd
1个回答
0
投票

感谢HolyBlackCat的回答。 我已经安装了gcc-11,gcc-12和g++-11,我不知道为什么gcc和g++的版本会导致问题。但这确实解决了我的问题。

$ sudo apt remove gcc-12
$ sudo apt install g++-12

Clangd 也很好用。

© www.soinside.com 2019 - 2024. All rights reserved.