1

I have a C library that declares:

typedef unsigned char byte;

The library then declares a function:

int some_function(const byte* b, int length);

My C++ application includes this C library. When compiling the application using C++17 or higher, I get a compilation error:

In file included from some_file_b.h:7,
                 from some_file_c.h:4,
                 from some_file_d.cpp:3:
some_file_a.h:9:41: error: reference to ‘byte’ is ambiguous
    9 | int some_function (const byte* b, int length);
      |                          ^~~~
In file included from /opt/rh/devtoolset-10/root/usr/include/c++/10/bits/stl_algobase.h:61,
                 from /opt/rh/devtoolset-10/root/usr/include/c++/10/bits/char_traits.h:39,
                 from /opt/rh/devtoolset-10/root/usr/include/c++/10/string:40,
                 from some_file_x.h:6,
                 from some_file_y.h:4,
                 from some_file_z.cpp:2:
/opt/rh/devtoolset-10/root/usr/include/c++/10/bits/cpp_type_traits.h:404:30: note: candidates are: ‘enum class std::byte’
  404 |   enum class byte : unsigned char;
      |                              ^~~~
In file included from some_file_m.h:8,
                 from some_file_n.h:6,
                 from some_file_o.h:6,
                 from some_file_p.h:6,
                 from some_file_q.h:4,
                 from some_file_r.cpp:3:
some_file_s.h:38:23: note:                 ‘typedef unsigned char byte’
   38 | typedef unsigned char byte;
      |                       ^~~~

My system specs are:

[me@centos7 ~/some_dir]$ gcc --version
gcc (GCC) 10.2.1 20200804 (Red Hat 10.2.1-2)
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

[me@centos7 ~/some_dir]$ uname -a
Linux centos7 3.10.0-1160.31.1.el7.x86_64 #1 SMP Thu Jun 10 13:32:12 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

Compilation is fine when using GCC 4.8.5.

How should the library code be changed to have compatibility with C++17 or higher?

By the way, I used -fpermissive in compiling in C++17 due to the following error:

/usr/include/cc++/string.h:734:35: error: friend declaration of ‘std::istream& getline(std::istream&, ost::String&, char, size_t)’ specifies default arguments and isn’t a definition [-fpermissive]
  734 |     friend __EXPORT std::istream &getline(std::istream &is, String &str, char delim = '\n', size_t size = 0);
      |                                   ^~~~~~~

I am not sure if this last information is relevant to the problem though. I included it anyway just in case.

Gerhardh
  • 11,688
  • 4
  • 17
  • 39
hermit.crab
  • 852
  • 1
  • 8
  • 20
  • 1
    There's a class `std::byte`: https://en.cppreference.com/w/cpp/types/byte. Do you have `using namespace std;` in effect or something like that? – Nate Eldredge Jun 23 '21 at 06:33
  • 1
    We hit this issue in our code base too when we upgraded the compiler to C++ 17 and it took a large effort to fix. The fundamental problem were header (`.h`) files declaring `using namespace std;` instead of in individual source (`.cpp`) files. – selbie Jun 23 '21 at 06:33
  • 2
    [why is "using namespace std;" considered bad practice](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Stefan Riedel Jun 23 '21 at 06:35
  • thank you so much for the `using namespace std;` tip. indeed, there was one header file in my application that declared this. upon removal, the compilation succeeded. – hermit.crab Jun 23 '21 at 06:42

0 Answers0