OpenGL 期间的链接器错误:SuperBible 教程

发布于 2024-09-30 18:11:51 字数 3563 浏览 1 评论 0原文

我目前正在尝试通过对 DirectX 10 和 OpenGL 3.3 进行一些编程来在 DirectX 和 OpenGL 之间做出决定。我已经完成了 DirectX 的设置,链接和编译相当容易。 OpenGL 更难。

OpenGL Superbible 有一个名为 Triangle.cpp 的起始示例,其中我们链接了两个库 freeglut_static.libGLTools.lib。这不是问题;我还转到了项目目录,并包含了所有必需的 OpenGL 扩展的 Include/ 和 lib/ 路径(GLEE、Glew、Glut、FreeGlut、GLTools——该死的,这够了吗?)。

首先,我遇到了几个链接器错误,因为我的代码生成是在 DLL 上设置的,而不是静态的。我已经修复了这个问题,并将 LIBC.lib 添加到链接器中忽略的库列表中(不确定将代码生成设置为静态是否也修复了这个问题)。

现在我仍然有两个无法摆脱的链接器错误:

1>Triangle.obj : error LNK2019: unresolved external symbol ___glutInitWithExit referenced in function _glutInit_ATEXIT_HACK
1>Triangle.obj : error LNK2019: unresolved external symbol ___glutCreateWindowWithExit referenced in function _glutCreateWindow_ATEXIT_HACK

我在谷歌上搜索了这个问题,很多人评论了程序的静态性质(我已经修复了)以及之间版本冲突的特定问题Glut.h 和 Glut.lib。然而,我什至使用了旧版本的 Glut (3.6),链接器错误仍然存​​在。

谷歌中的其他搜索并没有真正找到任何合理的可用内容。所以,我在这里问:我该如何解决这个问题?

Info

代码生成:多线程

C++ 预处理器命令:FREEGLUT_STATIC

IDE:Visual Studio 2008 和 2010。(在两者上进行测试 - 相同的错误两者)

忽略的库:LIBC.lib

Triangle.cpp 代码(从书中代码简单复制/粘贴):

// Triangle.cpp
// Our first OpenGL program that will just draw a triangle on the screen.
#include <GLTools.h> // OpenGL toolkit
#include <GLShaderManager.h> // Shader Manager Class

//#define FREEGLUT_STATIC

#include <GL/glut.h> // Windows FreeGlut equivalent

GLBatch triangleBatch;
GLShaderManager shaderManager;
///////////////////////////////////////////////////////////////////////////////
// Window has changed size, or has just been created. In either case, we need
// to use the window dimensions to set the viewport and the projection matrix.
void ChangeSize(int w, int h)
{
    glViewport(0, 0, w, h);
}
///////////////////////////////////////////////////////////////////////////////
// This function does any needed initialization on the rendering context.
// This is the first opportunity to do any OpenGL related tasks.
void SetupRC()
{
    // Blue background
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f );
    shaderManager.InitializeStockShaders();
    // Load up a triangle
    GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f,
        0.5f, 0.0f, 0.0f,
        0.0f, 0.5f, 0.0f };
    triangleBatch.Begin(GL_TRIANGLES, 3);
    triangleBatch.CopyVertexData3f(vVerts);
    triangleBatch.End();
}
///////////////////////////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
    triangleBatch.Draw();
    // Perform the buffer swap to display the back buffer
    glutSwapBuffers();
}
///////////////////////////////////////////////////////////////////////////////
// Main entry point for GLUT based programs
int main(int argc, char* argv[])
{
    gltSetWorkingDirectory(argv[0]);
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Triangle");
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);
    GLenum err = glewInit();
    if (GLEW_OK != err) {
        fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
        return 1;
    }
    SetupRC();
    glutMainLoop();
    return 0;
}

I'm currently trying to decide between DirectX and OpenGL by programming a little DirectX 10 and OpenGL 3.3. I already have the setup for DirectX finished, it was fairly easy to link and compile. OpenGl is... harder.

The OpenGL Superbible has a beginning example called Triangle.cpp in which we link two libraries freeglut_static.lib and GLTools.lib. This isn't a problem; I have also gone to the Project Directories and included the Include/ and lib/ path of all necessary OpenGL extensions (GLEE, Glew, Glut, FreeGlut, GLTools- damn, is that enough?).

First I had several linker errors, because my code generation was set on DLL, not Static. I have fixed this and also added LIBC.lib to the list of ignored libraries in the Linker (not sure if setting the code generation to static fixed this as well).

Now I still have two linker errors remaining which I cannot get rid of:

1>Triangle.obj : error LNK2019: unresolved external symbol ___glutInitWithExit referenced in function _glutInit_ATEXIT_HACK
1>Triangle.obj : error LNK2019: unresolved external symbol ___glutCreateWindowWithExit referenced in function _glutCreateWindow_ATEXIT_HACK

I searched this problem on google, and many people commented on the static nature of the program (which I have fixed) as well as a particular problem with a conflicting version between Glut.h and Glut.lib. However, I even used an older version of Glut (3.6) and the linker error still remains.

Other searches in google don't really come up with anything reasonable to work with. So, I'm asking here: How do I fix this?

Info

Code Generation: Multithreaded

C++ Preprocessor Command: FREEGLUT_STATIC

IDE: Visual Studio 2008 and 2010. (Testing on both- same error on both)

Ignored Libraries: LIBC.lib

Triangle.cpp code (a simple copy/paste from the code in the book):

// Triangle.cpp
// Our first OpenGL program that will just draw a triangle on the screen.
#include <GLTools.h> // OpenGL toolkit
#include <GLShaderManager.h> // Shader Manager Class

//#define FREEGLUT_STATIC

#include <GL/glut.h> // Windows FreeGlut equivalent

GLBatch triangleBatch;
GLShaderManager shaderManager;
///////////////////////////////////////////////////////////////////////////////
// Window has changed size, or has just been created. In either case, we need
// to use the window dimensions to set the viewport and the projection matrix.
void ChangeSize(int w, int h)
{
    glViewport(0, 0, w, h);
}
///////////////////////////////////////////////////////////////////////////////
// This function does any needed initialization on the rendering context.
// This is the first opportunity to do any OpenGL related tasks.
void SetupRC()
{
    // Blue background
    glClearColor(0.0f, 0.0f, 1.0f, 1.0f );
    shaderManager.InitializeStockShaders();
    // Load up a triangle
    GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f,
        0.5f, 0.0f, 0.0f,
        0.0f, 0.5f, 0.0f };
    triangleBatch.Begin(GL_TRIANGLES, 3);
    triangleBatch.CopyVertexData3f(vVerts);
    triangleBatch.End();
}
///////////////////////////////////////////////////////////////////////////////
// Called to draw scene
void RenderScene(void)
{
    // Clear the window with current clearing color
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
    GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f };
    shaderManager.UseStockShader(GLT_SHADER_IDENTITY, vRed);
    triangleBatch.Draw();
    // Perform the buffer swap to display the back buffer
    glutSwapBuffers();
}
///////////////////////////////////////////////////////////////////////////////
// Main entry point for GLUT based programs
int main(int argc, char* argv[])
{
    gltSetWorkingDirectory(argv[0]);
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
    glutInitWindowSize(800, 600);
    glutCreateWindow("Triangle");
    glutReshapeFunc(ChangeSize);
    glutDisplayFunc(RenderScene);
    GLenum err = glewInit();
    if (GLEW_OK != err) {
        fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
        return 1;
    }
    SetupRC();
    glutMainLoop();
    return 0;
}

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

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

发布评论

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

评论(2

皓月长歌 2024-10-07 18:11:51

好吧,这就是答案:

我必须添加以下命令:

#pragma (lib, "freeglut_static.lib")
#pragma (lib, "gltools.lib")

将所需文件的库路径放入项目目录中后。之后,我还需要将 glut32.dll 包含在我的解决方案目录中(因为我也链接到 Glut32),然后它就起作用了。

希望这对某人有帮助!

Alright, here is the answer:

I had to add the following commands:

#pragma (lib, "freeglut_static.lib")
#pragma (lib, "gltools.lib")

after putting in the library path of the needed files in the Project Directories. After that I also needed to include glut32.dll in my Solution directory (as I had also linked to Glut32), and then it worked.

Hope this helps someone!

丑疤怪 2024-10-07 18:11:51

为了将来参考,您需要在链接器阶段提供要链接的库的名称。

例如,在 Visual Studio 中,您可以在

Project Properties->Linker->Input

“只需添加它们‘;’” 下找到它在“附加依赖项”字段中分隔。

就我个人而言,我喜欢 #pragma comment 解决方案,因为您不需要记住在其他依赖项下添加 .libs,您只需要在某处添加这些 #pragma 语句即可。

For future reference you need to supply the names of the libraries you want to link with at the linker stage.

In visual studio for example you'll find this under

Project Properties->Linker->Input

Just add them ';' delimited in the "Additional Dependencies" field.

Personally I like the #pragma comment solution though as you don't need to remember to add the .libs under additional dependencies you just need those #pragma statements somewhere.

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