1

I'm trying to compile this very simple program where I'm implementing a simplified version of C++ strings. However the compiler cannot find the std::strlen function even though I included

//main.cpp
#include "str.h"

int main()
{
    return 0;
}

// str.h
#ifndef _STRING_H_
#define _STRING_H_

#include <vector>
#include <cstring>
#include <algorithm>
#include <iterator>

class Str {
public:
    typedef std::vector<char>::size_type size_type;

    Str() { }
    Str(size_type n, char c): data(n, c) { }
    Str(const char* cp) {
        std::copy(cp, cp+std::strlen(cp), std::back_inserter(data));
    }
    template <class In> Str(In b, In e) {
        std::copy(b, e, std::back_inserter(data));
    }

private:
    std::vector<char> data;
};

#endif

I'm compiling using g++ (homebrew)

g++ main.cpp -o main

and here's the result log

In file included from str.h:5,
                 from main.cpp:1:
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:75:11: error: '::memchr' has not been declared
   using ::memchr;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:76:11: error: '::memcmp' has not been declared
   using ::memcmp;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:77:11: error: '::memcpy' has not been declared
   using ::memcpy;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:78:11: error: '::memmove' has not been declared
   using ::memmove;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:79:11: error: '::memset' has not been declared
   using ::memset;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:80:11: error: '::strcat' has not been declared
   using ::strcat;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:81:11: error: '::strcmp' has not been declared
   using ::strcmp;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:82:11: error: '::strcoll' has not been declared
   using ::strcoll;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:83:11: error: '::strcpy' has not been declared
   using ::strcpy;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:84:11: error: '::strcspn' has not been declared
   using ::strcspn;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:85:11: error: '::strerror' has not been declared
   using ::strerror;
           ^~~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:86:11: error: '::strlen' has not been declared
   using ::strlen;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:87:11: error: '::strncat' has not been declared
   using ::strncat;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:88:11: error: '::strncmp' has not been declared
   using ::strncmp;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:89:11: error: '::strncpy' has not been declared
   using ::strncpy;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:90:11: error: '::strspn' has not been declared
   using ::strspn;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:91:11: error: '::strtok' has not been declared
   using ::strtok;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:92:11: error: '::strxfrm' has not been declared
   using ::strxfrm;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:93:11: error: '::strchr' has not been declared
   using ::strchr;
           ^~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:94:11: error: '::strpbrk' has not been declared
   using ::strpbrk;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:95:11: error: '::strrchr' has not been declared
   using ::strrchr;
           ^~~~~~~
/usr/local/Cellar/gcc/8.3.0/include/c++/8.3.0/cstring:96:11: error: '::strstr' has not been declared
   using ::strstr;
           ^~~~~~
In file included from main.cpp:1:
str.h: In constructor 'Str::Str(const char*)':
str.h:19:31: error: 'strlen' is not a member of 'std'
         std::copy(cp, cp+std::strlen(cp), std::back_inserter(data));
                               ^~~~~~
str.h:19:31: note: suggested alternative: 'strstr'
         std::copy(cp, cp+std::strlen(cp), std::back_inserter(data));
                               ^~~~~~
                               strstr

Am I doing something wrong here ? I don't think I missed any headers or namespace identifier so it should compile.

NBWL
  • 141
  • 6
  • 4
    https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier - change your include guards – Mat Jul 14 '19 at 08:17
  • Cool thanks, I didn't know _ was reserved for the implementation. – NBWL Jul 14 '19 at 08:21

1 Answers1

5

The macro name _STRING_H_ is reserved for the C standard to use. Defining such macro is undefined behavior.

You'r include guard around str.h is _STRING_H_ and you define it. But, the same include guard is most probably used in string.h standard C header. So all symbols from string.h will be not visible in your program. As you could get lucky with glibc, as it uses _STRING_H, but I could find many implementations that use just the string _STRING_H_ as include guards around their files.

To fix the issue, change your include guard from _STRING_H_ to example STR_H_ or STRING_H_ without the leading underscore. Remember, about reserved names by the C standard when writing programs.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111