【问题标题】:Can't include <gl/gl.h>不能包含 <gl/gl.h>
【发布时间】:2011-04-15 05:27:28
【问题描述】:

我正在使用 Visual Studio 2010。 我正在尝试在 OpenGL 中编写简单的 Camera 类。 我需要在 Camera.h 中包含 gl/gl.h
gl/gl.h 已包含在 main.cpp 中,Camera.h 已包含在 main.cpp 中 当我把

#include <gl/gl.h>

在 Camera.h 中,我遇到了很多类似这样的错误:
Error 11 error C2086: 'int APIENTRY' : redefinition C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\include\gl\GL.h 1153 1 Gaz 3D

文件:
相机.h

#include <math.h>
#include <gl/gl.h>

#ifndef _CAMERA_H
#define _CAMERA_H

class Camera
{
private:
    Camera();
public:
    static Camera& getCamera();
    float x, y, z, rotv, roth;
    void moveForward(float n);
    void moveBackward(float n);
    void moveLeft(float n);
    void moveRight(float n);
    void lookUp(float n);
    void lookDown(float n);
    void lookLeft(float n);
    void lookRight(float n);
};

#endif

main.cpp:

#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include <math.h>
#include "Camera.h"

// ... some variables ...

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,  
                   LPSTR lpCmdLine, int nCmdShow)
{
    // main code ...
}

我做错了什么?

【问题讨论】:

  • 我不认为这会导致您的问题,所以我不会将其作为答案发布:#include &lt;math.h&gt;#include &lt;gl/gl.h&gt; 应该在相机中的 #ifndef 保护内。 H。再三考虑,您甚至根本不需要该标题中的那些包含;你在 Camera 的声明中没有使用他们的任何东西。
  • 我不会,但我会使用数学和 gl。

标签: opengl include redefinition


【解决方案1】:

只需先包含 windows.h。

#include <windows.h>

正如 OpenGL 常见问题解答中所说:

另外,请注意,您需要在 #include&lt;GL/gl.h&gt; 之前放置一个 #include &lt;windows.h&gt; 语句。 Microsoft 要求系统 DLL 使用特定的调用约定,这不是大多数 Win32 C 编译器的默认调用约定,因此他们在 gl.h 中使用一些扩展为非标准 C 语法的宏来注释 OpenGL 调用。这导致 Microsoft 的 C 编译器使用系统调用约定。 windows.h 包含的包含文件之一定义了宏。


资源:

【讨论】:

    【解决方案2】:

    编辑:显然 Colin Hebert 解决了这个问题,但作为一般提示,我想说:

    在Camera.h中写

    #ifndef _CAMERA_H
    #define _CAMERA_H
    

    高于所有其他包括。 并将 .cpp 文件所需的所有头文件包含在 .h 文件中。

    至少我认为这是最佳实践。

    【讨论】:

    • 这应该没有区别。 GL 标头也有一个包含保护。
    • 这只是一般提示。
    • 如果不是为了解决用户的问题,你应该这么说。事实上,听起来你在回答他的问题,而答案是错误的。
    • @LarsH 好的,抱歉,照你说的做了。
    猜你喜欢
    • 2013-07-03
    • 2016-02-18
    • 2012-05-04
    • 2011-08-11
    • 2014-03-13
    • 2012-01-24
    • 2020-03-06
    • 1970-01-01
    • 2010-09-30
    相关资源
    最近更新 更多