相关疑难解决方法(0)

C/C++包括头文件顺序

应该指定哪些顺序包含文件,即将一个标题包含在另一个标题之前的原因是什么?

例如,系统文件,STL和Boost是在本地包含文件之前还是之后运行?

c c++

265
推荐指数
8
解决办法
10万
查看次数

Explicit direct #include vs. Non-contractual transitive #include

Say we have this header file:

MyClass.hpp

#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 …

c++ header include c++17

20
推荐指数
3
解决办法
1470
查看次数

预编译的标题?我们真的需要它们吗?

很久以前我曾经使用过预编译的头文件:a.加快编译速度和b.因为我支持多种开发工具,如CodeWarrior,MPW,VS,ProjectBuilder,gcc,intel编译器等等.

现在我有一台带有32GB RAM的Mac Pro.

现在我只使用CMake.

那么我们真的需要预先编译的头文件吗?

我有没有看到/知道明显的好处?

如何制作跨平台的预编译头?也许这会简化我的生活.

c++ precompiled-headers c-preprocessor

12
推荐指数
2
解决办法
8630
查看次数

当我使用这样的头文件时,如何停止#include冗余头?

所以我仍然习惯于模块化编程,并希望确保我坚持最佳实践.如果我有下面的两个模块头文件,是否会#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)

c compiler-construction

3
推荐指数
1
解决办法
2367
查看次数