0

I need to create a C ++ directory on linux using some simple function that doesn't make use of the Boost library. I don't want to use any library that requires installing software on the linux machine.

One way to do this is to use the stat and mkdir functions, but I'd like to know if there are other alternatives, using C ++.

I am testing the following code:

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <filesystem>

int main ()
{
  std::string folder_name;

  folder_name = "new_directory";
  std::filesystem::create_directories(folder_name.c_str());
  return 0;
}

I compile it with the command:

g++ -Wall -std=c++17 example.cpp -o example

And I get the error:

/usr/bin/ld: /tmp/ccXy1DgT.o: in function `main':
example.cpp:(.text+0x92): undefined reference to `std::filesystem::create_directories(std::filesystem::__cxx11::path const&)'
/usr/bin/ld: /tmp/ccXy1DgT.o: in function `std::filesystem::__cxx11::path::path<char const*, std::filesystem::__cxx11::path>(char const* const&, std::filesystem::__cxx11::path::format)':
example.cpp:(.text._ZNSt10filesystem7__cxx114pathC2IPKcS1_EERKT_NS1_6formatE[_ZNSt10filesystem7__cxx114pathC5IPKcS1_EERKT_NS1_6formatE]+0x64): undefined reference to 'std::filesystem::__cxx11::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status
  • What do I have to change for it to compile?

  • Is there an easier way to do it that does not require compiling with C ++ 17 (and that is not using stat and mkdir)?

sehe
  • 374,641
  • 47
  • 450
  • 633
jstechg
  • 123
  • 1
  • 2
  • 10

0 Answers0