Dwi Novitasari
Dwi Novitasari

Reputation: 21

Why does compiling filesystem code with c++ return me this error?

I'm trying to create a new directory and using the filesystem to set the permission, but when trying to build the project I got an error instead

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;

int main()
{
    fs::path dir = "C:/newscan";
    fs::create_directory(dir);
    fs::permissions(dir, fs::perms::all);
}

it should be creating new directory but i got this error instead

c:\mingw\mingw\include\c++\8.2.0\bits\fs_path.h|237|error: no match for 'operator!=' (operand types are 'std::filesystem::__cxx11::path' and 'std::filesystem::__cxx11::path')|

Upvotes: 1

Views: 3796

Answers (1)

Thomas Sablik
Thomas Sablik

Reputation: 16448

You are using C++17 features in your code and therefore you have to pass the appropriate parameter to the compiler. For gcc and clang the parameter is -std=c++17. You can compile your code in terminal with

/path/to/g++ -std=c++17 /path/to/your/source.cpp -o /path/to/output

If the compiler path is listed in the environment variable PATH you can use g++ without its path. Otherwise the compiler path probably is c:\mingw\mingw\bin\g++ or c:\mingw\bin\g++.

In code::blocks you have to set the same parameter as compiler flag. Go to Project > Build Options > Compiler Settings and put "-std=c++17" in the Other Options. Do the same in Project > Build Options > Linker Settings.

It's possible that you also need to link to stdc++fs with the linker flag -lstdc++fs. I tried it with gcc 9.2 and it worked without -lstdc++fs.

EDIT: I tried gcc:8.2 with docker and the linker flag is necessary. The command line is

/path/to/g++ -std=c++17 /path/to/your/source.cpp -o /path/to/output -lstdc++fs

It's important that -lstdc++fs comes after /path/to/your/source.cpp. I assume that code::blocks will correctly position the flags.

You can read about these compiler and linker flags in https://gcc.gnu.org/projects/cxx-status.html

GCC supports different dialects of C++, corresponding to the multiple published ISO standards. Which standard it implements can be selected using the -std= command-line option.

  • C++98
  • C++11
  • C++14
  • C++17
  • C++2a
  • Technical Specifications

and https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html

-l library

Search the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)

The -l option is passed directly to the linker by GCC. Refer to your linker documentation for exact details. The general description below applies to the GNU linker.

The linker searches a standard list of directories for the library. The directories searched include several standard system directories plus any that you specify with -L.

Static libraries are archives of object files, and have file names like liblibrary.a. Some targets also support shared libraries, which typically have names like liblibrary.so. If both static and shared libraries are found, the linker gives preference to linking with the shared library unless the -static option is used.

It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.

Upvotes: 1

Related Questions