c++cclangclang++meson-build

Meson and clang++ on Ubuntu 22.04.2 LTS; ld: cannot find -lstdc++


I've always been able to compile meson projects using clang and clang++ by running:

CC=clang CXX=clang++ meson setup clang-debug --buildtype=debug

The output I get is:

he Meson build system
Version: 1.1.0
Source dir: /home/maarten/github/psy-lib
Build dir: /home/maarten/github/psy-lib/clang-debug
Build type: native build
Project name: libpsy
Project version: 0.1
C compiler for the host machine: clang (clang 14.0.0-1ubuntu1 "Ubuntu clang version 14.0.0-1ubuntu1")
C linker for the host machine: clang ld.bfd 2.38

meson.build:1:0: ERROR: Compiler clang++ can not compile programs.

A full log can be found at /home/maarten/github/psy-lib/clang-debug/meson-logs/meson-log.txt

If I run:

meson setup debug --buildtype=debug

I get a working build, that uses cc and c++ to compile C and C++ respectively.

The relevant part from meson-log for the clang(++) build is:

-----
Detecting compiler via: clang++ --version
compiler returned <Popen: returncode: 0 args: ['clang++', '--version']>
compiler stdout:
Ubuntu clang version 14.0.0-1ubuntu1
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

compiler stderr:

Running command: clang++ -E -dM -
-----
Detecting linker via: clang++ -Wl,--version
linker returned <Popen: returncode: 0 args: ['clang++', '-Wl,--version']>
linker stdout:
GNU ld (GNU Binutils for Ubuntu) 2.38
Copyright (C) 2022 Free Software Foundation, Inc.
This program is free software; you may redistribute it under the terms of
the GNU General Public License version 3 or (at your option) a later version.
This program has absolutely no warranty.

linker stderr:

Sanity testing C++ compiler: clang++
Is cross compiler: False.
Sanity check compiler command line: clang++ sanitycheckcpp.cc -o sanitycheckcpp.exe -D_FILE_OFFSET_BITS=64
Sanity check compile stdout:

-----
Sanity check compile stderr:
/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)

-----

meson.build:1:0: ERROR: Compiler clang++ can not compile programs.

I think what meson tries to do is to compile the following program:

class breakCCompiler;int main(void) { return 0; }

The file (sanitycheckcpp.cc) is compiled as:

clang++ sanitycheckcpp.cc -o sanitycheckcpp.exe -D_FILE_OFFSET_BITS=64

However, it fails with the following error:

/usr/bin/ld: cannot find -lstdc++

I don't know what is going wrong. I would appreciate any help!


Solution

  • In short, clang++ was looking for another version of libstd++. For me the short and perhaps not optimal solution is to install another version of libstdc++

    sudo apt install libstdc++-12-dev
    

    also installing gcc version 12 might help.

    I found a similar post/answer: "/usr/bin/ld: cannot find -lstdc++: No such file or directory" on running flutter linux app

    So if you upvote this answer, make sure upvote the answer above, as that answer has more depth to it.