18

I came across the following code in a .cpp file. I do not understand the construct or syntax which involves the header files. I do recognize that these particular header files relate to Android NDK. But, I think the question is a general question about C++ syntax. These appear to be preprocessor commands in some way because they begin with "#". But, they are not the typical #include, #pragma, #ifndef, #define, etc. commands. The source file has more 1000+ such occurrences referencing hundreds of different .h, .c, .cpp files.

typedef int __time_t;
typedef int __timer_t;
# 116 "/home/usr/download/android-ndk-r8b/platforms/android-3/arch-arm/usr/include/machine/_types.h"
# 41 "/home/usr/download/android-ndk-r8b/platforms/android-3/arch-arm/usr/include/sys/_types.h" 2
# 33 "/home/usr/download/android-ndk-r8b/platforms/android-3/arch-arm/usr/include/stdint.h" 2
# 48 "/home/usr/download/android-ndk-r8b/platforms/android-3/arch-arm/usr/include/stdint.h"
typedef __int8_t int8_t;
typedef __uint8_t uint8_t;

The compiler (GCC) does not appear to be throwing any error related to these lines. But, I would like to understand their purpose and function. Can anybody explain these?

Jongware
  • 22,200
  • 8
  • 54
  • 100

3 Answers3

18

This is output from the GCC preprocessor. Those lines are known as linemarkers. They have the syntax:

# linenum filename flags

They are interpreted as saying that the following line has come from the line linenum from filename. They basically just help you and the compiler see where lines were included from. The flags provide some more information:

  • 1 - This indicates the start of a new file.
  • 2 - This indicates returning to a file (after having included another file).
  • 3 - This indicates that the following text comes from a system header file, so certain warnings should be suppressed.
  • 4 - This indicates that the following text should be treated as being wrapped in an implicit extern "C" block.

You can see this output from preprocessing your own programs if you give the -E flag to g++.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
2

You'll typically see lines like that in the output of the preprocessor (i.e., you normally shouldn't be seeing them at all).

They're similar to the standard #line directive, which has the form:

#line 42

or

#line 42 "foo.c"

which the compiler uses to control the contents of error messages.

Without the word line, this:

# 42 "foo.c"

is technically a non-directive (which, just to add to the fun, is a kind of directive). It's essentially a comment as far as the C standard is concerned. At a guess, gcc's preprocessor probably emits these rather than #line directives because #line directives are intended as input to the preprocessor.

gcc's preprocessor refers to these as "linemarkers"; they're discussed in the cpp manual. They're treated like #line directives, except that they can take an additional flag argument.

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
1

The preprocessors tend to introduce these directives and use them to indicate the line and filename. The C++ doesn't define the meaning but it reserves the use of

# <non-directive>

where is something which isn't one of the normal directives. It seems compiler writes have agreed to use the line number and filename in these as the result of preprocessing the file. This use is similar to basically all compilers supporting the -E option to indicate that the file(s) should just be processed.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380