getting this error on flutter run
for a linux desktop application
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
output of 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!
output of clang++ --version
Ubuntu clang version 14.0.0-1ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
I noticed that /usr/lib/libstdc++.so
was missing, so manually created a symlink
sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6 /usr/lib/libstdc++.so
.
Then I ended up in this error on 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
I ran into a similar problem after a system upgrade. After a while, I realized that it was because
nvidia-driver-520-open
installedgcc-12
. I useclang-15
that I installed with the script installClang15.sh.clang-15
depends and hence installslibstdc++-11-dev
andgcc-11
. The problem is that oncegcc-12
was installed,clang++
picked it up automatically instead ofgcc-11
. This can be confirmed withclang++ --verbose
which outputs something likeSolution 1: Install
libstdc++-12-dev
I thought about this solution thanks to this GitHub comment. This is the easiest solution, but it has a few drawbacks. On Ubuntu, you can install the package with
The drawbacks that I faced are:
nvidia-driver-520-open
requiresgcc-13
? I would have to figure out again why I'm having this error, and then installlibstdc++-13-dev
, which might potentially require some code updates again.Solution 2: Fix the gcc installation that
clang++
usesI did not find any clean and easy way to do that, but there are ways.
clang-16
, not yet released as of November 28th 2022 but available when building clang, has a new option--gcc-install-dir
described here. This fixes compilation:where
a.cpp
is a trivial C++ "Hello World!" programOlder versions of
clang
have the option--gcc-toolchain
, which is not quite as useful for reasons described on llvm's discourse. However, you can still be ingenious and do what this Stack Overflow answer suggest. The TL;DR isThe
ln --symbolic /usr/include /opt/gcc-root/
is important, otherwise I encountered problems with incremental build. Then, you should be able to compile withThis leaves one question open: "How do I integrate this compiler option system wide?" I didn't find any satisfying answers here.
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.clang++
that contains and then useexport 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 (likeclang-tidy
orclangd
) 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 filecompile_commands.json
created with theCMAKE_EXPORT_COMPILE_COMMANDS
CMake variable.clang++
as, for instance,fixed-gcc-root-clang++
, and then you create a filefixed-gcc-root-clang++.cfg
in the proper place (forclang-15
, it is/usr/lib/llvm-15/bin
) that contains the option--gcc-toolchain=/opt/gcc-root
. Finally, you useexport CXX=fixed-gcc-root-clang++
. A problem is that if somebody or something (for instance an IDE) usesclang++
directly, your configuration file won't be read. You'll also have problems if a new symbolic link tofixed-gcc-root-clang++
is created, for instancec++ -> fixed-gcc-root-clang++
, since in this case, the configuration file would not be read.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.