应该指定哪些顺序包含文件,即将一个标题包含在另一个标题之前的原因是什么?
例如,系统文件,STL和Boost是在本地包含文件之前还是之后运行?
Say we have this header file:
#pragma once
#include <vector>
class MyClass
{
public:
MyClass(double);
/* ... */
private:
std::vector<double> internal_values;
};
Run Code Online (Sandbox Code Playgroud)
Now, whenever we use #include "MyClass.hpp" in some other hpp or cpp file, we effectively also #include <vector>, despite the fact that we do not need it. The reason I am saying it is not needed is that std::vector is only used internally in MyClass, but it is not required at all to actually interact …
很久以前我曾经使用过预编译的头文件:a.加快编译速度和b.因为我支持多种开发工具,如CodeWarrior,MPW,VS,ProjectBuilder,gcc,intel编译器等等.
现在我有一台带有32GB RAM的Mac Pro.
现在我只使用CMake.
那么我们真的需要预先编译的头文件吗?
我有没有看到/知道明显的好处?
如何制作跨平台的预编译头?也许这会简化我的生活.
所以我仍然习惯于模块化编程,并希望确保我坚持最佳实践.如果我有下面的两个模块头文件,是否会#included多次包含每个文件的标题(例如"mpi.h")?有没有正确的方法来解释这个?
此外,我的模块标题通常看起来像这些示例,因此任何其他批评/指针都会有所帮助.
/* foo.h */
#ifndef FOO_H
#define FOO_H
#include <stdlib.h>
#include "mpi.h"
void foo();
#endif
Run Code Online (Sandbox Code Playgroud)
和
/* bar.h */
#ifndef BAR_H
#define BAR_H
#include <stdlib.h>
#include "mpi.h"
void bar();
#endif
Run Code Online (Sandbox Code Playgroud)
并使用示例程序:
/* ExampleClient.c */
#include <stdlib.h>
#include <stdio.h>
#include "mpi.h"
#include "foo.h"
#include "bar.h"
void main(int argc, char *argv[]) {
foo();
MPI_Func();
bar();
exit(0)
}
Run Code Online (Sandbox Code Playgroud)