Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this C++ language construct: # (i.e. hash) integer "path_to_header_or_cpp_file" <integer>?

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?

like image 553
Jerimiah Baldwin Avatar asked Dec 27 '13 18:12

Jerimiah Baldwin


People also ask

What is a construct in a language?

A language construct is a syntactically allowable part of a program that may be formed from one or more lexical tokens in accordance with the rules of a programming language. The term "language construct" is often used as a synonym for control structure.

What are constructs in C++?

Apart from using and defining functions, there are three basic programming constructs in C++ that everyone needs to know: the if statement, the while loop and the for loop.

What are examples of programming constructs?

There are a number of recognised basic programming constructs that can be classified as follows: Sequences (First Floor) Selection (Second Floor) Repetition (Third Floor)


2 Answers

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++.

like image 92
Joseph Mansfield Avatar answered Oct 01 '22 15:10

Joseph Mansfield


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.

like image 21
Keith Thompson Avatar answered Oct 01 '22 15:10

Keith Thompson