在运行Flutter Linux应用时出现“/usr/bin/ld: cannot find -lstdc++: No such file or directory”的错误提示。

22
在 Linux 桌面应用程序上运行 flutter run 时出现此错误。
Running "flutter pub get" in proj...            5.3s
Launching lib/main.dart on Linux in debug mode...
CMake Error at /usr/share/cmake-3.22/Modules/CMakeTestCXXCompiler.cmake:62 (message):
  The C++ compiler

    "/usr/bin/clang++"

  is not able to compile a simple test program.

  It fails with the following output:

    Change Dir: /media/kingbob/Dvolve/EData/proj/build/linux/x64/debug/CMakeFiles/CMakeTmp

    Run Build Command(s):/usr/bin/ninja cmTC_5f1b6 && [1/2] Building CXX object CMakeFiles/cmTC_5f1b6.dir/testCXXCompiler.cxx.o
    [2/2] Linking CXX executable cmTC_5f1b6
    FAILED: cmTC_5f1b6 
    : && /usr/bin/clang++   CMakeFiles/cmTC_5f1b6.dir/testCXXCompiler.cxx.o -o cmTC_5f1b6   && :
    /usr/bin/ld: cannot find -lstdc++: No such file or directory
    clang: error: linker command failed with exit code 1 (use -v to see invocation)
    ninja: build stopped: subcommand failed.





  CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
  CMakeLists.txt:2 (project)


Building Linux application...                                           
Exception: Unable to generate build files


flutter doctor 的输出结果
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.3.8, on Ubuntu 22.04.1 LTS 5.15.0-53-generic, locale en_IN)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
[✓] Chrome - develop for the web
[✓] Linux toolchain - develop for Linux desktop
[✓] Android Studio (version 2021.3)
[✓] Connected device (2 available)
[✓] HTTP Host Availability

• No issues found!

clang++ --version 的输出

Ubuntu clang version 14.0.0-1ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

我注意到缺少了 /usr/lib/libstdc++.so,因此手动创建了符号链接 sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6 /usr/lib/libstdc++.so

然后在执行 flutter run 时遇到了这个错误。

Launching lib/main.dart on Linux in debug mode...
/usr/include/glib-2.0/glib/glib-typeof.h:39:10: fatal error: 'type_traits' file not found
/usr/include/glib-2.0/glib/glib-typeof.h:39:10: fatal error: 'type_traits' file not found
/usr/include/glib-2.0/glib/glib-typeof.h:39:10: fatal error: 'type_traits' file not found
/usr/include/glib-2.0/glib/glib-typeof.h:39:10: fatal error: 'type_traits' file not found
/usr/include/glib-2.0/glib/glib-typeof.h:39:10: fatal error: 'type_traits' file not found
/usr/include/glib-2.0/glib/glib-typeof.h:39:10: fatal error: 'type_traits' file not found
3个回答

61

在系统升级后,我遇到了类似的问题。过了一段时间,我意识到这是因为 nvidia-driver-520-open 安装了 gcc-12。我使用的是通过脚本 installClang15.sh 安装的 clang-15clang-15 依赖并安装了 libstdc++-11-devgcc-11。问题在于,一旦安装了 gcc-12clang++ 会自动选择它而不是 gcc-11。可以通过运行 clang++ --verbose 来确认这一点,输出内容类似于:

Ubuntu clang version 15.0.6
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/local/bin
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/11
Found candidate GCC installation: /usr/lib/gcc/x86_64-linux-gnu/12
Selected GCC installation: /usr/lib/gcc/x86_64-linux-gnu/12
Candidate multilib: .;@m64
Selected multilib: .;@m64

解决方案1:安装libstdc++-12-dev

我是通过这个 GitHub 评论想到了这个解决方案。这是最简单的解决方案,但它有一些缺点。在 Ubuntu 上,您可以使用以下命令安装该软件包:

sudo apt install libstdc++-12-dev

我所面临的缺点包括:
  • 我将使用更新版本的gcc(版本12)。这可能需要(实际上确实需要)我更新我的代码。在这个问题的背景下,这非常棘手,因为并不一定能控制代码。
  • 如果在6个月内,nvidia-driver-520-open需要gcc-13,我将不得不再次找出为什么会有这个错误,然后安装libstdc++-13-dev,这可能又需要进行一些代码更新。

解决方案2:修复clang++使用的gcc安装

我没有找到任何干净简单的方法来做到这一点,但是有方法可以实现。截至2022年11月28日,尚未发布的clang-16构建clang时可用,并具有一个新选项--gcc-install-dir,在这里描述。这样可以修复编译问题:

$HOME/src/llvm-project/staging/bin/clang++ --gcc-install-dir=/usr/lib/gcc/x86_64-linux-gnu/11 a.cpp

其中a.cpp是一个简单的C++“Hello World!”程序。

#include <iostream>
int main() { std::cout << "Hello World!" << std::endl; }

