LNK2019错误:未解析的外部符号。

13
当我想编译我的OpenGL代码时,出现以下错误:
Error   1   error LNK2019: unresolved external symbol __imp__glewInit@0 
Error   2   error LNK2019: unresolved external symbol __imp__glewGetErrorString@4 
Error   3   error LNK2001: unresolved external symbol __imp____glewAttachShader     
Error   4   error LNK2001: unresolved external symbol __imp____glewCompileShader    
Error   5   error LNK2001: unresolved external symbol __imp____glewCreateProgram    
Error   6   error LNK2001: unresolved external symbol __imp____glewCreateShader 
Error   7   error LNK2001: unresolved external symbol __imp____glewDeleteProgram    
Error   8   error LNK2001: unresolved external symbol __imp____glewDisableVertexAttribArray 
Error   9   error LNK2001: unresolved external symbol __imp____glewEnableVertexAttribArray  
Error   10  error LNK2001: unresolved external symbol __imp____glewGetAttribLocation    
Error   11  error LNK2001: unresolved external symbol __imp____glewGetProgramiv 
Error   12  error LNK2001: unresolved external symbol __imp____glewGetShaderiv  
Error   13  error LNK2001: unresolved external symbol __imp____glewLinkProgram  
Error   16  error LNK2001: unresolved external symbol __imp____glewVertexAttribPointer  
Error   17  error LNK1120: 16 unresolved externals  

我的代码是:

#include <Windows.h>
#include <iostream>
#include <glew.h>
#include <gl\GL.h>
#include <freeglut.h>

using namespace std;

GLuint program;
GLint attribute_coord2d;

int init_resources(void)
{
  GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;

  GLuint vs = glCreateShader(GL_VERTEX_SHADER);
  const char *vs_source = 
#ifdef GL_ES_VERSION_2_0
    "#version 100\n"  // OpenGL ES 2.0
#else 
    "#version 120\n"  // OpenGL 2.1
#endif
    "attribute vec2 coord2d;                  "
    "void main(void) {                        "
    "  gl_Position = vec4(coord2d, 0.0, 1.0); "
    "}";
  glShaderSource(vs, 1, &vs_source, NULL);
  glCompileShader(vs);
  glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_ok);
  if (0 == compile_ok)
  {
    fprintf(stderr, "Error in vertex shader\n");
    return 0;
  }

   GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
  const char *fs_source =
    "#version 120           \n"
    "void main(void) {        "
    "  gl_FragColor[0] = 0.0; "
    "  gl_FragColor[1] = 0.0; "
    "  gl_FragColor[2] = 1.0; "
    "}";
  glShaderSource(fs, 1, &fs_source, NULL);
  glCompileShader(fs);
  glGetShaderiv(fs, GL_COMPILE_STATUS, &compile_ok);
  if (!compile_ok) {
    fprintf(stderr, "Error in fragment shader\n");
    return 0;
  }

   program = glCreateProgram();
  glAttachShader(program, vs);
  glAttachShader(program, fs);
  glLinkProgram(program);
  glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
  if (!link_ok) {
    fprintf(stderr, "glLinkProgram:");
    return 0;
  }

    const char* attribute_name = "coord2d";
  attribute_coord2d = glGetAttribLocation(program, attribute_name);
  if (attribute_coord2d == -1) {
    fprintf(stderr, "Could not bind attribute %s\n", attribute_name);
    return 0;
  }

  return 1;
}

void onDisplay()
{
  /* Clear the background as white */
  glClearColor(1.0, 1.0, 1.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT);

  glUseProgram(program);
  glEnableVertexAttribArray(attribute_coord2d);
  GLfloat triangle_vertices[] = {
     0.0,  0.8,
    -0.8, -0.8,
     0.8, -0.8,
  };
  /* Describe our vertices array to OpenGL (it can't guess its format automatically) */
  glVertexAttribPointer(
    attribute_coord2d, // attribute
    2,                 // number of elements per vertex, here (x,y)
    GL_FLOAT,          // the type of each element
    GL_FALSE,          // take our values as-is
    0,                 // no extra data between each position
    triangle_vertices  // pointer to the C array
  );

  /* Push each element in buffer_vertices to the vertex shader */
  glDrawArrays(GL_TRIANGLES, 0, 3);
  glDisableVertexAttribArray(attribute_coord2d);
 
  /* Display the result */
  glutSwapBuffers();
}

void free_resources()
{
  glDeleteProgram(program);
}


int main(int argc, char* argv[])
{
  /* Glut-related initialising functions */
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
  glutInitWindowSize(640, 480);
  glutCreateWindow("My First Triangle");

  /* Extension wrangler initialising */
  GLenum glew_status = glewInit();
  if (glew_status != GLEW_OK)
  {
    fprintf(stderr, "Error: %s\n", glewGetErrorString(glew_status));
    return EXIT_FAILURE;
  }

  /* When all init functions runs without errors,
  the program can initialise the resources */
  if (1 == init_resources())
  {
    /* We can display it if everything goes OK */
    glutDisplayFunc(onDisplay);
    glutMainLoop();
  }

  /* If the program exits in the usual way,
  free resources and exit with a success */
  free_resources();
  return EXIT_SUCCESS;
}

