cgcccompilation

c language gcc compiler *.i file #3 "" 2 what is this?


//main.c
#include <stdio.h>
#include "swap.h"

int main(void){
    return 0;
}

.

//swap.h
void swap(int* a, int* b){
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

I want wondering how preprocessor working in compiler.

So i started to analyze from preprocessor.

And i tried preprocessing in the terminal.

gcc -E  c.c -o c.i

In this code.

And I have a question.

# 2 "c.c" 2
# 1 "swap.h" 1

# 1 "swap.h"
void swap(int* a, int* b){
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
# 3 "c.c" 2

extern void hello(void);

int main(void){
    return 0;
}

In this code.

# 2 "c.c" 2
# 1 "swap.h" 1
# 3 "c.c" 2

What does this code mean?

In other words, what do # and numbers mean?

And what do ".c" and ".h" mean?


Solution

  • These directives indicate the origin of a pariticular line of code in the output.

    The first two fields are the line number and source file name of the original file starting from that point. Any numbers after that are flags

    From the GCC documentation on preprocessor output:

    Source file name and line number information is conveyed by lines of the form

    # linenum filename flags
    

    These are called linemarkers. They are inserted as needed into the output (but never within a string or character constant). They mean that the following line originated in file filename at line linenum. filename will never contain any non-printing characters; they are replaced with octal escape sequences.

    After the file name comes zero or more flags, which are ‘1’, ‘2’, ‘3’, or ‘4’. If there are multiple flags, spaces separate them. Here is what the flags mean:

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