Jump to content
  • Advertisement
Sign in to follow this  
fathom88

Force Source File Compile In C++

This topic is 3665 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

Is there a way to force the compile order of c++ source files? I know it can by done in the compiler. Is there a way to give a hint to the compiler in the source file itself?

Share this post


Link to post
Share on other sites
Advertisement
No, because the file will not be read by the compiler until it is compiled.

Either way, the compile order of C++ source files is irrelevant to the functionality of the program, so I doubt that you would need this anyway.

Linking order might be of more interest to you.

Share this post


Link to post
Share on other sites
Good point. How would one change the link order of objects? I would assume you can only do this in the compiler. Thanks for the help.

Share this post


Link to post
Share on other sites
Quote:
Original post by fathom88
Good point. How would one change the link order of objects? I would assume you can only do this in the compiler. Thanks for the help.


The way to specify link order is an implementation detail. What tools are you using?

Share this post


Link to post
Share on other sites
Quote:
Original post by fathom88
Good point. How would one change the link order of objects? I would assume you can only do this in the compiler. Thanks for the help.


Why do you think you need to, or care about it? It is probably a sign you are doing something wrong.

Share this post


Link to post
Share on other sites
What I do is define my object in one big file. Let's say I have a BlueDevilCharacter.cpp. In the cpp, I would look like this.

class BlueDevil : public Character
{
//filled with stuff
public:
void AddToWorld();
};

BlueDevil::BlueDevil()
{

}
BlueDevil::~BlueDevil()
{
}

void BlueDevil::AddToWorld()
{
//world is a singleton
//add character is static function
//that stores a static list of characters
CWorld::AddCharacter(this);
}

//define anything else

BlueDevil ABlueDevilTypeCharacter;

ABlueDevilTypeCharacter.AddToWorld();
//blue devil type character is now known and
//can be used to generate one or more blue devils
//using the world singleton

Is this good practice?

Share this post


Link to post
Share on other sites
Quote:
Original post by fathom88
Is this good practice?


No. You should avoid using global objects for many reasons. One of them (and probably the reason for this thread) is that you cannot predict in what order the constructors will run. Another is that it leads to code that is hard to read, debug, and maintain.

Share this post


Link to post
Share on other sites
Sure can define the order. Just define all your global obects in one file. In MyGlobalObject.cpp

CWorld DesertHell;

BlueDevil BlueDevilTypeCharacter;
BlackDragon BlackDragonTypeCharacter;
.....

I'm kidding of course. You make a good point. I've been doing my objects this way because someone told me it would be easier to maintain my code. She said I would be able to drop and add cpp files into my project as needed. If I created another project, I could simply add the BlueDevil character cpp to my project and it would be availble. If I didn't want to be able to generate BlueDevils, I could simply remove the cpp and I would not have to change any code.

Share this post


Link to post
Share on other sites
Believe it or not, this is actually one of the hardest things about using C or C++ for larger projects. A lot of things that seem like really good ideas (like creating "master" files that include all of your headers) actually turn in to nightmares further down the road.

Also, most C++ books barely mention these issues at all focusing instead on language features. This article goes over how best to organize your code files. It should be mandatory reading for intermediate C and C++ programmers.

Share this post


Link to post
Share on other sites
As I'm seeing it, he wants the other translation units to know of some class, so he thinks he should force them in the right order.

My advice therefore would be to read up on header files and learn how to use them.

I could be wrong altogether too.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!