h 和 hpp 问题,包含顺序错误

发布于 2024-11-25 02:40:04 字数 2023 浏览 0 评论 0原文

我正在编译一个包含超过 500 个类的大型项目。编译VS 2010后没有出现任何问题。在 Windows 下使用 g++ (Code::Blocks/Netbeans) 时,代码将无法编译并出现以下错误:

library/source/algorithms/file/../../algorithms/graph/../algebra/../spheredistance
/file.hpp:31:51: fatal error: ../../exception/ErrorOverflow.h: No such file or directory. 
Compilation terminated.

但是,该文件存在于指定路径中。 Linux 版本工作正常。 路径中的字符 / 或 \ 并不重要(已测试)。

如果我更改包含文件的顺序,上述错误就会消失,并且代码中的其他位置会出现类似的错误...

我认为代码中的某个位置存在循环依赖性或包含文件的顺序错误。

文件结构:

1) .cpp 文件

#include "file.h"

2) .h 文件

#ifndef file_H
#define file_H

template <typename T>
class Class 
{
};

#include "file.hpp"
#endif

3) .hpp 文件

#ifndef file_HPP
#define file_HPP

#include "../../somefile.h"

template <typename T>
class Class 
{

};

#endif

大多数头文件 *.h 都包含在 *.hpp 文件中,但在某些 *.h 文件中需要包含另一个 *.h文件。一个简短而简化的示例,说明结果的舍入/舍入:

Orientation.h
#ifndef Orientation_H
#define Orientation_H


typedef enum
{
    RoundOn, RoundOff
} TRound;


class Orientation
{
    public:
            template <typename T>
            static short getOrientation( const T dx1, const T dy1, const T dx2, const T dy2, const TRound round = RoundOff );
};

某些类位置:方法给出舍入/舍入结果

#include "Orientation.hpp"

Position.hpp
#ifndef Position_H
#define Position_H

#include "../orientation/Orientation.h"  //must be included for Rounding


class Position
{
    public:
            template <typename Point1, typename Point2>
            static unsigned short getPosition ( const Point1 * p, const Point2 * p1, const Point2 * p2, const TRoundType round );
};

#include "Position.hpp"

#endif

请告知:

  • 哪个标头/包含策略适合如此大的项目。
  • 如何找到包含错误顺序的标题(如果可能)
  • 如何重新组织该项目以避免上述问题。

更新结果:

感谢大家的有用建议。

对不起,我完全错了。错误实际上是在相对路径中包含字符..(双点)

重写所有包含指令后一切正常。

I am compiling a large project containing >500 classes. After compiling VS 2010 there have been no problems. When using g++ (Code:: Blocks / Netbeans) under Windows the code will not compile with the following error:

library/source/algorithms/file/../../algorithms/graph/../algebra/../spheredistance
/file.hpp:31:51: fatal error: ../../exception/ErrorOverflow.h: No such file or directory. 
Compilation terminated.

However, this file exists in the specified path. Linux version works correctly.
Character / or \ in the path does not matter (tested).

If I change order of included files, the above mentioned error disappears and a similar error appears elsewhere in the code...

I think that somewhere in the code there is a cyclic dependency or wrong order of included files.

File structure:

1) .cpp file

#include "file.h"

2) .h file

#ifndef file_H
#define file_H

template <typename T>
class Class 
{
};

#include "file.hpp"
#endif

3) .hpp file

#ifndef file_HPP
#define file_HPP

#include "../../somefile.h"

template <typename T>
class Class 
{

};

#endif

Most of header files *.h are included in *.hpp files, but in some *.h files there is a need for including another *.h file. A short and simplified example illustrating rounding on/off of the result:

Orientation.h
#ifndef Orientation_H
#define Orientation_H


typedef enum
{
    RoundOn, RoundOff
} TRound;


class Orientation
{
    public:
            template <typename T>
            static short getOrientation( const T dx1, const T dy1, const T dx2, const T dy2, const TRound round = RoundOff );
};

Some class Position: method gives round on / round off results

#include "Orientation.hpp"

Position.hpp
#ifndef Position_H
#define Position_H

#include "../orientation/Orientation.h"  //must be included for Rounding


class Position
{
    public:
            template <typename Point1, typename Point2>
            static unsigned short getPosition ( const Point1 * p, const Point2 * p1, const Point2 * p2, const TRoundType round );
};

#include "Position.hpp"

#endif

Please advise:

  • which header/include policy is suitable for such a large project.
  • how to find headers with the wrong order of including (if it is possible)
  • how to reorganize this project to avoid above mentioned problems.

UPDATED RESULTS:

Thank you all for your helpful advice.

I am sorry, I was completely wrong. A bug was actually in relative path containing characters .. (double dot)

After rewriting all include directives is everything OK.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许一世地老天荒 2024-12-02 02:40:04

但是这个文件存在于指定的路径

不,不存在。

尽管您认为循环包含可能会导致一些误导性的错误消息,但这不是其中之一。

需要记住的一件事是,包含路径的分辨率是由实现定义的 (2003:12.8/1),这可能就是您看到工具链之间不一致的原因。相对路径的使用特别令人好奇。简化你的源代码树和包含路径,这个问题就会消失。

However, this file exists in the specified path

No, it does not.

Although you're right in that cyclic inclusion can lead to some misleading error messages, this is not one of them.

One thing to bear in mind is that the resolution of inclusion paths is implementation-defined (2003:12.8/1), which may be why you're seeing inconsistency across toolchains. The use of relative paths is particularly curious. Simplify your source tree and inclusion paths, and this problem will go away.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文