Force Source File Compile In C++

Started by
9 comments, last by Zahlman 15 years, 11 months ago
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?
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.
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.
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?
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.
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?

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.
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.
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.
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.

This topic is closed to new replies.

Advertisement