Jump to content
  • Advertisement
Sign in to follow this  
nick112147

Unity multiple .cpp files in a single project

This topic is 4029 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was tooling around the ZDoom source code and I noticed it had a boat load of .cpp files that just contained the guts to the functions etc. It seems to me like all of that could be in a .h/.hpp file, and achieve the same effect. UNLESS using the .cpp extension causes the compiler to treat the source differently. Could someone educate me in this subject, please! Note that ZDoom does use MSVC. For instance would the following function behave or compile differently if it were in a .cpp or in a .h file: void print_hello (void) { printf("Hello World"); } I just don't see the difference, but I am sure there will be a world of difference. Note that ZDoom is open source, so don't flame me over having "top secret" source code lol, I was in one forum [name shall be omitted] and some putz gave me a serious flaming because he swore up and down ZDoom source is Id's property and is not supposed to be GPL, when it is in fact a source PORT by Randy Heit from the original linux source port GLPed by ID back in the day. I'm not trying to be a putz myself, but it just seems like unless one spent some time in the retro source code community, people just make tooooo many assumptions lol ;-). Fortunately i wasn't banned because the forum moderator was savvy to ZDoom. Thanks in advance, Nick.

Share this post


Link to post
Share on other sites
Advertisement
Header (.h/.hpp) files DO NOT GET COMPILED - think of them as "just text, not code".

only source (.c/.cpp) files are compiled. This compilation results in an intermediate file (.o/.obj/etc...). These intermediate files are then linked into executable code (.exe/.dll/etc...).

(When a source file is compiled, the compiler basically copy/paste's all of the text from the header files into the source code)


If you put functions into a .h file, and that .h is included into several different .cpp files, then the code for that function will end up in several .o files. When this happens, the linker won't know which duplicate copy of the function to use, and won't be able to continue.


The function prototype (e.g. "void doStuff();" ) belongs in a header, but the actual implementation (e.g. "void doStuff() { ... }" ) belongs IN A SINGLE CPP file.


In other words, if you write function implementations inside headers, then you're basically writing the same function in multiple source files - and that is wrong!!


So to answer the question about your example - if that code is in a header, then it's ok as long as that header is only ever included in one source file. If that header is included in multiple source files, then you will experience link errors. So just don't put function bodies (implementations) in headers!

The exception to this is if the function is 'inline' or a 'template', but that's another story.

[edit]
Further reading...

Share this post


Link to post
Share on other sites
AHA! I pretty much thought it would be something like that. I just wanted to be sure, thanks a bunch man, you probably saved me renaming ALOT of files.


Nick

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!