My developing/producing environments are all CentOS-7.9. And I have installed "devtoolset-8" on my developing env to use GCC8. But when I execute the code. I met some problems.
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main()
{
std::cout
<< fs::path( "/foo/bar.txt" ).filename() << '\n'
<< fs::path( "/foo/.bar" ).filename() << '\n'
<< fs::path( "/foo/bar/" ).filename() << '\n'
<< fs::path( "/foo/." ).filename() << '\n'
<< fs::path( "/foo/.." ).filename() << '\n'
<< fs::path( "." ).filename() << '\n'
<< fs::path( ".." ).filename() << '\n'
<< fs::path( "/" ).filename() << '\n'
<< fs::path( "//host" ).filename() << '\n';
}
In order to execute the code, I input the command
g++ testFilesystem.cpp -std=c++17 -lstdc++fs
But it occurs errors.
/tmp/ccmt2a0M.o: In function `std::filesystem::path::path<char [13], std::filesystem::path>(char const (&) [13], std::filesystem::path::format)':
testFilesystem.cpp:(.text._ZNSt10filesystem4pathC2IA13_cS0_EERKT_NS0_6formatE[_ZNSt10filesystem4pathC5IA13_cS0_EERKT_NS0_6formatE]+0x5e): undefined reference to `std::filesystem::path::_M_split_cmpts()'
/tmp/ccmt2a0M.o: In function `std::filesystem::path::path<char [10], std::filesystem::path>(char const (&) [10], std::filesystem::path::format)':
testFilesystem.cpp:(.text._ZNSt10filesystem4pathC2IA10_cS0_EERKT_NS0_6formatE[_ZNSt10filesystem4pathC5IA10_cS0_EERKT_NS0_6formatE]+0x5e): undefined reference to `std::filesystem::path::_M_split_cmpts()'
/tmp/ccmt2a0M.o: In function `std::filesystem::path::path<char [7], std::filesystem::path>(char const (&) [7], std::filesystem::path::format)':
testFilesystem.cpp:(.text._ZNSt10filesystem4pathC2IA7_cS0_EERKT_NS0_6formatE[_ZNSt10filesystem4pathC5IA7_cS0_EERKT_NS0_6formatE]+0x5e): undefined reference to `std::filesystem::path::_M_split_cmpts()'
/tmp/ccmt2a0M.o: In function `std::filesystem::path::path<char [8], std::filesystem::path>(char const (&) [8], std::filesystem::path::format)':
testFilesystem.cpp:(.text._ZNSt10filesystem4pathC2IA8_cS0_EERKT_NS0_6formatE[_ZNSt10filesystem4pathC5IA8_cS0_EERKT_NS0_6formatE]+0x5e): undefined reference to `std::filesystem::path::_M_split_cmpts()'
/tmp/ccmt2a0M.o: In function `std::filesystem::path::path<char [2], std::filesystem::path>(char const (&) [2], std::filesystem::path::format)':
testFilesystem.cpp:(.text._ZNSt10filesystem4pathC2IA2_cS0_EERKT_NS0_6formatE[_ZNSt10filesystem4pathC5IA2_cS0_EERKT_NS0_6formatE]+0x5e): undefined reference to `std::filesystem::path::_M_split_cmpts()'
/tmp/ccmt2a0M.o:testFilesystem.cpp:(.text._ZNSt10filesystem4pathC2IA3_cS0_EERKT_NS0_6formatE[_ZNSt10filesystem4pathC5IA3_cS0_EERKT_NS0_6formatE]+0x5e): more undefined references to `std::filesystem::path::_M_split_cmpts()' follow
collect2: error: ld returned 1 exit status