C language and GCC compiler

Anne langage C gcc

Standard defined macros

To know the standard macros defined by the compiler, simply run:

gcc -dM -E - < /dev/null

For instance:

$ gcc -dM -E - < /dev/null | grep __STDC_VERSION__
#define __STDC_VERSION__ 201112L

Don’t use standard include files

In some cases, an application has to be self contained ie. no other files are needed to compile it. To be sure that gcc doesn’t use the system header files, the option -nostdinc can be used :

Do not search the standard system directories for header files.
Only the directories you have specified with -I options
(and the directory of the current file, if appropriate) are searched.

See also more preprocessor options.

Include Case-Sensitivity

Problem

When studying an application that comes from Windows, there can be some mismatch between the names of the included files and their names on the system. It can work on Windows because the file system is case-insensitive.

Solution

The correct way to fix the problem is to change the source code, but in some cases, this can be forbidden.

A workaround is to use the gcc option -remap that makes possible to provide a file, that must be named header.gcc, to give the match between the include name and the file name.

How to do

Suppose that you have a source directory srcs and an include directory includes.

  • build the list of all the files :

      SRCS="srcs/*.[cC] includes/*.[hH]"
    
  • find the names of the included files (supposed to be between ") :

      INCL=$(grep 'include *"' $SRCS | sed 's=.*"\(.*\)".*=\1=' | sort -u )
    

    Adapt the regexp for more complex cases.

  • find the file(s) for each included header :

for f in $INCL ; do
  FILES=$(find includes/ -iname $f)
  if [ -z "$FILES" ] ; then
    echo "ERROR: $f not found" 1>&2
  else
    if [ $(echo $FILES | wc -l) -ne 1 ] ; then
      echo "ERROR: found more then one $f : $FILES" 1>&2
    else
      if [ "$f" != "$FILES" ] ; then
        echo "$f $FILES"
      fi
    fi
  fi
done
  • the result of the script can be redirected to header.gcc but the errors have to be fixed.

WARNING: this has to be fixed. The header.gcc file has to be in each source directory to make -remap work.

Wrong file extension

When studying a C application that comes from Windows, it can be the case that some files have the extension .C because Windows file system is case-insensitive (see above). But gcc use the file extension to guess its kind, and concludes that .C files are C++ files. The option -x c forces it to consider them as C code.

See also

Voir aussi :