error LNK2019: unresolved external symbol
Asked Answered
K

5

13

while i want to compile my opengl code i get the following errors:

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  

my code is :

#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;
}

i tried every thing from tweaking linker option including .lib files explicitly, specifying include paths reading forums related to these errors and so on, none of them helped, can you guys help me how i fix this problem?

Kammerer answered 12/11, 2012 at 6:54 Comment(1)
What libraries do you link with?Perkin
A
9

I got the glew binaries from http://glew.sourceforge.net/index.html (https://sourceforge.net/projects/glew/files/glew/1.9.0/glew-1.9.0-win32.zip/download) and freeglut 2.8.0 MSVC Package from http://www.transmissionzero.co.uk/software/freeglut-devel/ (http://files.transmissionzero.co.uk/software/development/GLUT/freeglut-MSVC.zip)

I set the include path to glew-1.9.0\include\, freeglut\include\ and library path to freeglut\lib\, glew-1.9.0\lib\.

I corrected the header of your file as

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

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

Linking successful, and it worked.

UPD

When using third-party libraries, usually:

  • You must set the include path to <3rdPartyDir>\include, but not to <3rdPartyDir>\include\lib_name. Declare its inclusion in the source code should be:

correct: #include <lib_name/header_name.h>

wrong: #include <header_name.h>, because within the library can be internal dependencies, for example #include <lib_name/other_header_name.h>

  • Set the library path to <3rdPartyDir>\lib. Then, you must specify the required libraries, one of the following methods:

For MSVC, add

#ifdef _MSC_VER
#pragma comment(lib, "lib1_name.lib")
#pragma comment(lib, "lib2_name.lib")
/// etc
#endif

Or, add the required libraries to the linker options.

Some libraries support auto-linking mechanism (for example, freeglut), that is, the header file contains a line like #pragma comment(lib, "lib1_name.lib")

  • Copy the required dlls from <3rdPartyDir>\bin to <MyExePath>\
Apophasis answered 12/11, 2012 at 7:24 Comment(4)
well this worked for me but with previous headers, but now during runtime it gives me an error >> the program can't start because glew32.dll is missing, so how should i fix this ?? should i copy glew32.dll to windows/system32 directory?Kammerer
copy freeglut.dll and glew32.dll to output directory (for example, to testGl\Debug for my test project)Apophasis
hooray it worked thank you very much!! could you please explain what was exactly the problem?? previously i had not such issues regarding .dll files being missing, is there a way to overcome this problem permanently so i don't encounter it during future projects..Kammerer
Please extend the procedure for static linking. I am trying to link to libglew32.lib.Dinin
S
2

I was having the same problem. Finally found useful instructions in this Visual Studio and OpenGL tutorial. The issue was correctly including the .dll files for the right configuration (Win32 or x64).

Sisterhood answered 9/12, 2015 at 20:21 Comment(0)
B
2

If the above answers don't work and glew seems to be linked properly, you may have simply neglected to link opengl32.lib.

In VS,

Project > Properties > Linker > Input > Additional Dependencies and add opengl32.lib to that line if it isn't there already.

Battlefield answered 31/10, 2022 at 18:19 Comment(0)
C
1

This is definitely a problem with linker settings, specifically to do with the glew library. Why your previous attempts to fix it have not worked isnt too clear to me.

Are you able to get any tutorial programs that glew provides to compile?


Edit

From your comment it looks like you are having issues including your lib file.
- Can you verify if it is where you think it is (is it installed correctly)?
- Does Visual studio know where it is supposed to be(is correct path to lib provided)?

Does Project ->Right click + properties -> Configuration Properties -> Linker -> General -> Additional Linker directories in Visual Studio have the path to the folder containing glew32.lib?

Culet answered 12/11, 2012 at 7:12 Comment(7)
whenever i include glew32.lib file in the linker settings i get one error: Error 1 error LNK1104: cannot open file 'glew32.lib' i tried including glew32.lib file into my project bundle yet i get the same errorKammerer
well there is the core of your problem. To fix - is glew32.lib where you think it is? does visual studio know where it is (correct path) ?Culet
previously i was able to use glew without any problem, this error is specific to this codeKammerer
i put it in windows SDK/lib folder, also i add it explicitly to my project filesKammerer
I see, in that case im out of ideas, is @pogorskiy's fix helping?Culet
Yeah but did you tell the linker about it, as in @Apophasis 's answer?Donell
yea i did as he said now i get a runtime error (i wrote the error on his post)Kammerer
L
1

It seems as you used not correct glew.lib. when using config win32 to build you must use glew.lib(win32) or opposite. You can try by replace glew.lib in your project.

Larcenous answered 12/7, 2015 at 12:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.