Communication error with compilation error

Why does a double declaration of structures cause a compilation error , and a double definition of functions causes a binding error ?

+4
source share
4 answers

Because function definitions are included in the executable at link time, but decalization or syntax checking is done at compile time

Consider also that when you call a function, and the compiler cannot find the function declaration, then it will generate a warning as an implicit declaration of func() .

To remove this warning, we provide a direct declaration of the func function, int func(); and compiled without warning.

Why do you think this is happening? This is because the compiler did not find the func() character. All this is up to the compiler to make error-free code in accordance with the grammar of the language.

But the construction of the final executable file is performed during the link, and then the linker started looking for the deftion func() function, if found, then fine, and if not, then Linker error

could not have resolved external symbol _func()

Note: any external characters are allowed during connection

In gcc only for compilation use this: (this may vary depending on the compiler)

gcc -Werror -c test.c It will generate a test.o file

then try linking it and making an executable

gcc -Werror -o test test.o test is executable

+4
source

The standard does not indicate when to report an error - this is before the compiler, but mainly because it is detected when errors are detected .

The compiler first parses the files. It is easy to see if the definition of a struct or class several times in the same translation unit ( only this is an error, between translation units you can have a multiple definition of the type of a class ), because it deals with this translation unit.

Secondly, it links object files together (linking). Only now we can say that the same character was exported several times, because this happens when an error occurs.

+2
source

When compiling a program, the compiler must know what the exact definition of the structure used is. But we only need to know which function to use only when we try to link the program.

So, if the structure is defined twice, the compiler gets confused at compile time, so it complains about compilation time.

You can have several definitions for a function at compile time, but confusion only occurs during linking, so it complains at link time.

+2
source

What you say is not necessarily true.

If you “embed” a function definition in the header, and then write its definition in the compilation unit, you will receive an error message

...already has a definition.

The thing you are talking about is when two different compilation units define (or see the definition) of the same function, so no compilation error occurs in any particular compilation unit, this combination of them causes a communication error.

Note, by the way, that this inline really matters. For a non-templated function defined in the header, if you use the inline , this will mean that several compilation units may exist with the specified function. Actually, this does not guarantee that the compiler will execute it.

+1
source

Source: https://habr.com/ru/post/1445967/


All Articles