Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

linux

gcc

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:

  • http://www.gnu.org/software/libc/manual/html_node/Search-Functions.html#index-index
  • http://pubs.opengroup.org/onlinepubs/009695399/functions/index.html

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.
like image 266
ideasman42 Avatar asked May 03 '16 09:05

ideasman42


People also ask

How to suppress warnings in GCC?

To suppress this warning use the unused attribute (see Variable Attributes). This warning is also enabled by -Wunused , which is enabled by -Wall . Warn whenever a static function is declared but not defined or a non-inline static function is unused. This warning is enabled by -Wall .

Which GCC flag is used to enable all compiler warnings?

gcc -Wall enables all compiler's warning messages. This option should always be used, in order to generate better code.

How do I enable all warnings in GCC?

For GCC, copying from the full list of warnings provided by this tool for your compiler version appears to be the only way to ensure that all warnings are turned on, since (unlike Clang) GCC does not provide -Weverything . The tool appears to parse the actual c.

What is pedantic GCC?

The -pedantic option tells GCC to issue warnings in such cases; -pedantic-errors says to make them errors instead. This does not mean that all non-ISO constructs get warnings or errors. See Options to Request or Suppress Warnings, for more detail on these and related command-line options.

What is GCC compiler in Linux?

The term compiler refers to a piece of software that converts our source code from a high-level programming language to a low-level programming language (machine-level code) in order to build an executable program file and in Linux Operating Systems, and to compile C program in Linux, we'll need to install the GCC Compiler.

How do I run an executable program from a GCC file?

When you run this command, the compiler generates an executable program called a.out. To run the executable program type the following command: To give the executable program a different name, we can add the "-o" option to the gcc command after the name of the file we are compiling, as shown below:

How to compile a C program from source code in Linux?

This wikiHow teaches you how to compile a C program from source code by using the GNU Compiler (GCC) for Linux and Minimalist Gnu (MinGW) for Windows. Open up a terminal window on your Linux system. Its icon usually is a black screen with some white characters on it. You can usually find it in your Applications menu. Install GCC.

How to check if GCC is working on Linux?

Now you can check whether GCC is working with the following command: You can install GCC on Arch Linux too. All the required packages are available in the Arch package repository. Arch also has a meta package base-devel, which you can install to get all the required tools needed to compile C and C++ programs on Arch Linux.


Video Answer


2 Answers

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]

like image 115
M.M Avatar answered Oct 24 '22 00:10

M.M


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