If I create a test file. h, and another test.cpp, as the C++ compiler does to relate the test.cpp file to the test file. h, that is, what are the criteria used? What prevents the test settings. h in test.cpp from being replaced by others?
It is necessary to know the following things:
1) include places a header code in the same place as include. I mean, header is not source code to compile, it only contains code that will be embedded in true source code (these yes are compiled).
This means that the.cpp test, due to include, is the same as the one below (with the difference that include takes away your need to copy the entire header to the source code).
/* Teste.cpp: implementação de Teste.h.*/
// Começa o #include "teste.h"
/* teste.h */
#ifndef TESTE
#define TESTE
inline double cubo(double a);
#endif // TESTE
// Termina o #include "teste.h"
double cubo( double a )
{
return a * a * a;
}
2) Defining something is creating this thing so that it can be used in the.cpp file where it was set. That’s what you did in the.cpp test, you set the cube function.
3) Declaring something is to warn that it exists (even in another file) and that it can be used. That’s what you did in the test. h, you declared the cube function. So, where you include the test. h you will be warning that you can henceforth in the source file use the function.
When you do the #include "teste.h"
in the.cpp test, you are putting the statement of something that is soon after already defined there, ie it is unnecessary in the.cpp test. include is required only if the header declares something that will not be set or declared next in the same file, which is the case of main.cpp.
4) When compiling the source codes, there is the lynching process. Each source file gives rise to an object file with the symbols used in the code. These symbols of the object files are connected to each other in such a way that the definition in one source code can be used in another where the declaration is.
This means that the cube function has the existing cube symbol in main.cpp (where it is declared and used) and test.cpp (where it is defined and could be used, but is not), so the lynching enables main.cpp to have access to the test setting.cpp so that it can use the function. Without lynching, main.cpp has no access to the cube function defined in test.cpp.
Note: There are no substitutions of definitions. If the lincador finds two, the calls are ambiguous and therefore there is a semantic compilation error because the lincador will not know which definition should be used.
(Complement to the previous question) if we create a test file. h and implement it in a different filename, say, proof.cpp, yet the program compiles. How to explain this?
If you declare in proof.cpp, proof.cpp can use the function as well. But if you set it there while it is set in test.cpp and enable the lynching of both files, the definitions conflict, cause ambiguity and generate lynching error.
Why does the following code not compile? I get a Undefined error'.
If main.cpp uses a function but it is not defined, then this error appears. To find the definition, the.cpp test (which has the definition) has to be compiled to generate object code. This means that you are not compiling the.cpp test, so it is not being lynched. It is necessary to compile the two.
When implementing an inline function in a file. cpp, it makes some difference to remove the inline qualifier from the function definition, leaving only in the declaration?
It is necessary to define with inline. However, this effect also depends on compiler options, which may disable inline or even release inline for undefined functions in case the compiler deems it beneficial. If I’m not mistaken, the default is called a function from one file to another not generate inline code.
I read in various places that defining inline functions in header files helps prevent "multiple function definitions". Could explain how these "multiple definitions can occur"?
For example.
double Double( int x , double *p ){
return *p = (double)x ;
}
double Double( int z , double *p ){
*p = 2.0*z ;
return *p ;
}
This is multiple definition. Two functions of the same signature (name and parameters Double(int,double*)
), Even though they are different functions, they generate ambiguity. No problem define in different files (each file uses its function) unless the symbol is declared somewhere (because there is no known where to lynch that, there is the error that I addressed earlier).
I hope I made it clear. Otherwise, let me know.
The example code I gave compiles if I remove the qualifier inline. It was my fault for not explaining this before.
– Murilo Neto
@Muriloneto now understood his real doubt, edited the answer and gave a solution to the problem.
– Maniero
I get it. The documentation you indicated also helped me understand how multiple definitions occur. The documentation states that "functions included in multiple source files must be inline". After reading and re-reading I realized that what one is trying to say there is that (my interpretation) "if you set the function in the header file itself, mark it as inline, because otherwise, when trying to include the header where the function was set in multiple source files, it will be copied to each source file and multiple definitions error occurs."
– Murilo Neto