The STL <string> header require strings.h header. In this header you can see index variable. Variable can't be named without `_` in STL by C++ Standards. I can't compile my C++ project, because I use `index` variable too. So, file `1.cpp`: ``` #include <string> std::string index; int main() { } ``` Compile: c++ 1.cpp And I've got an error!
FYI: The specific error report is: # c++ string_index_use.cpp string_index_use.cpp:3:13: error: redefinition of 'index' as different kind of symbol std::string index; ^ /usr/include/strings.h:62:7: note: previous definition is here char *index(const char *, int) __pure; /* LEGACY */ ^ 1 error generated. for: # more string_index_use.cpp #include <string> std::string index; int main() { } I get the same for stable/13 and main [so: 14]. For reference, for the __BEGIN_DECLS related use in strings.h : # grep -r "define.*__BEGIN_DECLS" /usr/include/ /usr/include/sys/cdefs.h:#define __BEGIN_DECLS extern "C" { /usr/include/sys/cdefs.h:#define __BEGIN_DECLS and the code block is: #if defined(__cplusplus) #define __BEGIN_DECLS extern "C" { #define __END_DECLS } #else #define __BEGIN_DECLS #define __END_DECLS #endif The strings.h is used via /usr/include/string.h by the text: . . . #include <sys/cdefs.h> #include <sys/_null.h> #include <sys/_types.h> /* * Prototype functions which were historically defined in <string.h>, but * are required by POSIX to be prototyped in <strings.h>. */ #if __BSD_VISIBLE #include <strings.h> #endif . . . So __BSD_VISIBLE in involved in causing the extra definition of index.
(In reply to Mark Millard from comment #1) It looks like anything with __POSIX_VISIBLE==0 (such as adding a line at the beginning to define _C11_SOURCE to avoid POSIX) tends to get errors, such as: In file included from string_index_use.cpp:5: In file included from /usr/include/c++/v1/string:523: In file included from /usr/include/c++/v1/__functional_base:23: /usr/include/c++/v1/new:327:11: error: no member named 'posix_memalign' in the global namespace (void)::posix_memalign(&__result, __alignment, __size); ~~^ In file included from string_index_use.cpp:5: In file included from /usr/include/c++/v1/string:525: In file included from /usr/include/c++/v1/algorithm:667: In file included from /usr/include/c++/v1/functional:506: In file included from /usr/include/c++/v1/__functional/function.h:22: In file included from /usr/include/c++/v1/__memory/shared_ptr.h:35: In file included from /usr/include/c++/v1/atomic:524: In file included from /usr/include/c++/v1/__thread/timed_backoff_policy.h:16: /usr/include/c++/v1/__threading_support:411:11: error: use of undeclared identifier 'nanosleep' while (nanosleep(&__ts, &__ts) == -1 && errno == EINTR); ^ 2 errors generated. __BSD_VISIBLE==0 is associated with __POSIX_VISIBLE==0 by default. So it looks like __BSD_VISIBLE==0 is not really an alternative for avoiding the issue that you hit.
This works as designed. Use: #define _POSIX_C_SOURCE 200809 And you will be fine.
(In reply to Eugene Grosbein from comment #3) Or, better: #define _XOPEN_SOURCE 700
Feedback timeout (3 months). That was pilot error.