Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


Another ''alredy defined in .obj'' error


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 mrheisenberg   Members   -  Reputation: 356

Like
0Likes
Like

Posted 12 September 2012 - 12:13 PM

I have a ''Utility.h'' header that has 2 functions in it(fully defined with bodies and everything,since I won't be changing them anytime soon).Once I included them in my .cpp files,it gave me an error for each .cpp file like:

void_cdecl MyFunction(char const*, char const*, unsigned int) (?MyFunction@@YAXPBD00GI@Z) alredy defined in MyModule.obj.

Every header file,including Utility.h has 2 include guards(pragma comment and ifndef) + it's only included in the cpp files,I don't understand why it would still give such an error.None of my other headers give such errors.

Sponsor:

#2 powly k   Members   -  Reputation: 653

Like
0Likes
Like

Posted 12 September 2012 - 02:49 PM

First your code is compiled. That means that every .cpp file is turned into a .obj file that contains machine language versions of all your code. Then your .obj files are linked. In this process, the linker checks what functions and variables your program needs and puts them into your .exe file. So, what happened here is you had two identical functions in two separate .cpp files (by inclusion) which in turn creates two identical functions in your .obj files. This confuses the linker because it doesn't know which one to choose so it throws an error. So, what you want to do is only declare your functions in your header and define the body in a single .cpp file. This causes the compiler to not throw errors because it knows what your functions look like but their bodies will be defined only once so you won't confuse the linker. If you won't be changing the code, just don't touch it. You can copy the .cpp file around into your next projects and whatnot, but always keep them in a separate file.

#3 BinaryPhysics   Members   -  Reputation: 294

Like
0Likes
Like

Posted 12 September 2012 - 03:05 PM

I was of the thought that as long as the code was completely identical it was okay. Did I read that wrong somewhere?

#4 SiCrane   Moderators   -  Reputation: 9673

Like
0Likes
Like

Posted 12 September 2012 - 07:39 PM

You can define functions in headers if they are explicitly or implicitly inlined. You explicitly inline them with the inline keyword. Implicitly inlined functions include member functions defined in class definitions and template functions. Inline functions must be identical in every translation unit they are defined in, otherwise bad juju happens.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS