# find\_library does not find stdc++fs

**URL:** https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764
**Category:** Usage
**Tags:** os:linux
**Created:** [January 2, 2022, 10:28pm UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764 "2022-01-02T22:28:44Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![jcelerier](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jcelerier/32/2071_2.png) [@jcelerier](https://discourse.cmake.org/u/jcelerier)
#### Post date: [January 2, 2022, 10:28pm UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/1 "2022-01-02T22:28:44Z")

</div>

I have the following:

```auto
$ find /usr -name '*stdc*'
/usr/lib/gcc/x86_64-redhat-linux/10/32/libstdc++.a
/usr/lib/gcc/x86_64-redhat-linux/10/32/libstdc++.so
/usr/lib/gcc/x86_64-redhat-linux/10/32/libstdc++fs.a
/usr/lib/gcc/x86_64-redhat-linux/10/libstdc++fs.a
/usr/lib/gcc/x86_64-redhat-linux/10/libstdc++.so
/usr/lib64/libstdc++.so.6
/usr/lib64/libstdc++.so.6.0.28

```

Yet:

```auto
find_library(STDCPPFS_LIBRARY NAMES stdc++fs)

```

tells me “not found”. What can I do ? I cannot hardcode paths as I want that code to work on any Linux distro. I cannot just do a blanket add of target\_link\_libraries(stdc++fs) because more recent distros / toolchains / … don’t have it, which causes link errors.

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [January 3, 2022, 6:41am UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/2 "2022-01-03T06:41:17Z")

</div>

I’m not sure why you’d be wanting to find this particular library. I’d expect the compiler would find the right one if you linked to `stdc++fs`. Either of the following should work:

```cmake
target_link_libraries(SomeTarget PRIVATE stdc++fs)

```

To answer your question more directly, `find_library()` won’t be looking in any of the directories where you have a `libstdc++fs.*` file. You’d have to provide it with hints, but really I’d be looking to let the compiler provide it on its own with the default linker search path.

You would need to provide the logic that works out whether `libstdc++fs.*` needs to be linked rather than simply relying on whether you can find such a library or not. That probably means logic that only links it for specific version ranges of specific compilers. For example, [cppreference.com](https://en.cppreference.com/w/cpp/filesystem) contains the following note near the bottom of that page:

> Using this library may require additional compiler/linker options. GNU implementation prior to 9.1 requires linking with `-lstdc++fs` and LLVM implementation prior to LLVM 9.0 requires linking with `-lc++fs`.

---

<div class="post-metadata">

### Author: ![jcelerier](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jcelerier/32/2071_2.png) [@jcelerier](https://discourse.cmake.org/u/jcelerier)
#### Post date: [January 3, 2022, 8:37am UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/3 "2022-01-03T08:37:34Z")

</div>

> I’m not sure why you’d be wanting to find this particular library.

Well, because on some platforms it’s there and on others it’s not and I need my code to work on all these platforms. For instance when I compile with libc++13 under Linux there isn’t any such library as the feature has been merged within libc++ directly.

Shouldn’t cmake guarantee that I can use std::filesystem without issue ?

---

<div class="post-metadata">

### Author: ![jcelerier](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jcelerier/32/2071_2.png) [@jcelerier](https://discourse.cmake.org/u/jcelerier)
#### Post date: [January 3, 2022, 10:46am UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/4 "2022-01-03T10:46:27Z")

</div>

Also

```auto
target_link_libraries(SomeTarget PRIVATE stdc++fs)

```

does not work, here it tells me that it does not find the library when linking on systems without that lib (linux with a clang/lld toolchain):

```
ld.lld: error: unable to find library -lstdc++fs
```

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [January 3, 2022, 12:30pm UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/5 "2022-01-03T12:30:08Z")

</div>

See the part I quoted earlier for clang/LLVM:

> [@craig.scott](#):
>
> LLVM implementation prior to LLVM 9.0 requires linking with `-lc++fs`.

You may find `CMAKE_CXX_COMPILER_ID` and `CMAKE_CXX_COMPILER_VERSION` useful in working out if and which library you need to link to.

---

<div class="post-metadata">

### Author: ![jcelerier](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jcelerier/32/2071_2.png) [@jcelerier](https://discourse.cmake.org/u/jcelerier)
#### Post date: [January 3, 2022, 12:33pm UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/6 "2022-01-03T12:33:07Z")

</div>

That is when using libc++, I’m still using libstdc++ in some case. (And CMake does not provide an easy way to tell which standard library is being used).

But again: is this an official CMake position that it’s not possible to simply have \<filesystem\> just work without having to ifdef for every {gcc / clang}x{libc++,libstdc++} version under the sun ?

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [January 4, 2022, 4:21pm UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/7 "2022-01-04T16:21:22Z")

</div>

`filesystem` is now part of C++17 standard, so it can be used without any special flags on compile or link steps for compilers supporting this standard.

In my opinion, it is too costly (and useless) to try to support, in `CMake`, compilers offering a non-standard way to use `filesystem`. Moreover, various compilers, before `C++17`, offers only a partial or buggy `filesystem` implementation.

---

<div class="post-metadata">

### Author: ![JKaniarz](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@JKaniarz](https://discourse.cmake.org/u/JKaniarz)
#### Post date: [March 20, 2023, 3:11pm UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/8 "2023-03-20T15:11:18Z")

</div>

> [@craig.scott](#):
>
> You may find `CMAKE_CXX_COMPILER_ID` and `CMAKE_CXX_COMPILER_VERSION` useful in working out if and which library you need to link to.

Sorry for resurrecting this old thread but I’m also struggling with this problem. I’ve been getting along so far with a large hierarchy of compiler id and version testing, but I’ve got a new twist. Cray’s Clang-based compiler and ICC Clang front end both show up with a complier ID of “Clang”, but Cray’s Clang version 12 requires stdc++fs, Apple’s Clang 12 won’t accept it. Debian’s Clang package could go either way. Is there any other way to distinguish which Clang I’m using or a way to find libstdc++fs?

---

<div class="post-metadata">

### Author: ![craig.scott](https://discourse.cmake.org/user_avatar/discourse.cmake.org/craig.scott/32/20_2.png) [@craig.scott](https://discourse.cmake.org/u/craig.scott)
#### Post date: [March 20, 2023, 9:30pm UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/9 "2023-03-20T21:30:25Z")

</div>

@ben.boeckel @brad.king @chuckatkins Do you folks know if there is a secondary variable that would differentiate these different Clang variants? I don’t have access to Cray or ICC Clang to check.

> [@JKaniarz](#):
>
> Debian’s Clang package could go either way.

Is that just due to which version of Debian you are using (so, just which Clang version)?

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 20, 2023, 9:53pm UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/10 "2023-03-20T21:53:27Z")

</div>

Intel’s Clang frontend should be [`IntelLLVM`](https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html). I suspect a new ID will be required for Cray clang (especially if its versioning differs from upstream Clang).

---

<div class="post-metadata">

### Author: ![JKaniarz](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@JKaniarz](https://discourse.cmake.org/u/JKaniarz)
#### Post date: [March 20, 2023, 10:02pm UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/11 "2023-03-20T22:02:08Z")

</div>

> [@craig.scott](#):
>
> Is that just due to which version of Debian you are using (so, just which Clang version)?

It’s “Debian clang version 11.0.1-2”. By “can go either way” I mean it doesn’t require stdc++fs, but if I pass -lstdc++fs it doesn’t complain either.

As an answer to the original question, I found this “[FindFilesystem.cmake](https://github.com/vector-of-bool/CMakeCM/blob/master/modules/FindFilesystem.cmake)” script. So, so I don’t need Clang variant divination anymore.

---

<div class="post-metadata">

### Author: ![JKaniarz](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@JKaniarz](https://discourse.cmake.org/u/JKaniarz)
#### Post date: [March 20, 2023, 10:11pm UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/12 "2023-03-20T22:11:04Z")

</div>

> [@ben.boeckel](#):
>
> I suspect a new ID will be required for Cray clang (especially if its versioning differs from upstream Clang).

The HPC I’m trying to build on comes with CMake 3.17, so I apologize if this has been fixed. The default CC in PrgEnv-cray shows up in CMake as “Clang”. The version string is “Cray clang version 12.0.3”.

The only thing weird about it I’ve come across is that it requires -lstdc++fs to use std::filesystem.

---

<div class="post-metadata">

### Author: ![brad.king](https://discourse.cmake.org/user_avatar/discourse.cmake.org/brad.king/32/11_2.png) [@brad.king](https://discourse.cmake.org/u/brad.king)
#### Post date: [March 21, 2023, 2:53pm UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/13 "2023-03-21T14:53:00Z")

</div>

> [@JKaniarz](#):
>
> The only thing weird about it I’ve come across is that it requires -lstdc++fs to use std::filesystem.

This requirement comes from the version of the GNU `libstdc++` in use. Early implementations of `std::filesystem` were kept out of the main `-lstdc++` that the compiler driver links by default in order to avoid committing to a stable ABI immediately. Instead one must explicitly link `-lstdc++fs` to get those symbols. Newer versions of `libstdc++` have the symbols integrated.

There are some `FindFilesystem` scripts out in the wild that try to hide this distinction. Upstream CMake never gained one to avoid permanently maintaining infrastructure only needed for a transitional period.

---

<div class="post-metadata">

### Author: ![JKaniarz](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/j/e9a140/32.png) [@JKaniarz](https://discourse.cmake.org/u/JKaniarz)
#### Post date: [March 21, 2023, 7:09pm UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/14 "2023-03-21T19:09:51Z")

</div>

I’m familiar with the history of the library. What’s specifically weird is Cray’s Clang 12 _requires_ it when Clang’s documentation says it’s only required in Clang 9 and my experience with many other versions and variants doesn’t contradict that clam. It’s like Cray went out of their way to extend the transitional period.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [March 22, 2023, 12:22pm UTC](https://discourse.cmake.org/t/find-library-does-not-find-stdc-fs/4764/15 "2023-03-22T12:22:21Z")

</div>

> [@JKaniarz](#):
>
> What’s specifically weird is Cray’s Clang 12 _requires_ it when Clang’s documentation says it’s only required in Clang 9

Clang docs are probably talking about libc++. If Cray is using an older libstdc++, even newer Clang would need to link AFAIK (though a link to the docs in question would help).
