opengl 错误LNK2019:无法解析的外部符号

ffx8fchx  于 2022-11-04  发布在  其他
关注(0)|答案(5)|浏览(385)

当我想编译我的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文件显式,指定包括路径阅读论坛相关的这些错误等,没有一个帮助,你们能帮我如何解决这个问题吗?

vmjh9lq9

vmjh9lq91#

我从http://glew.sourceforge.net/index.htmlhttps://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

使用第三方库时,通常:

  • 必须将包含路径设置为<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")的行

  • 将所需的dll从<3rdPartyDir>\bin复制到<MyExePath>\
0dxa2lsx

0dxa2lsx2#

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

8ulbf1ek

8ulbf1ek3#

这肯定是链接器设置的问题,特别是与glew库有关。为什么你以前试图修复它没有工作,我不太清楚。
你能得到glew提供的任何教程程序来编译吗?

编辑

从你的评论看,你似乎有问题,包括你的lib文件。

  • 您是否可以验证它是否在您认为的位置(是否正确安装)?
  • Visual studio是否知道它应该在哪里(是否提供了指向lib的正确路径)?
    Visual Studio中的Project ->Right click + properties -> Configuration Properties -> Linker -> General -> Additional Linker directories是否具有指向包含glew32.lib的文件夹的路径?
dced5bon

dced5bon4#

看起来你使用了不正确的glew.lib。当使用config win32来构建时,你必须使用glew.lib(win32)或相反。你可以尝试在你的项目中替换glew.lib。

rjee0c15

rjee0c155#

如果上面的答案不起作用,并且glew看起来链接正确,那么您可能只是忽略了链接opengl32.lib。
在VS中,
Project〉Properties〉Linker〉Input〉Additional Dependencies,然后将opengl32.lib添加到该行(如果该行中还没有opengl32.lib)。

相关问题