Sunday 23 September 2012

Welcome post and annoying link error.

Finally I got some spare time to actually write something on my blog that I planned to do so almost a year ago. In the meantime I was able to get my bachelors degree in computer science (actually last Friday) and finished the accompanying project. The title of the thesis is "Design and implementation of a 2D game engine" and the project name was LAME engine. This might give a hint to what kind of posts you might expect to find here in a near future.

I am planning to publish some stuff directly from my thesis in a little modified form, as there were a couple of interesting ideas presented. In addition I am currently working on a brand new project that I am really excited about and have a lots of ideas, so the ground to cover with my programming experiments is quite large and I will not hesitate to share with you with my results.

Finally as a little bonus to this introductory post I would like to share with you, a rather unusuall (at least for me) error that cost me at least 3 hours of scratching my head, tearing the code upside-down and shuffling includes back and forth.

All I got was numerous susspicious linker errors saying about Unresolved external symbol. We all had to struggle with them at some point, but I consider myself a person after his share of fights with these errors and I was able to fix such very quickly. Well not this time. In addition what was even more strange was that cleaning the solution and rebuilding gave sometimes different Unresolved external symbol! It seemed as these were random. The structure, the includes etc. everything seemed perfectly fine (and to point out, they were perfectly fine) so it drove me insane. These unresolved symbols appeared always in one of two files in the project and they appeared after the new was actually added yesterday, but these files were not even connected at all.

Things between me and the linker got intense. As I was about to choke him to death I realized that I should carefully read the output for any additional clues. In the lengthy log file I found a hint:
warning LNK4042: object specified more than once; extras ignored

This was Types.obj, so I went back looking for places where I could have broken include guards, or whatnot. 

After some time I finally realized that the problems started when I introduced a new files (Types.h again) while merging my network library into the existing, larger, solution. Although, everything lived in its own folder and I had filter hierarchy in Visual Studio, somehow this was the problem.

So I had core/Types.h and network/Types.h somehow colliding. The solution was really easy but quite tricky to find out.

As it seems, Visual Studio flattens the whole hierarchy when producing output files by default. Therfore, whatever file (Types.h) was compiled first it was used everywhere, even where it shouldn't have been used.

Probably one way to fix that would be change the filename to something like NetTypes.h and fix #include statements everywhere in the code, but it was easier to explicitly say the linker to output files in their rightful place in the hierarchy.

To do that all I had to do was:
 Go into project properties -> C/C++ ->  Output Files -> Object File Name

and change the default $(IntDir)/ to $(IntDir)/%(RelativeDir)/


Hope It will help someone having similar strange issues.