0

I have three files- main.cpp, file1.cpp and headerFile.h.

file1 is-

#include<iostream> using namespace std; void function1(){ cout<<"this is function1 from file1"<<endl; } 

headerFile.h is-

#ifndef HEADERFILE_H_ #define HEADERFILE_H_ void function1(); #endif /* HEADERFILE_H_ */ 

main.cpp file is-

#include<iostream> using namespace std; #include "headerFile.h" int main(){ function1(); cout<<"this is main function from mainFile"<<endl; return 0; } 

Until this stage function1() is unknown to main.cpp file.
Now people say that as the compiler encounters #include "headerFile.h" in main file it simply copies the code of headerFile.h into main file,
hence the main file becomes-

#include<iostream> using namespace std; #ifndef HEADERFILE_H_ #define HEADERFILE_H_ void function1(); #endif /* HEADERFILE_H_ */ int main(){ function1(); cout<<"this is main function from mainFile"<<endl; return 0; } 

Now, here definition of function1() is still unknown to the compiler. How is this definition resolved ? Kindly explain.

8
  • 4
    The declaration (signature) of the function becomes known, so the code can be compiled. Once each .cpp file has been compiled, they get linked together into the final progam. This is when all the function definitions are matched up. Commented Jan 6, 2016 at 8:06
  • Please give some more explanation Commented Jan 6, 2016 at 8:08
  • I emphasised the key terms to aid your research. I don't have time at this second to write up a full answer. Commented Jan 6, 2016 at 8:09
  • 1
    @enhzflep Oops I really didn't meant to be that harsh :( (I really didn't) . I actually said this way because BobTFish said that he didn't had much time and so had I asked him one more question(""Swapping "you" and "can" in addition to adding a question mark (?) at the end "") he might have got offended. That's why I said it this way. Had I the chance now, I would have surely edited my comment :D. No hard feeling man. And thanks, I'll surely take care next time. Commented Jan 6, 2016 at 8:46
  • 1
    @Brut3Forc3 - No problem, I didn't (ever) think that to be your intended meaning, hence what was supposed to be a joke, along with my explanation and finally, mention of languages. All the best. :) Commented Jan 6, 2016 at 9:02

3 Answers 3

3

The function definition is still unknown to compiler at the time of compilation of main.cpp (where it becomes main.obj or simillar depending on the compiler/platform). It just has to know the signature of that function (its name, return value, parameters), provided by declaration in headerFile.h. In the compiled object file there will be written (by machine language).

here, the user is calling some function1, please, linker, find its address and insert it there.

Now, when you compile file1.cpp into file1.obj, file1.obj will contain compiled function1.

Finally, when you link these two files into executable (or library), linker will resolve all issues of calling only declared (but not defined) functions. So the quoted part will become

here, the user is calling the function1, and it's definition is located at 0x0123ABC.

If you do, however, provide object files with function calls, which can't be resolved, you'll get a linker error (something like)

/tmp/ccwckoGI.o: In function main:
/home/user/file.cpp:5: undefined reference to foo()

Sign up to request clarification or add additional context in comments.

6 Comments

isn't this a very time taking process.. first compile all files then find all functions that are not defined in main.cpp file and then find definitions to those functions in all files compiled(it would take really long time if number of files are large ~100).
@Brut3Forc3 Now, it's not - it's precisely what is happening during every linking of a program/library. This is simply how linking works. Although, of course, linking large binaries can take tens of seconds. But then again, compiling them usually takes tens of minutes.
It takes quite some time (I have not done any measurement), but still, it should be faster that lexical analysis of each source file.
Sorry I'm still unable to understand how linking function definition and function declaration works. A simple explanation on the above would be appreciated :)
@Brut3Forc3 "How does C linker work" is quite googlable.
|
1

The header file - headerFile.h contains the declaration of the function, and must be accompanied by a source file - headerFile.cpp.

Now, when you do a #include "headerFile.h" the declaration of the function becomes known and so the code can be compiled. At this point - when main.cpp is compiled, main.obj doesn't have definitions of the function. After each .cpp file has been compiled and .obj files are made, they get linked together, by the linker to give you the program.

4 Comments

Basically if I'm not wrong the linker searches every file in that project to match the function definition and once it finds it, it links function declaration to definition, and starts searching for another function definition until all declarations are resolved.
It links all the object files by replacing the references to undefined symbols with the correct addresses. Each of these symbols can be defined in other object files or in libraries. If they are defined in libraries other than the standard library, you need to tell the linker about them.
No, it doesn't do that on its own - you usually have to do it through a Makefile or your IDE, but you can also do the linking in your compilation statement.
I meant that during linking process, the linker searches every file to match the function declaration to the function definition and keeps searching until it finds it. Is this how it works ?
0
// headerFile.h #ifndef HEADERFILE_H_ #define HEADERFILE_H_ // add extern to tell the compiler that this function definition is in other file, yes it is in file1, complied into file1.obj extern void function1(); #endif /* HEADERFILE_H_ */ 

The linker will find function1()'s definition in the file1.obj. You should add to your build command of course.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.