我尝试了各种方法,包括调整链接器选项,明确指定.lib文件,指定包含路径,阅读相关错误的论坛等等,但都没有帮助。请问你们能否帮我解决这个问题?


你链接了哪些库? - Some programmer dude
5个回答

9
我从 http://glew.sourceforge.net/index.html (https://sourceforge.net/projects/glew/files/glew/1.9.0/glew-1.9.0-win32.zip/download) 获取了 glew 二进制文件,以及从 http://www.transmissionzero.co.uk/software/freeglut-devel/ (http://files.transmissionzero.co.uk/software/development/GLUT/freeglut-MSVC.zip) 下载了 freeglut 2.8.0 MSVC 包。
我将 include 路径设置为 glew-1.9.0\include\freeglut\include\,library 路径设置为 freeglut\lib\glew-1.9.0\lib\
我已更正您文件的头信息。
#include <Windows.h>
#include <iostream>
#include <gl/glew.h>
#include <gl/GL.h>
#include <gl/freeglut.h>

#pragma comment(lib, "glew32.lib")

链接成功,一切正常。 更新 当使用第三方库时,通常需要:
  • 将包含路径设置为<3rdPartyDir>\include,而不是<3rdPartyDir>\include\lib_name。在源代码中声明其包含应该是:
  • 正确的:#include <lib_name/header_name.h> 错误的:#include <header_name.h>,因为库内部可能存在依赖项,例如 #include <lib_name/other_header_name.h>
  • 将库路径设置为<3rdPartyDir>\lib。然后,必须使用以下一种方法指定所需库:
  • 对于MSVC,请添加
    #ifdef _MSC_VER
    #pragma comment(lib, "lib1_name.lib")
    #pragma comment(lib, "lib2_name.lib")
    /// etc
    #endif
    

    或者将所需的库添加到链接器选项中。

    一些库支持自动链接机制(例如,freeglut),即头文件包含类似于#pragma comment(lib, "lib1_name.lib")的行。

    • <3rdPartyDir>\bin复制所需的dll到<MyExePath>\

    这个方法之前对我起作用了,但是现在在运行时出错了 >> 程序无法启动,因为 glew32.dll 丢失,那么我该如何解决呢?应该将 glew32.dll 复制到 windows/system32 目录下吗? - BulBul
    1
    freeglut.dllglew32.dll 复制到输出目录中(例如,对于我的测试项目,复制到 testGl\Debug 目录下)。 - pogorskiy
    太好了,它成功了,非常感谢!您能否解释一下问题到底是什么?以前我没有遇到过缺少.dll文件的问题,有没有办法永久解决这个问题,这样我在未来的项目中就不会再遇到它了。 - BulBul
    请扩展静态链接的过程。我正在尝试链接到 libglew32.lib - Necktwi

    2

    我遇到了同样的问题。最终在这个Visual Studio和OpenGL教程中找到了有用的指导问题在于正确地包含适合的配置(Win32或x64)的.dll文件。


    2
    如果以上答案无效且glew似乎已经正确链接,则可能只是您忽略了链接opengl32.lib库。
    在VS中,
    项目->属性->链接器->输入->附加依赖项
    如果该行中没有opengl32.lib,请将其添加到该行中。

    1

    看起来你使用了不正确的glew.lib。当使用win32配置构建时,必须使用glew.lib(win32)或反之亦然。你可以尝试通过替换项目中的glew.lib来解决问题。


    1
    这绝对是链接器设置的问题,特别是与glew库有关。为什么你之前尝试修复它没有起作用并不太清楚。
    你能编译任何glew提供的教程程序吗?

    编辑

    从您的评论中看来,您似乎在包含lib文件方面遇到了问题。
    - 您能验证它是否在您认为的位置上(是否已正确安装)?
    - Visual Studio是否知道它应该在哪里(提供了正确的lib路径)?

    在Visual Studio中,项目 ->右键单击 + 属性 -> 配置属性 -> 链接器 -> 常规 -> 附加链接器目录是否有指向包含glew32.lib的文件夹的路径?


    好的,这就是你问题的核心所在。为了解决它 - glew32.lib 是否在你认为的位置上? Visual Studio 是否知道它的正确路径? - Karthik T
    之前我能够毫无问题地使用glew,这个错误只与这段代码有关。 - BulBul
    我把它放在Windows SDK/lib文件夹中,并且明确地将其添加到我的项目文件中。 - BulBul
    我明白了,那我就没有更多的想法了,@pogorskiy的修复有帮助吗? - Karthik T
    是的,我按照他说的做了,现在我得到了一个运行时错误(我在他的帖子上写了这个错误)。 - BulBul
    显示剩余2条评论

    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接