开发者

How do I include everything

I've been fig开发者_StackOverflow中文版hting 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


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


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