ideasman42
ideasman42

Reputation: 48268

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:

See:


Notes:

Upvotes: 9

Views: 374

Answers (3)

David Ranieri
David Ranieri

Reputation: 41055

If I understand well, you want to use index as a variable name without conflicting with the index function included in <strings.h>.

In this case you can override using the preprocessor:

#include <stdio.h>
#define index(s, c) deprecated_index(s, c)
#include <strings.h>

int index = 5;

int main(void)
{
    printf("%d\n", index);
    return 0;
}

Or override index with strchr:

#include <stdio.h>
#include <string.h>
#include <strings.h>

#define index(s, c) strchr(s, c)

int main(void)
{
    int index = 5;
    char *ptr = index("text", 'x');

    printf("%s %d\n", ptr, index);
    return 0;
}

Upvotes: 0

M.M
M.M

Reputation: 141613

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]

Upvotes: 6

You shouldn't redefine these identifiers, as some library that you're linking against could still depend on them existing.

Upvotes: 2

Related Questions