Link errors using members in C++17

前端 未结 3 1922
小鲜肉
小鲜肉 2020-12-04 01:38

I\'m using gcc 7.2 on Ubuntu 16.04, and I need to use the new filesystem library from C++17. Even though there is indeed a library called experimental/filesystem, I can\'t u

相关标签:
3条回答
  • 2020-12-04 01:56

    You can also sudo apt install g++-8 and use #include <filesystem> as cppreference described instead of #include <experimental/filesystem> in older g++ and libstdc++ version.

    If I install gcc 8 in Ubuntu, will I have 2 different libstdc++ library or merely the original one get updated?

    you'll probably have two even though the newer one should work as a drop-in replacement for the old one.

    I notice that a libstdc++-8-dev is installed along with g++-8.

    This works for me:

    g++-8 -g -Wall -std=c++17 test.cpp -lstdc++fs

    It seems that even with g++-8, the filesystem library is not automatically linked, you still need to provide -lstdc++fs, and -std=c++17 is also needed in language level.

    0 讨论(0)
  • 2020-12-04 02:00

    Following worked for me:

    In code:

    #include <filesystem>
    namespace filesystem = std::filesystem;
    

    In CMakeLists:

    set (CMAKE_CXX_FLAGS "-lstdc++fs -std=c++17")
    

    On Ubuntu 18.04 with GCC 10.

    0 讨论(0)
  • 2020-12-04 02:07

    Add the flag -lstdc++fs:

    $ g++-7 test.cpp -std=c++17 -lstdc++fs
    

    gcc 7.2 supports C++17 experimental filesystem namespace only. I do not know, maybe gcc 7.3 supports std filesystem namespace already.

    0 讨论(0)
提交回复
热议问题