Word from the Author

"One mans mistake is another mans break"
Most of the article(s) which I have posted here are based on my own personal experience. Please do not get aggravated if you disagree with what I wrote.This is just my opinion and it might not be worth much. In case you find some errors or mistakes , have any other useful information which others including myself would benefit from or if you like an article you can always post your messages and comments here.

C++ Linker Error : One or more Multiply defined symbols found

One of the most frustrating messages which you can get while building your project is a linker error.When I first came across this error , I was told that a bit of code re factoring in my project was required . Later on I realized that this error was being produced due to my unfamiliarity to the C++ Build Process. I have provided a little overview on how the build process works and then a very short example demonstrating how we could tackle such a problem. The solution would be worth the read for both firmware and software developers in the long run. Why ? I'll get to that later , but for now lets get started with the Build Process

Understanding C++ Build Process:
Although the C++ Build Process is fairly complex . Incase you are still interested have a look here . However the following diagram shows the major steps which the IDE takes here in order to build our application from meager .cpp files to a fully functional .exe/dll file .


Creating A Simple Linker Error Scenario :
The following diagram displays a very simple scenario and also includes a method on overcoming the linker error.
 















Summary:
Only .cpp files are preprocessed and then converted to individual .obj files.Hence In order to solve the problem of “Multiply defined files”. The functions in header files are only prototyped , and the definition of those functions are placed in individual .cpp files. The compiler while preprocessing the CPP file inserts the entire header into that space of the cpp file and since multiple prototype declarations is possible hence the link time error is resolved and since all the functions from the header files are in a single cpp they get registered only once..so no link time errors.

No comments:

Post a Comment