1

Recently I stumbled into a multiple "was not declared in this scope" error looking like this:

/usr/include/c++/9/ext/string_conversions.h:84:25: error: ‘ERANGE’ was not declared in this scope
   84 |       else if (errno == ERANGE
      |                         ^~~~~~
In file included from /usr/include/c++/9/system_error:39,
                 from /usr/include/c++/9/bits/ios_base.h:46,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/istream:38,
                 from /usr/include/c++/9/fstream:38,
                 from myproject/main.cpp:3:
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h: At global scope:
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:42:40: error: ‘EAFNOSUPPORT’ was not declared in this scope
   42 |       address_family_not_supported =   EAFNOSUPPORT,
      |                                        ^~~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:43:28: error: ‘EADDRINUSE’ was not declared in this scope
   43 |       address_in_use =     EADDRINUSE,
      |                            ^~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:44:34: error: ‘EADDRNOTAVAIL’ was not declared in this scope
   44 |       address_not_available =    EADDRNOTAVAIL,
      |                                  ^~~~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:45:30: error: ‘EISCONN’ was not declared in this scope
   45 |       already_connected =    EISCONN,
      |                              ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:46:35: error: ‘E2BIG’ was not declared in this scope
   46 |       argument_list_too_long =    E2BIG,
      |                                   ^~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:47:35: error: ‘EDOM’ was not declared in this scope
   47 |       argument_out_of_domain =    EDOM,
      |                                   ^~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:48:25: error: ‘EFAULT’ was not declared in this scope
   48 |       bad_address =     EFAULT,
      |                         ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:49:32: error: ‘EBADF’ was not declared in this scope
   49 |       bad_file_descriptor =    EBADF,
      |                                ^~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:52:25: error: ‘EBADMSG’ was not declared in this scope
   52 |       bad_message =     EBADMSG,
      |                         ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:55:25: error: ‘EPIPE’ was not declared in this scope
   55 |       broken_pipe =     EPIPE,
      |                         ^~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:56:31: error: ‘ECONNABORTED’ was not declared in this scope
   56 |       connection_aborted =    ECONNABORTED,
      |                               ^~~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:57:42: error: ‘EALREADY’ was not declared in this scope
   57 |       connection_already_in_progress =   EALREADY,
      |                                          ^~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:58:31: error: ‘ECONNREFUSED’ was not declared in this scope
   58 |       connection_refused =    ECONNREFUSED,
      |                               ^~~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:59:29: error: ‘ECONNRESET’ was not declared in this scope
   59 |       connection_reset =    ECONNRESET,
      |                             ^~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:60:30: error: ‘EXDEV’ was not declared in this scope
   60 |       cross_device_link =    EXDEV,
      |                              ^~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:61:40: error: ‘EDESTADDRREQ’ was not declared in this scope
   61 |       destination_address_required =   EDESTADDRREQ,
      |                                        ^~~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:62:35: error: ‘EBUSY’ was not declared in this scope
   62 |       device_or_resource_busy =   EBUSY,
      |                                   ^~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:63:32: error: ‘ENOTEMPTY’ was not declared in this scope
   63 |       directory_not_empty =    ENOTEMPTY,
      |                                ^~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:64:35: error: ‘ENOEXEC’ was not declared in this scope
   64 |       executable_format_error =   ENOEXEC,
      |                                   ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:65:32: error: ‘EEXIST’ was not declared in this scope
   65 |       file_exists =            EEXIST,
      |                                ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:66:28: error: ‘EFBIG’ was not declared in this scope
   66 |       file_too_large =     EFBIG,
      |                            ^~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:67:30: error: ‘ENAMETOOLONG’ was not declared in this scope
   67 |       filename_too_long =    ENAMETOOLONG,
      |                              ^~~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:68:35: error: ‘ENOSYS’ was not declared in this scope
   68 |       function_not_supported =    ENOSYS,
      |                                   ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:69:29: error: ‘EHOSTUNREACH’ was not declared in this scope
   69 |       host_unreachable =    EHOSTUNREACH,
      |                             ^~~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:72:31: error: ‘EIDRM’ was not declared in this scope
   72 |       identifier_removed =    EIDRM,
      |                               ^~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:75:34: error: ‘EILSEQ’ was not declared in this scope
   75 |       illegal_byte_sequence =    EILSEQ,
      |                                  ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:76:45: error: ‘ENOTTY’ was not declared in this scope
   76 |       inappropriate_io_control_operation =  ENOTTY,
      |                                             ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:77:25: error: ‘EINTR’ was not declared in this scope
   77 |       interrupted =     EINTR,
      |                         ^~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:79:26: error: ‘ESPIPE’ was not declared in this scope
   79 |       invalid_seek =     ESPIPE,
      |                          ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:80:22: error: ‘EIO’ was not declared in this scope
   80 |       io_error =     EIO,
      |                      ^~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:81:28: error: ‘EISDIR’ was not declared in this scope
   81 |       is_a_directory =     EISDIR,
      |                            ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:82:26: error: ‘EMSGSIZE’ was not declared in this scope
   82 |       message_size =     EMSGSIZE,
      |                          ^~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:83:26: error: ‘ENETDOWN’ was not declared in this scope
   83 |       network_down =     ENETDOWN,
      |                          ^~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:84:27: error: ‘ENETRESET’ was not declared in this scope
   84 |       network_reset =     ENETRESET,
      |                           ^~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:85:32: error: ‘ENETUNREACH’ was not declared in this scope
   85 |       network_unreachable =    ENETUNREACH,
      |                                ^~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:86:28: error: ‘ENOBUFS’ was not declared in this scope
   86 |       no_buffer_space =    ENOBUFS,
      |                            ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:87:29: error: ‘ECHILD’ was not declared in this scope
   87 |       no_child_process =    ECHILD,
      |                             ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:90:21: error: ‘ENOLINK’ was not declared in this scope
   90 |       no_link =     ENOLINK,
      |                     ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:93:30: error: ‘ENOLCK’ was not declared in this scope
   93 |       no_lock_available =    ENOLCK,
      |                              ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:96:33: error: ‘ENODATA’ was not declared in this scope
   96 |       no_message_available =    ENODATA,
      |                                 ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:99:24: error: ‘ENOMSG’ was not declared in this scope
   99 |       no_message =     ENOMSG,
      |                        ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:100:31: error: ‘ENOPROTOOPT’ was not declared in this scope
  100 |       no_protocol_option =    ENOPROTOOPT,
      |                               ^~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:101:31: error: ‘ENOSPC’ was not declared in this scope
  101 |       no_space_on_device =    ENOSPC,
      |                               ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:104:32: error: ‘ENOSR’ was not declared in this scope
  104 |       no_stream_resources =    ENOSR,
      |                                ^~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:107:37: error: ‘ENXIO’ was not declared in this scope
  107 |       no_such_device_or_address =   ENXIO,
      |                                     ^~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:108:28: error: ‘ENODEV’ was not declared in this scope
  108 |       no_such_device =     ENODEV,
      |                            ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:109:37: error: ‘ENOENT’ was not declared in this scope
  109 |       no_such_file_or_directory =   ENOENT,
      |                                     ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:110:28: error: ‘ESRCH’ was not declared in this scope
  110 |       no_such_process =    ESRCH,
      |                            ^~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:111:28: error: ‘ENOTDIR’ was not declared in this scope
  111 |       not_a_directory =    ENOTDIR,
      |                            ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:112:26: error: ‘ENOTSOCK’ was not declared in this scope
  112 |       not_a_socket =     ENOTSOCK,
      |                          ^~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:115:26: error: ‘ENOSTR’ was not declared in this scope
  115 |       not_a_stream =     ENOSTR,
      |                          ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:118:27: error: ‘ENOTCONN’ was not declared in this scope
  118 |       not_connected =     ENOTCONN,
      |                           ^~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:119:30: error: ‘ENOMEM’ was not declared in this scope
  119 |       not_enough_memory =    ENOMEM,
      |                              ^~~~~~
In file included from /usr/include/errno.h:28,
                 from /usr/include/c++/9/cerrno:42,
                 from /usr/include/c++/9/ext/string_conversions.h:44,
                 from /usr/include/c++/9/bits/basic_string.h:6493,
                 from /usr/include/c++/9/string:55,
                 from /usr/include/c++/9/bits/locale_classes.h:40,
                 from /usr/include/c++/9/bits/ios_base.h:41,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/istream:38,
                 from /usr/include/c++/9/fstream:38,
                 from myproject/main.cpp:3:
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:122:27: error: ‘EOPNOTSUPP’ was not declared in this scope; did you mean ‘ENOTSUP’?
  122 |       not_supported =     ENOTSUP,
      |                           ^~~~~~~
In file included from /usr/include/c++/9/system_error:39,
                 from /usr/include/c++/9/bits/ios_base.h:46,
                 from /usr/include/c++/9/ios:42,
                 from /usr/include/c++/9/istream:38,
                 from /usr/include/c++/9/fstream:38,
                 from Pyfhel/Afhel/Afseal.cpp:34:
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:129:34: error: ‘EINPROGRESS’ was not declared in this scope
  129 |       operation_in_progress =    EINPROGRESS,
      |                                  ^~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:130:35: error: ‘EPERM’ was not declared in this scope
  130 |       operation_not_permitted =   EPERM,
      |                                   ^~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:131:35: error: ‘EOPNOTSUPP’ was not declared in this scope; did you mean ‘ENOTSUP’?
  131 |       operation_not_supported =   EOPNOTSUPP,
      |                                   ^~~~~~~~~~
      |                                   ENOTSUP
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:132:34: error: ‘EWOULDBLOCK’ was not declared in this scope
  132 |       operation_would_block =    EWOULDBLOCK,
      |                                  ^~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:138:30: error: ‘EACCES’ was not declared in this scope
  138 |       permission_denied =    EACCES,
      |                              ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:141:28: error: ‘EPROTO’ was not declared in this scope
  141 |       protocol_error =     EPROTO,
      |                            ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:144:35: error: ‘EPROTONOSUPPORT’ was not declared in this scope
  144 |       protocol_not_supported =    EPROTONOSUPPORT,
      |                                   ^~~~~~~~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:145:34: error: ‘EROFS’ was not declared in this scope; did you mean ‘EOF’?
  145 |       read_only_file_system =    EROFS,
      |                                  ^~~~~
      |                                  EOF
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:146:41: error: ‘EDEADLK’ was not declared in this scope
  146 |       resource_deadlock_would_occur =   EDEADLK,
      |                                         ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:147:42: error: ‘EAGAIN’ was not declared in this scope
  147 |       resource_unavailable_try_again =   EAGAIN,
      |                                          ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:148:32: error: ‘ERANGE’ was not declared in this scope
  148 |       result_out_of_range =    ERANGE,
      |                                ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:155:28: error: ‘ETIME’ was not declared in this scope
  155 |       stream_timeout =     ETIME,
      |                            ^~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:159:28: error: ‘ETXTBSY’ was not declared in this scope
  159 |       text_file_busy =     ETXTBSY,
      |                            ^~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:162:23: error: ‘ETIMEDOUT’ was not declared in this scope
  162 |       timed_out =     ETIMEDOUT,
      |                       ^~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:163:41: error: ‘ENFILE’ was not declared in this scope; did you mean FILE’?
  163 |       too_many_files_open_in_system =   ENFILE,
      |                                         ^~~~~~
      |                                         FILE
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:164:32: error: ‘EMFILE’ was not declared in this scope; did you mean FILE’?
  164 |       too_many_files_open =    EMFILE,
      |                                ^~~~~~
      |                                FILE
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:165:28: error: ‘EMLINK’ was not declared in this scope
  165 |       too_many_links =     EMLINK,
      |                            ^~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:166:41: error: ‘ELOOP’ was not declared in this scope
  166 |       too_many_symbolic_link_levels =   ELOOP,
      |                                         ^~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:169:28: error: ‘EOVERFLOW’ was not declared in this scope
  169 |       value_too_large =    EOVERFLOW,
      |                            ^~~~~~~~~
/usr/include/x86_64-linux-gnu/c++/9/bits/error_constants.h:172:32: error: ‘EPROTOTYPE’ was not declared in this scope
  172 |       wrong_protocol_type =    EPROTOTYPE
      |                                ^~~~~~~~~~
In file included from myproject/main.cpp:4:
/usr/include/c++/9/thread: In function ‘void std::this_thread::sleep_for(const std::chrono::duration<_Rep1, _Period1>&)’:
/usr/include/c++/9/thread:378:53: error: ‘EINTR’ was not declared in this scope
  378 |  while (::nanosleep(&__ts, &__ts) == -1 && errno == EINTR)
      |                                                     ^~~~~

arising from a single .cpp compilation:

gcc -Imyproject/include1 -Imyproject/include2 -Imyproject/include3 -c myproject/main.cpp -o myproject/main.o

The .cpp file is not doing anything special:

// main.cpp
#include <iostream> /* Print in std::cout */
#include <fstream>      /* file management */
#include <thread>   /* memory pools, multithread*/
#include "myheader1.h"
int main(int argc, char** argv) {
    std::cout << "Have " << argc << " arguments:" << std::endl;
    for (int i = 0; i < argc; ++i) {
        std::cout << argv[i] << std::endl;
    }
}

And the myheader.h is empty.

What is the cause of this error and how to solve it?

Note: All the error lines thrown belong to issues in the standard library, and switching to g++ doesn't have any effect.

ibarrond
  • 6,617
  • 4
  • 26
  • 45
  • If anyone wanders why I posted this and answered it, I googled these precise errors and didn't find anything. After solving it, I thought it would be of help to index the full error messages with a minimalistic example to help others in need of this guidance. – ibarrond Jul 07 '21 at 09:18
  • Does this still happen if you compile with `g++` instead of `gcc`? – Useless Jul 07 '21 at 10:28
  • Yeap, [`g++` is just `gcc` with some extra parameters](https://stackoverflow.com/questions/172587/what-is-the-difference-between-g-and-gcc), and it yields the same results. – ibarrond Jul 07 '21 at 10:59
  • 2
    I know that, but whether those _extra parameters_ change the result is relevant information that should be in the question. – Useless Jul 07 '21 at 12:28

1 Answers1

1

Since we are quite certain we didn't touch the standard library in purpose, and it is not a matter of C++ version (already tried adding -std=c++17 and similar to the compilation args, to no avail), this error means we accidentally mangled with the standard .h headers.

What is the cause of this error?

At least one of the header files present in the include dirs (myproject/include1, myproject/include2, myproject/include3 in this case) is overloading the name of a C++ stdlib module, causing it to break.

How to solve it?

Remove the conflicting header file (if you detect it), rename all header files in your include dirs to have non-standard names (avoid vector.h, memory.h and the sorts), or include only the files & directories needed for your compilation, leaving out the conflicting files.

ibarrond
  • 6,617
  • 4
  • 26
  • 45
  • I have a similar error when trying to compile my project on RHEL 8, I searched /usr/include for 'ERANGE' and it does not return any result. – sham1810 Nov 02 '22 at 08:55
  • @sham1810 I would check two things: 1) The compiler and its directives support C++17 or whichever version you were working with that included `ERANGE`, 2) None of the include directories you added in the compilation contains any header that could overlap with headers from the standard library. – ibarrond Nov 02 '22 at 14:03