Home >Backend Development >C++ >Why am I Getting a Linker Error When Using `experimental::filesystem` in C 1z?
In attempting to utilize the latest C 1z features amidst the ongoing development of GCC 6.0, a peculiar linker error arises when compiling a sample code involving the experimental::filesystem header.
The issue manifests itself when running the following code:
#include <iostream> #include <experimental/filesystem> namespace fs = std::experimental::filesystem; int main() { fs::path p1 = "/home/pete/checkit"; std::cout << "p1 = " << p1 << std::endl; }
Compiling this code results in the following error:
undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
Resolution:
Contrary to the initial assumption, the Filesystem TS is not affiliated with C 1z but rather constitutes a distinct specification. The GCC implementation of the Filesystem TS is accessible in C 11 mode. To resolve the linking error, the following flag should be added to the linking command:
-lstdc++fs
This instruction guarantees that the program links against the appropriate library containing the necessary implementation for the experimental::filesystem header.
Update:
Subsequent versions of GCC (8.x and above) introduce support for the C 17 Filesystem library. It is accessible via the As of GCC 9, the C 17 std::filesystem components can be used without -lstdc fs, while std::experimental::filesystem symbols are made available through -lstdc exp in GCC 13.3. The above is the detailed content of Why am I Getting a Linker Error When Using `experimental::filesystem` in C 1z?. For more information, please follow other related articles on the PHP Chinese website!