Skip to content
Advertisement

How to compile C code on Linux/GCC without deprecated functions being available?

There are old functions such as index, rindex which have now been superseded by strchr and strrchr.

Is there a way to configure the compiler or defines so these functions aren’t available?

It can cause confusing warnings when:

  • accidentally using index name outside of scope for eg – or worse, not warn and use the function in a way that’s not intended.
  • Older GCC versions (4.x) warn when using -Wshadow if you have a variable called index.

See:


Notes:

  • as @antti-haapala says, the global symbol index shouldn’t be redefined since libraries may use it.
    This question is regarding the common case when a local variable is called index.
  • At the time of writing, glibc doesn’t mark these functions with the deprecated attribute, so warnings related to using deprecated functions have no effect.

Advertisement

Answer

Use the compiler in ISO C mode. The C standard prohibits conforming programs being broken by the presence of identifiers that are not reserved words.

For example, use flags -std=c99.

Sample program:

#include <string.h>

int main()
{
   index("abc", 'x');
}

Compiled with -std=c11 -Werror gives:

error: implicit declaration of function 'index' [-Werror=implicit-function-declaration]

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement