本文介绍了错误LNK2019:在函数"void __cdecl init(void)"中引用了未解析的外部符号_LoadShaders. (?init @@ YAXXZ)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习OpenGL,并尝试运行我的第一个程序.我已将所有文件包括在include,lib和bin文件夹中.我试图添加opengl32.lib; glut32.lib; glu32.lib;在配置属性"->链接器"->输入"中,但是它也不起作用.

I am learning OpenGL and trying to run my first program. I have included all the files in include, lib, and bin folders. I have tried to add opengl32.lib;glut32.lib;glu32.lib; in Configuration properties -> linker -> input, but it did't work too.

我正在使用Visual Studio 2012.

I am using Visual Studio 2012.

///////////////////////////////////////////////////////////////////////
//
// triangles.cpp
//
///////////////////////////////////////////////////////////////////////

#include <iostream>
using namespace std;

#include <vgl.h>
#include <LoadShaders.h>

enum VAO_IDs { Triangles, NumVAOs };
enum Buffer_IDs { ArrayBuffer, NumBuffers };
enum Attrib_IDs { vPosition = 0 };

GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];

const GLuint NumVertices = 6;

//---------------------------------------------------------------------
//
// init
//

void
init(void)
{
    glGenVertexArrays(NumVAOs, VAOs);
    glBindVertexArray(VAOs[Triangles]);

    GLfloat vertices[NumVertices][2] = {
        { -0.90, -0.90 }, // Triangle 1
        { 0.85, -0.90 },
        { -0.90, 0.85 },
        { 0.90, -0.85 }, // Triangle 2
        { 0.90, 0.90 },
        { -0.85, 0.90 }
    };

    glGenBuffers(NumBuffers, Buffers);
    glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),
                vertices, GL_STATIC_DRAW);

    ShaderInfo shaders[] = {
        { GL_VERTEX_SHADER, "triangles.vert" },
        { GL_FRAGMENT_SHADER, "triangles.frag" },
        { GL_NONE, NULL }
    };

    GLuint program = LoadShaders(shaders);
    glUseProgram(program);

    glVertexAttribPointer(vPosition, 2, GL_FLOAT,
    GL_FALSE, 0, BUFFER_OFFSET(0));

    glEnableVertexAttribArray(vPosition);
}

//---------------------------------------------------------------------
//
// display
//

void
display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glBindVertexArray(VAOs[Triangles]);
    glDrawArrays(GL_TRIANGLES, 0, NumVertices);

    glFlush();
}

//---------------------------------------------------------------------
//
// main
//

int
main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA);
    glutInitWindowSize(512, 512);
    glutInitContextVersion(4, 3);
    glutInitContextProfile(GLUT_CORE_PROFILE);
    glutCreateWindow(argv[0]);

    if (glewInit()) {
        cerr << "Unable to initialize GLEW ... exiting" << endl;
        exit(EXIT_FAILURE);
    }

    init();

    glutDisplayFunc(display);

    glutMainLoop();
}

推荐答案

除非该文件只是标头,否则您将缺少该LoadShaders()文件的lib(或cpp)文件.不知道其内容,我们不能肯定地说.

You're missing the lib (or cpp) file for that LoadShaders() thing unless it's header only. Without knowing its contents we can't say that for sure.

更新:

包含书源代码的zip文件包含您需要的所有内容.

The zip file with the book's source code includes everything you need.

您应该执行以下操作:

  • 将zip文件提取到易于记忆的位置,例如"c:\ openglbook".
  • 在Visual Studio 2012中打开或创建您的项目.
  • 打开菜单项目",然后选择"[您的项目]属性..."(或按 + ).
  • 为每个配置重复以下步骤,或选择左上角的所有配置".
  • 在左侧的树中,打开配置属性",然后选择"VC ++目录".
  • c:\openglbook\include添加到包括目录".
  • c:\openglbook\lib添加到图书馆目录".
  • 关闭项目属性.
  • 将文件"c:\ openglbook \ lib \ LoadShaders.cpp"复制到您的项目文件中,并将其添加为另一个源文件.
  • Extract the zip file to an easy to remember location, e.g. "c:\openglbook".
  • Open or create your project in Visual Studio 2012.
  • Open the menu "PROJECT" and pick "[your project] Properties..." (or hit +).
  • Repeat the following steps for each of your configurations or select "All Configurations" on the top left.
  • In the tree on the left open "Configuration Properties" and select "VC++ Directories".
  • Add c:\openglbook\include to "Include Directories".
  • Add c:\openglbook\lib to "Library Directories".
  • Close the project properties.
  • Copy the file "c:\openglbook\lib\LoadShaders.cpp" to your project files and add it as another source file.

完成后,您的项目应该构建.如果仍然缺少依赖项(例如GLUT函数),则将"lib"子目录中的适当库添加到您已经知道的链接器库列表中.

Once this is done your project should build. If you're still lacking dependencies, like GLUT functions, add the apropriate library from the "lib" sub directory to the linker libraries list you already know.

这篇关于错误LNK2019:在函数"void __cdecl init(void)"中引用了未解析的外部符号_LoadShaders. (?init @@ YAXXZ)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 02:28