Brian
Brian

Reputation: 331

How do I include everything

I've been fighting with my compiler for too long. Problems with circular includes, redefinitions, "missing ';' before *" and so on.

This seems like the place to get a good answer.

How do I include everything into everything else, and never have to worry about the subtleties of includes ever, ever again? What combination of #DEFINE, #pragma, #include, or whatever else do I need to do to ensure that data types in the murky depths of my project hierarchy will have no difficulty knowing what anything else is?

This is not a troll post, incase such a concept is entirely unthinkable, nor is it posted in the middle of being angry. I'm simply curious as to whether or not such a possibility exists. Dealing with spaghetti includes is probably the biggest headache I have to deal with in C++, and getting rid of it would increase my workflow significantly.

Cheers, Brian

Upvotes: 2

Views: 1949

Answers (3)

xjdrew
xjdrew

Reputation: 383

in the beginning of every header file,

#ifndef   __MYHEADER_H__
#define   __MYHEADER_H__

in the end,

#endif /* __MYHEADER_H__ */

can avoid contain a header file repeatedly.

if you use visual stuido, you can just put

#pragma once

in the beginning of header file.

By the way, you can use some static code check tool to find these kinds of issues, such as lint.

Upvotes: 0

Hoàng Long
Hoàng Long

Reputation: 10848

Good question. I would like to know how to do this, too. Here's some of my tricks:

  1. Figuring the file hirechary structure (which file use which file) to draw a raw concept graph.

  2. Using the following code structure is helpful for prevent RE-DEFINING. Details can be found here.

#ifndef MY_CLASS

#define MY_CLASS 
#endif

It means if the file is already included, it will not be included again.

Upvotes: 0

Jason Dietrich
Jason Dietrich

Reputation: 155

Forward deceleration in the headers and inclusions in the implementation (.c, .cpp, etc).

Upvotes: 3

Related Questions