【问题标题】:error LNK2019: unresolved external symbol错误 LNK2019:未解析的外部符号
【发布时间】:2012-10-31 14:59:32
【问题描述】:

当我想编译我的 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 文件,指定包含路径读取与这些错误相关的论坛等等,但都没有帮助,你们能帮我解决这个问题吗?

【问题讨论】:

  • 你链接哪些库?

标签: c++ c visual-studio-2010 opengl compiler-errors


【解决方案1】:

我从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 包

我将包含路径设置为glew-1.9.0\include\freeglut\include\,将库路径设置为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")

链接成功,并且成功了。

UPD

使用第三方库时,通常:

  • 您必须将包含路径设置为&lt;3rdPartyDir&gt;\include,而不是&lt;3rdPartyDir&gt;\include\lib_name。声明它包含在源代码中应该是:

正确:#include &lt;lib_name/header_name.h&gt;

错误:#include &lt;header_name.h&gt;,因为库内部可以是内部依赖,例如#include &lt;lib_name/other_header_name.h&gt;

  • 将库路径设置为&lt;3rdPartyDir&gt;\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")这样的一行

  • 将所需的dll从&lt;3rdPartyDir&gt;\bin复制到&lt;MyExePath&gt;\

【讨论】:

  • 这对我有用,但使用以前的标题,但现在在运行时它给了我一个错误>>程序无法启动,因为 glew32.dll 丢失,那么我应该如何解决这个问题?我应该将 glew32.dll 复制到 windows/system32 目录吗?
  • 复制freeglut.dllglew32.dll到输出目录(例如,我的测试项目复制到testGl\Debug
  • 万岁成功了,非常感谢!!你能解释一下到底是什么问题吗?以前我没有关于 .dll 文件丢失的问题,有没有办法永久克服这个问题,所以我在以后的项目中不会遇到它..
  • 请扩展静态链接的过程。我正在尝试链接到libglew32.lib
【解决方案2】:

我遇到了同样的问题。终于找到有用的指令in this Visual Studio and OpenGL tutorial。问题是正确包含正确配置(Win32 或 x64)的 .dll 文件。

【讨论】:

    【解决方案3】:

    这绝对是链接器设置的问题,特别是与glew 库有关。为什么你之前的修复尝试没有奏效,我不太清楚。

    你能得到glew提供的任何教程程序来编译吗?


    编辑

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

    Visual Studio 中的Project -&gt;Right click + properties -&gt; Configuration Properties -&gt; Linker -&gt; General -&gt; Additional Linker directories 是否有指向包含glew32.lib 的文件夹的路径?

    【讨论】:

    • 每当我在链接器设置中包含 glew32.lib 文件时,我都会收到一个错误:错误 1 ​​错误 LNK1104:无法打开文件 'glew32.lib' 我尝试将 glew32.lib 文件包含到我的项目包中但我得到同样的错误
    • 嗯,这就是你问题的核心。修复 - glew32.lib 你认为它在哪里? Visual Studio 知道它在哪里(正确的路径)吗?
    • 以前我可以毫无问题地使用glew,这个错误是特定于这个代码的
    • 我把它放在 windows SDK/lib 文件夹中,我也将它显式添加到我的项目文件中
    • 我明白了,在这种情况下我没有想法,@pogorskiy 的修复有帮助吗?
    【解决方案4】:

    您使用的 glew.lib 似乎不正确。使用 config win32 构建时,必须使用 glew.lib(win32) 或相反。您可以尝试替换项目中的 glew.lib。

    【讨论】:

      猜你喜欢
      • 2012-08-31
      • 1970-01-01
      • 2015-06-03
      • 2018-07-04
      相关资源
      最近更新 更多