issue with g++-7 filesystem module

I have installed gcc7 package and tried to compile a simple test program (see below) to make use of the included filesystem module.

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

auto main (int argc, char* argv ]) -> int
{
        fs::path p (argv [1]);
        std::cout << "filename and extension: " << p.filename () << std::endl;  // "file.ext"
        std::cout << "filename only: " << p.stem () << std::endl;               // "file"

        return0;
}

When I tried to compile the above code, I got the following error messages.

[habibie@linux:/home/local/PEOPLE/habibie/works/C++/filesystem/build 386%] ~ make
**Scanning dependencies of target filesystem**
 50%] Building CXX object CMakeFiles/filesystem.dir/main.cpp.o
**/home/local/PEOPLE/habibie/works/C++/filesystem/main.cpp:2:10:****fatal error: **filesystem: No such file or directory
 #include **<filesystem>**
          **^~~~~~~~~~~~**
compilation terminated.
CMakeFiles/filesystem.dir/build.make:62: recipe for target 'CMakeFiles/filesystem.dir/main.cpp.o' failed
make[2]: *** [CMakeFiles/filesystem.dir/main.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/filesystem.dir/all' failed
make[1]: *** [CMakeFiles/filesystem.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
[habibie@linux:/home/local/PEOPLE/habibie/works/C++/filesystem/build 387%] ~

It looks like the gcc7 package does not come with a filesystem header file, but rather an experimental/filesystem header file. So, I replaced the line #include with #include <experimental/filesystem> and the namespace line to reflect the change as shown below.

#include <iostream>
#include <**experimental**/filesystem>

namespace fs = std**::experimental**::filesystem**::v1**;

auto main (int argc, char* argv ]) -> int
{
        fs::path p (argv [1]);
        std::cout << "filename and extension: " << p.filename () << std::endl;  // "file.ext"
        std::cout << "filename only: " << p.stem () << std::endl;               // "file"

        return0;
}

Now, the compilation gives the following error messages:

[habibie@linux:/home/local/PEOPLE/habibie/works/C++/filesystem/build 389%] ~ make
**Scanning dependencies of target filesystem**
 50%] Building CXX object CMakeFiles/filesystem.dir/main.cpp.o