旧版的clang有一个选项--gcc-toolchain,但由于在LLVM讨论区中所述的原因,它并不是很有用。然而,你仍然可以像这个Stack Overflow答案所建议的那样聪明地去做。简而言之,就是
sudo mkdir --parents /opt/gcc-root/lib/gcc/x86_64-linux-gnu
sudo ln --symbolic /usr/lib/gcc/x86_64-linux-gnu/11 /opt/gcc-root/lib/gcc/x86_64-linux-gnu/11
sudo ln --symbolic /usr/include /opt/gcc-root/

重要的是使用ln --symbolic /usr/include /opt/gcc-root/,否则我会遇到增量构建问题。然后,您应该能够编译:

clang++ --gcc-toolchain=/opt/gcc-root a.cpp

这里还有一个问题需要解决:“如何将此编译器选项整合到整个系统中?”我在这里没有找到令人满意的答案。
  • You could use export CCC_OVERRIDE_OPTIONS=^--gcc-toolchain=/opt/gcc-root as described here and documented on GitHub. This might be the best solution within the context of this question.
  • You could create a shell script named clang++ that contains
    #!/usr/bin/env bash
    /usr/local/bin/clang++ --gcc-toolchain=/opt/gcc-root "$@"
    
    and then use export CXX=<PATH_TO_SCRIPT>/clang++. This work well enough in practice: it's been my quick and dirty fix. However, I'm worried that other tools in the toolchain (like clang-tidy or clangd) might have problems if they're not passed the option --gcc-toolchain. Depending on how you are setup, they might not see the option since it would not be in the file compile_commands.json created with the CMAKE_EXPORT_COMPILE_COMMANDS CMake variable.
  • You could use clang's configuration files. The TL;DR is that you symbolically link clang++ as, for instance, fixed-gcc-root-clang++, and then you create a file fixed-gcc-root-clang++.cfg in the proper place (for clang-15, it is /usr/lib/llvm-15/bin) that contains the option --gcc-toolchain=/opt/gcc-root. Finally, you use export CXX=fixed-gcc-root-clang++. A problem is that if somebody or something (for instance an IDE) uses clang++ directly, your configuration file won't be read. You'll also have problems if a new symbolic link to fixed-gcc-root-clang++ is created, for instance c++ -> fixed-gcc-root-clang++, since in this case, the configuration file would not be read.
  • The best solution I found requires to control the build system, so it doesn't apply within the context of this question. The solution is to modify your build system toolchain. I find this unsatisfying since a packaging problem (remember, this is all because nvidia-driver-520-open installed a new transitive dependency) finds its solution in the C++ build system. Hence, the separation of concerns principle is not respected.

太棒了!我从来没有想到我的NVIDIA驱动程序可能是罪魁祸首。由于多年来我遇到的所有问题,我真的怀疑是否还要安装NVIDIA驱动程序。 - jari
2
sudo apt install libstdc++-12-dev 对我有效。 - Mattia Dinosaur

10

请安装以下内容:

重新安装所有依赖项(它已经包含在这里了)

sudo apt-get install clang cmake ninja-build pkg-config libgtk-3-dev liblzma-dev libstdc++-12-dev

您可以在此处阅读最近的两个条目:https://github.com/flutter/flutter/issues/115909

3
sudo apt install libstdc++-12-dev 安装解决了我的问题。 - Marsel Arduanov

2

原来,升级到 clang-14 导致了这个问题。以下是我解决它的方法:

  • 降级到 clang-13
  • 如果 /usr/lib/libstdc++.so 不存在,则创建符号链接。 sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6 /usr/lib/libstdc++.so
  • 如果在 flutter run 中遇到了 fatal error: 'type_traits' file not found,请导出 clang 包含路径 export CPLUS_INCLUDE_PATH="${CPLUS_INCLUDE_PATH:+${CPLUS_INCLUDE_PATH}:}/usr/lib/llvm-13/include/c++/v1/"

你能提供回退到13的代码部分吗? 我无法构建任何东西。我不是专家。 最好只说,“运行以下命令”。 - Bhikkhu Subhuti
安装clang-13 `sudo apt install clang-13' 将/usr/bin/clang链接到/usr/bin/clang-13 将/usr/bin/clang++链接到/usr/bin/clang++-13 这样就可以了 - legacyO7
我还注意到,“致命错误:未找到'type_traits'文件”可以通过安装g++-10来解决。现在我已经将系统升级到Ubuntu 22.10,一切都正常工作,无需任何调整! - legacyO7
1
我该怎么办?还安装了llvm 14。两者都没有C++目录。 我想知道为什么他们允许这种情况发生。Flutter doctor应该更聪明一些。世界上可能只有5个人在编译Linux桌面。 $ ls /usr/lib/llvm-13/include/c++/v1/ ls:无法访问'/usr/lib/llvm-13/include/c++/v1/':没有那个文件或目录。 - Bhikkhu Subhuti

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接