# Correct way to link std::filesystem with GCC 8?

**URL:** https://discourse.cmake.org/t/correct-way-to-link-std-filesystem-with-gcc-8/4121
**Category:** Code
**Created:** [September 20, 2021, 2:37pm UTC](https://discourse.cmake.org/t/correct-way-to-link-std-filesystem-with-gcc-8/4121 "2021-09-20T14:37:02Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![spinicist](https://discourse.cmake.org/user_avatar/discourse.cmake.org/spinicist/32/605_2.png) [@spinicist](https://discourse.cmake.org/u/spinicist)
#### Post date: [September 20, 2021, 2:37pm UTC](https://discourse.cmake.org/t/correct-way-to-link-std-filesystem-with-gcc-8/4121/1 "2021-09-20T14:37:02Z")

</div>

As noted at the bottom of the page here: [Filesystem library - cppreference.com](https://en.cppreference.com/w/cpp/filesystem), using `std::filesystem` from C++17 with GCC 8 requires adding `-lstdc++fs` to the linker options. GCC 8 is the system compiler for Debian 10.

What is the cleanest/correct way to deal with this within CMake?

After digging around on Github, I have seen projects that deal with this simply by matching on `CMAKE_CXX_COMPILER_ID` and adding the relevant library, which I have tried here: [riesling/CMakeLists.txt at stdfs · spinicist/riesling · GitHub](https://github.com/spinicist/riesling/blob/stdfs/CMakeLists.txt#L65). I am concerned that this may not be optimal/robust, hence I’m wondering if there is a better way. Thanks.

---

<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: [September 20, 2021, 2:51pm UTC](https://discourse.cmake.org/t/correct-way-to-link-std-filesystem-with-gcc-8/4121/2 "2021-09-20T14:51:41Z")

</div>

There is no special handling in `CMake` for this because it is highly dependent on the compiler as well as the version. For example, more recent versions of `gcc` do not require anymore this library because `fs` is part of the standard `C++` runtime.

---

<div class="post-metadata">

### Author: ![spinicist](https://discourse.cmake.org/user_avatar/discourse.cmake.org/spinicist/32/605_2.png) [@spinicist](https://discourse.cmake.org/u/spinicist)
#### Post date: [September 20, 2021, 2:59pm UTC](https://discourse.cmake.org/t/correct-way-to-link-std-filesystem-with-gcc-8/4121/3 "2021-09-20T14:59:58Z")

</div>

Thanks - that’s exactly why I am asking this question. What’s the most idiomatic way to handle this in a graceful way? What extra lines do I have to add to `CMakeLists.txt` to deal with it across as wide a number of compilers and versions as possible?

As far as I can tell, higher versions of GCC still ship that library, even if the main `libstdc++` also includes the symbols. So I think it’s okay to not worry about the specific GCC version?

---

<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: [September 20, 2021, 3:06pm UTC](https://discourse.cmake.org/t/correct-way-to-link-std-filesystem-with-gcc-8/4121/4 "2021-09-20T15:06:00Z")

</div>

You have to do exactly what you currently do: check compiler kind and version to ensure correct configuration to use `filesystem`.

But, FYI, most of recent compilers include `fs` as part of the standard `C++` runtime because it is now part of the language standard (no longer a `Technical Specification`).

---

<div class="post-metadata">

### Author: ![Dakon](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dakon/32/854_2.png) [@Dakon](https://discourse.cmake.org/u/Dakon)
#### Post date: [September 20, 2021, 6:22pm UTC](https://discourse.cmake.org/t/correct-way-to-link-std-filesystem-with-gcc-8/4121/5 "2021-09-20T18:22:02Z")

</div>

I had the same issue in OSM2go, and that’s how I solved it:

> <https://github.com/osm2go/osm2go/blob/master/find_filesystem.cmake>

Maybe I’ll rewrite it to use targets one day.

Eike

---

<div class="post-metadata">

### Author: ![dbahadir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dbahadir/32/197_2.png) [@dbahadir](https://discourse.cmake.org/u/dbahadir)
#### Post date: [September 21, 2021, 8:23am UTC](https://discourse.cmake.org/t/correct-way-to-link-std-filesystem-with-gcc-8/4121/6 "2021-09-21T08:23:34Z")

</div>

At my old employer I solved it with the following function:

```cmake
function( set_required_build_settings_for_GCC8 )
    # Always link with libstdc++fs.a when using GCC 8.
    # Note: This command makes sure that this option comes pretty late on the cmdline.
    link_libraries( "$<$<AND:$<CXX_COMPILER_ID:GNU>,$<VERSION_LESS:$<CXX_COMPILER_VERSION>,9.0>>:-lstdc++fs>" )
endfunction()

```

I had to call that function pretty early in the top-level `CMakeLists.txt` file in order to make sure that it is used everywhere.  
Of course, you should probably instruct your linker to only link with libraries if they are used at all. (E.g. use `add_link_options( LINKER:--as-needed )`.)

---

<div class="post-metadata">

### Author: ![spinicist](https://discourse.cmake.org/user_avatar/discourse.cmake.org/spinicist/32/605_2.png) [@spinicist](https://discourse.cmake.org/u/spinicist)
#### Post date: [September 21, 2021, 8:45am UTC](https://discourse.cmake.org/t/correct-way-to-link-std-filesystem-with-gcc-8/4121/7 "2021-09-21T08:45:18Z")

</div>

@Dakon and @dbahadir Thank you for the suggestions, very helpful.

I’m going to try the one from @dbahadir as it’s explicitly only GCC 8 I’m worried about, not the pre-C++17 experimental filesystem support.

---

<div class="post-metadata">

### Author: ![mojca](https://discourse.cmake.org/user_avatar/discourse.cmake.org/mojca/32/610_2.png) [@mojca](https://discourse.cmake.org/u/mojca)
#### Post date: [January 12, 2022, 4:03pm UTC](https://discourse.cmake.org/t/correct-way-to-link-std-filesystem-with-gcc-8/4121/8 "2022-01-12T16:03:08Z")

</div>

The one-liner with the GCC check isn’t good enough to cover clang (and potentially other compilers).

I have just been pointed to the following issue:  
[https://gitlab.kitware.com/cmake/cmake/-/issues/17834](https://gitlab.kitware.com/cmake/cmake/-/issues/17834)