[100%] **Linking CXX executable filesystem**
CMakeFiles/filesystem.dir/main.cpp.o: In function `std::experimental::filesystem::v1::path::stem() const':
main.cpp:(.text._ZNKSt12experimental10filesystem2v14path4stemEv[_ZNKSt12experimental10filesystem2v14path4stemEv]+0xf): undefined r
eference to `std::experimental::filesystem::v1::path::_M_find_extension() const'
main.cpp:(.text._ZNKSt12experimental10filesystem2v14path4stemEv[_ZNKSt12experimental10filesystem2v14path4stemEv]+0x120): undefined
 reference to `std::experimental::filesystem::v1::path::_M_split_cmpts()'
CMakeFiles/filesystem.dir/main.cpp.o: In function `main':
main.cpp:(.text.startup+0x63): undefined reference to `std::experimental::filesystem::v1::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status
CMakeFiles/filesystem.dir/build.make:94: recipe for target 'filesystem' failed
make[2]: *** [filesystem] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/filesystem.dir/all' failed
make[1]: *** [CMakeFiles/filesystem.dir/all] Error 2
Makefile:127: recipe for target 'all' failed
make: *** [all] Error 2
1.513u+0.155s=0:02.41e(68.8%) TDSavg=0k+0k+0k max=97248k 26632+288io 27pf+0sw
[habibie@linux:/home/local/PEOPLE/habibie/works/C++/filesystem/build 390%] ~

Has anyone encountered such an issue and can offer a solution here?

Will be moved to Development > Programming/Scripting and is CLOSED for the moment.

Moved from Applications and open again.

Try adding -lstdc++fs.

Thank you and that should do the trick. However, I use cmake to generate the Makefile and it puts the linker flag (“-lstdc++fs”) somewhere between the c++ compiler and the object file and caused the compilation error as shown below.

[habibie@linux:/home/local/PEOPLE/habibie/works/C++/filesystem/build 571%] ~ make
/usr/bin/cmake -H/home/local/PEOPLE/habibie/works/C++/filesystem -B/home/local/PEOPLE/habibie/works/C++/filesystem/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/local/PEOPLE/habibie/works/C++/filesystem/build/CMakeFiles /home/local/PEOPLE/habibie/works/C++/filesystem/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/local/PEOPLE/habibie/works/C++/filesystem/build'
make -f CMakeFiles/filesystem.dir/build.make CMakeFiles/filesystem.dir/depend
make[2]: Entering directory '/home/local/PEOPLE/habibie/works/C++/filesystem/build'
cd /home/local/PEOPLE/habibie/works/C++/filesystem/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/local/PEOPLE/habibie/works/C++/filesystem /home/local/PEOPLE/habibie/works/C++/filesystem /home/local/PEOPLE/habibie/works/C++/filesystem/build /home/local/PEOPLE/habibie/works/C++/filesystem/build /home/local/PEOPLE/habibie/works/C++/filesystem/build/CMakeFiles/filesystem.dir/DependInfo.cmake --color=
make[2]: Leaving directory '/home/local/PEOPLE/habibie/works/C++/filesystem/build'
make -f CMakeFiles/filesystem.dir/build.make CMakeFiles/filesystem.dir/build
make[2]: Entering directory '/home/local/PEOPLE/habibie/works/C++/filesystem/build'
 50%] Building CXX object CMakeFiles/filesystem.dir/main.cpp.o
/usr/bin/c++ -I/home/local/PEOPLE/habibie/works/C++/filesystem/build -std=c++1z -O3 -DNDEBUG -s -std=c++1z -lstdc++fs -o CMakeFiles/filesystem.dir/main.cpp.o -c /home/local/PEOPLE/habibie/works/C++/filesystem/main.cpp
[100%] **Linking CXX executable filesystem**
/usr/bin/cmake -E cmake_link_script CMakeFiles/filesystem.dir/link.txt --verbose=1
/usr/bin/c++ -std=c++1z -O3 -DNDEBUG -s **-lstdc++fs** CMakeFiles/filesystem.dir/main.cpp.o -o filesystem -rdynamic
CMakeFiles/filesystem.dir/main.cpp.o: In function `std::experimental::filesystem::v1::path::stem() const':
main.cpp:(.text._ZNKSt12experimental10filesystem2v14path4stemEv[_ZNKSt12experimental10filesystem2v14path4stemEv]+0xf): undefined reference to `std::experimental::filesystem::v1::path::_M_find_extension() const'
main.cpp:(.text._ZNKSt12experimental10filesystem2v14path4stemEv[_ZNKSt12experimental10filesystem2v14path4stemEv]+0x120): undefined reference to `std::experimental::filesystem::v1::path::_M_split_cmpts()'
CMakeFiles/filesystem.dir/main.cpp.o: In function `main':
main.cpp:(.text.startup+0x63): undefined reference to `std::experimental::filesystem::v1::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status
CMakeFiles/filesystem.dir/build.make:97: recipe for target 'filesystem' failed
make[2]: *** [filesystem] Error 1
make[2]: Leaving directory '/home/local/PEOPLE/habibie/works/C++/filesystem/build'
CMakeFiles/Makefile2:70: recipe for target 'CMakeFiles/filesystem.dir/all' failed
make[1]: *** [CMakeFiles/filesystem.dir/all] Error 2
make[1]: Leaving directory '/home/local/PEOPLE/habibie/works/C++/filesystem/build'
Makefile:130: recipe for target 'all' failed
make: *** [all] Error 2
1.501u+0.119s=0:01.62e(99.3%) TDSavg=0k+0k+0k max=97340k 0+256io 0pf+0sw
[habibie@linux:/home/local/PEOPLE/habibie/works/C++/filesystem/build 572%] ~

However, if I manually moved the linker flag (“-lstdc++fs”) to the end (after the object file), it compiles OK. If anyone knows how to fix this issue with cmake, that will be splendid.

[habibie@linux:/home/local/PEOPLE/habibie/works/C++/filesystem/build 572%] ~ /usr/bin/c++ CMakeFiles/filesystem.dir/main.cpp.o -o filesystem -rdynamic -lstdc++fs
[habibie@linux:/home/local/PEOPLE/habibie/works/C++/filesystem/build 573%] ~

Finally, I have fixed the cmake issue using the cmake target_link_libraries option. Thank you all for helping.