16

I am trying to compile a project with experimental::filesystem in visual studio code using code runner, however I can't get it to compile even in the terminal.

The code is as follows, a very simple test usage from the docs:

#include <iostream> #include<experimental/filesystem> using namespace std; namespace fs = std::experimental::filesystem; int main(){ fs::create_directories("sandbox/a/b"); cout << "done."; return 0; } 

Compiling with

g++ -std=c++17 $fullFileName && ./a.out -lstdc++fs 

in the code runner config or with just

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

in terminal doesn't compile or doesn't work in general.

The error it provides is:

/tmp/cco0g7Qf.o: In function `main': test.cpp:(.text+0x24): undefined reference to `std::experimental::filesystem::v1::create_directories(std::experimental::filesystem::v1::__cxx11::path const&)' /tmp/cco0g7Qf.o: In function `std::experimental::filesystem::v1::__cxx11::path::path<char [12], std::experimental::filesystem::v1::__cxx11::path>(char const (&) [12])': test.cpp:(.text._ZNSt12experimental10filesystem2v17__cxx114pathC2IA12_cS3_EERKT_[_ZNSt12experimental10filesystem2v17__cxx114pathC5IA12_cS3_EERKT_]+0x64): undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()' collect2: error: ld returned 1 exit status 

Any help would be apreciated, I am running linux and have already checked to make sure my libstdc++ is up to date along with my gcc.

0

2 Answers 2

23

The following command line work great for me (GCC 7.2.0 on Ubuntu 17.10):

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

Note that when you omit the -lstdc++fs or put it before test.cpp in the command line, the linking will fail with the undefined reference error you've observed.

Sign up to request clarification or add additional context in comments.

6 Comments

I'm an idiot, in the terminal I was running main.cpp which had other errors. In visual studio code, I was putting it after the ./a.out which is the file it runs, not where it compiles. placing it before the && fixed this issue.
Do you need the -lstdc++fs flag when using the c++17 filesystem library? As in, NOT the experimental one? I'm getting that same undefined reference error with std::filesystem::create_directories(std::filesystem::path const&)
@IronAttorney I guess this comes late, but anybody else see this answer of Jonathan Wakely.
@IronAttorney I found I needed the linker flag for g++ < v10 but for g++ 10 it appears the filesystem library is fully included in libstdc++. This is the case whether I was compiling against C++17 or C++2a/20 with -std=g++XX or -std=c++XX, XX in {17, 2a, 20}
Yeah, I've similarly noticed it works fine lately using latest GCC and Clang with C++ 17 and 20, no messing around with extra flags
|
1

Option 1: The answer from @Flopp worked well for me.

Option 2: Remove ::experimental:: from all of my filesystem calls, then it compiled just fine in Linux under C++ 20 with g++ and clang++.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.