What are some techniques for limiting compilation dependencies in C++ projects?

孤街醉人 提交于 2019-12-19 07:14:12

问题


In a C++ project, compilation dependencies can make a software project difficult to maintain. What are some of the best practices for limiting dependencies, both within a module and across modules?


回答1:


  • Forward Declarations
  • Abstract Interfaces
  • The Pimpl Idiom



回答2:


Herb Sutter has a great treatment of this exact topic in Items 26, 27 and 28, "Minimizing Compile-time Dependencies, Parts 1, 2 and 3", in his excellent book Exceptional C++, ISBN: 0201615622.

alt text http://ak.buy.com/db_assets/prod_images/489/30611489.jpg

IMHO, this is one of the best C++ programming books available.




回答3:


I think you need to be very careful and considerate about this. Generally, you can limit dependencies by separating the code and using abstract interfaces (eg: function pointers or an object equivalent), but separation generally adds fragility. For example, you can call a module through a generic abstract interface to reduce the dependency on the actual object implementation, but you have to update the interface in sync with the object itself, or the code will fail at run-time.

I would say it's important to structure large projects in modules with a well-defined hierarchy, but within each module don't go overboard with breaking apart code to limit dependencies. If you're going for improved maintenance, you have to balance reducing dependencies with reducing code fragility.




回答4:


Also take a look at:

Large-Scale C++ Software Design (Addison-Wesley Professional Computing Series)



来源:https://stackoverflow.com/questions/188449/what-are-some-techniques-for-limiting-compilation-dependencies-in-c-projects

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!