error LNK2019: unresolved external symbol - c++

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?

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>\

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).

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?

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.

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.

Related

OpenGL with gl3w

I'm trying to learn OpenGL. I'm using the SFML window module for the context and gl3w as my loading library. I use SFML pretty often so it wasnt a problem to set it up for OpenGL following this tutorial:http://www.sfml-dev.org/tutorials/2.0/window-opengl.php
I could run the example code without any problems. I linked everything that I need for OpenGL (opengl32.lib and glu32.lib + sfml*.lib).
Then I followed this answer to get gl3w :How to set up gl3w on Windows?.
But now if I try to run this code which is mostly the example code from SFML
#include <SFML/gl3w.h>
#include <SFML/Window.hpp>
static const GLfloat red[] = { 1.0f, 0.f, 0.f, 1.f };
int main()
{
// create the window
sf::Window window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Default, sf::ContextSettings(32));
window.setVerticalSyncEnabled(true);
gl3wInit(); //ignore return value for now
// load resources, initialize the OpenGL states, ...
// run the main loop
bool running = true;
while (running)
{
// handle events
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
// end the program
running = false;
}
else if (event.type == sf::Event::Resized)
{
//adjust the viewport when the window is resized
glViewport(0, 0, event.size.width, event.size.height);
}
}
glClearBufferfv(GL_COLOR, 0, red);
// end the current frame (internally swaps the front and back buffers)
window.display();
}
// release resources...
return 0;
}
I get following Linker errors.
1>OpenGL.obj : error LNK2019: unresolved external symbol _gl3wInit referenced in function _main
1>OpenGL.obj : error LNK2001: unresolved external symbol _gl3wViewport
1>OpenGL.obj : error LNK2001: unresolved external symbol _gl3wClearBufferfv
I double checked if I linked the libs correctly.
Im working on Windows 7 with Visual Studio 2013.
Anyone knows what I do wrong?
You forgot to compile gl3w.
Assuming that you already:
grabbed python script
ran it: python gl3w_gen.py
found gl3w.h glcorearb.h and gl3w.c files generated
There are two ways:
First way. Just include those files in your project, so it will be compiled along with your own sources. Note, that you need to preserve GL folder or manually fix includes in gl3w.c.
Second way. Create new project add all three files in it and compile as static library. Then link it to your application (or just compile it in command line or makefile, whatever).
Happy coding!

OpenGL GLut linking issues with Visual Studio 2013 on Windows 8 64bit

First I downloaded glut from this source, http://user.xmission.com/~nate/glut.html"> Nate Robins. I put:
glut32.dll into C:\Windows\SysWOW64,
glut32.lib into C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib
glut.h into C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\GL
In Visual Studio, under C++ I created an empty project, changed the compiler to 64x. I made sure to select ALL Configurations and linked opengl32.lib,glu32.lib and glut32.lib.
In Tools -> Options -> VC++ Directories -> Include Files:C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include
In Configuration Properties → Linker → Additional Library Directories:C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\lib
/************************************
* Handout: example.cpp
*
* This program is to demonstrate the basic OpenGL program structure.
* Read the comments carefully for explanations.
************************************/
#define WINDOWS 1 /* Set to 1 for Windows, 0 else */
#define UNIX_LINUX 0 /* Set to 1 for Unix/Linux, 0 else */
#if WINDOWS
#include <windows.h>
#include <GL/glut.h>
#endif
#if UNIX_LINUX
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#endif
#include <stdio.h>
#include <math.h>
float f = 0.0;
void display(void);
void my_init(void);
void reshape(int w, int h);
void idle(void);
void main(int argc, char **argv)
{
/*---- Initialize & Open Window ---*/
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // double-buffering and RGB color
// mode.
glutInitWindowSize(500, 500);
glutInitWindowPosition(30, 30); // Graphics window position
glutCreateWindow("Rectangle"); // Window title is "Rectangle"
/*--- Register call-back functions; any order is fine ---*/
/* Events: display, reshape, keyboard, mouse, idle, etc.
- display: Automatically called when the window is first opened.
Later, when the frame content needs to be changed, we need
to call display again From the Program to re-draw the objects.
This is essential for animation.
- reshape: Automatically called when the window is first opened,
or when the window is re-sized or moved by the user.
- keyboard: Automatically called when a keyboard event occurs.
- mouse: Automatically called when a mouse event occurs.
- idle: Automatically called when nothing occurs.
This is essential for animation.
* Once entering the event loop, the execution control always returns to
the event loop. When particular event occurs, the corresponding call-back
function is called; when that call-back function finishes, control again
goes back to the event loop, and the process is repeated.
*/
glutDisplayFunc(display); // Register our display() function as the display
// call-back function
glutReshapeFunc(reshape); // Register our reshape() function as the reshape
// call-back function
// glutMouseFunc(mouse); // for mouse
// glutKeyboardFunc(key); // for keyboard
glutIdleFunc(idle); // Register our idle() function
my_init(); // initialize variables
glutMainLoop(); // Enter the event loop
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT); // clear frame buffer (also called the
// color buffer)
glColor3f(0.0, 1.0, 1.0); // draw in cyan.
// The color stays the same until we
// change it next time.
glBegin(GL_POLYGON); // Draw a polygon (can have many vertices)
// The vertex coordinates change by different
// values of i; see also function idle().
glVertex2f(100, 100 + f);
glVertex2f(200, 100 + f);
glVertex2f(200, 300 + f);
glVertex2f(100, 300 + f);
glEnd();
glFlush(); // Render (draw) the object
glutSwapBuffers(); // Swap buffers in double buffering.
}
void my_init()
{
glClearColor(0.0, 0.0, 0.0, 0.0); // Use black as the color for clearing
// the frame buffer (also called the
// color buffer). This produces a
// black background.
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h); // Viewport within the graphics window.
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}
void idle(void)
{
f += 0.02; // smaller number gives a slower but smoother animation
if (f > 180.0) f = 0.0;
glutPostRedisplay(); // or call display()
}
My errors are
Error 17 error LNK1120: 16 unresolved externals C:\Projects\Project1\x64\Debug\Project1.exe Project1
Error 15 error LNK2001: unresolved external symbol _fltused C:\Projects\Project1\Project1\main.obj Project1
Error 13 error LNK2001: unresolved external symbol _RTC_InitBase C:\Projects\Project1\Project1\main.obj Project1
Error 14 error LNK2001: unresolved external symbol _RTC_Shutdown C:\Projects\Project1\Project1\main.obj Project1
Error 16 error LNK2001: unresolved external symbol mainCRTStartup C:\Projects\Project1\Project1\LINK Project1
Error 7 error LNK2019: unresolved external symbol __imp___glutCreateWindowWithExit referenced in function glutCreateWindow_ATEXIT_HACK C:\Projects\Project1\Project1\main.obj Project1
Error 2 error LNK2019: unresolved external symbol __imp___glutInitWithExit referenced in function glutInit_ATEXIT_HACK C:\Projects\Project1\Project1\main.obj Project1
Error 1 error LNK2019: unresolved external symbol __imp_exit referenced in function glutInit_ATEXIT_HACK C:\Projects\Project1\Project1\main.obj Project1
Error 10 error LNK2019: unresolved external symbol __imp_glutDisplayFunc referenced in function main C:\Projects\Project1\Project1\main.obj Project1
Error 12 error LNK2019: unresolved external symbol __imp_glutIdleFunc referenced in function main C:\Projects\Project1\Project1\main.obj Project1
Error 3 error LNK2019: unresolved external symbol __imp_glutInitDisplayMode referenced in function main C:\Projects\Project1\Project1\main.obj Project1
Error 4 error LNK2019: unresolved external symbol __imp_glutInitWindowPosition referenced in function main C:\Projects\Project1\Project1\main.obj Project1
Error 5 error LNK2019: unresolved external symbol __imp_glutInitWindowSize referenced in function main C:\Projects\Project1\Project1\main.obj Project1
Error 6 error LNK2019: unresolved external symbol __imp_glutMainLoop referenced in function main C:\Projects\Project1\Project1\main.obj Project1
Error 8 error LNK2019: unresolved external symbol __imp_glutPostRedisplay referenced in function "void __cdecl idle(void)" (?idle##YAXXZ) C:\Projects\Project1\Project1\main.obj Project1
Error 11 error LNK2019: unresolved external symbol __imp_glutReshapeFunc referenced in function main C:\Projects\Project1\Project1\main.obj Project1
Error 9 error LNK2019: unresolved external symbol __imp_glutSwapBuffers referenced in function "void __cdecl display(void)" (?display##YAXXZ) C:\Projects\Project1\Project1\main.obj Project1
Cannot figure out what I'm doing wrong.
You are using 32bit libraries but you are compiling a x64 executable and the linker cannot resolve the symbols.

Linker error "... .obj : error LNK2019: unresolved external symbol referenced in function"

I have read all the previous replies or solutions for the same linker problem. I understand that the Linker is unable to access the library file that has the functions defined but still I have no luck in solving it!
The errors:
1>trial_12th.obj : error LNK2019: unresolved external symbol _viStatusDesc#12 referenced in function _main
1>trial_12th.obj : error LNK2019: unresolved external symbol _viClose#4 referenced in function _main
1>trial_12th.obj : error LNK2019: unresolved external symbol _viRead#16 referenced in function _main
1>trial_12th.obj : error LNK2019: unresolved external symbol _viWrite#16 referenced in function _main
1>trial_12th.obj : error LNK2019: unresolved external symbol _viOpen#20 referenced in function _main
1>trial_12th.obj : error LNK2019: unresolved external symbol _viOpenDefaultRM#4 referenced in function _main
1>C:\Users\41kchoudhary\Documents\Visual Studio 2010\Projects\trial_12th\Debug\trial_12th.exe : fatal error LNK1120: 6 unresolved externals
I am trying to send and receive data from a mixed-signal oscilloscope. In doing so I am required to write a .cpp file using the pre-defined commands/functions defined using Microsoft Visual Studio C++. I have read the user manual for using these commands, and I also have the header files and libraries required to implement it.
I am using the following code:
#include <visa.h>
#include "stdafx.h"
#include <stdio.h>
#include <memory.h>
int main(int argc, char* argv[])
{
ViSession rm = VI_NULL, vi = VI_NULL;
ViStatus status;
ViChar buffer[256];
ViUInt32 retCnt;
// Open a default session
status = viOpenDefaultRM(&rm);
if (status < VI_SUCCESS) goto error;
// Open the GPIB device at primary address 1, GPIB board 8
status = viOpen(rm, "USB::0x0699::0x0377::C011104::INSTR", VI_NULL, VI_NULL,
&vi);
if (status < VI_SUCCESS) goto error;
// Send an ID query.
status = viWrite(vi, (ViBuf) "*idn?", 5, &retCnt);
if (status < VI_SUCCESS) goto error;
// Clear the buffer and read the response
memset(buffer, 0, sizeof(buffer));
status = viRead(vi, (ViBuf) buffer, sizeof(buffer), &retCnt);
if (status < VI_SUCCESS) goto error;
// Print the response
printf("id: %s\n", buffer);
// Clean up
viClose(vi); // Not needed, but makes things a bit more
// understandable
viClose(rm); // Closes resource manager and any sessions
// opened with it
return 0;
error:
// Report error and clean up
viStatusDesc(vi, status, buffer);
fprintf(stderr, "failure: %s\n", buffer);
if (rm != VI_NULL) {
viClose(rm);
}
return 1;
}
You need to add either visa32.lib or visa64.lib to your linker settings.
One way to do that is to use a pragma in your compiler source file:
#pragma comment(lib,"visa32.lib")
If it is still not found then adjust your lib paths in your IDE or include the full path in the pragma.
I had the same issue. I figured out that you have to add visa32.lib in additional dependencies under the linker properties of your project
Go to your
project properties -> Linker -> Additional Dependencies-> Click Down Arrow -> "Edit -> Type visa32.lib
Click Ok, Ok

Multimedia timer? <mmsystem.h>

I'm trying to get this accurate timer working on VS2008, on Windows XP (and eventually Server 2008) from the following example:
http://technology.chtsai.org/w98timer/
However I get the following errors:
error LNK2019: unresolved external symbol _imp_timeEndPeriod#4
error LNK2019: unresolved external symbol _imp_timeGetTime#0
error LNK2019: unresolved external symbol _imp_timeBeginPeriod#4
error LNK2019: unresolved external symbol _imp_timeGetDevCaps#8
Could anybody please advise?
I just want a simple, accurate, millisecond-timing example for C++ on Windows.
#include <stdio.h>
#include <windows.h>
#include <mmsystem.h>
#include "stdafx.h"
void
main (void)
{
TIMECAPS resolution;
DWORD start, finish, duration, i;
if (timeGetDevCaps (&resolution, sizeof (TIMECAPS)) == TIMERR_NOERROR)
{
printf ("Minimum supported resolution = %d\n", resolution.wPeriodMin);
printf ("Maximum supported resolution = %d\n", resolution.wPeriodMax);
}
if (resolution.wPeriodMin <= 1)
{
if (timeBeginPeriod (1) == TIMERR_NOERROR)
{
for (i = 100; i <= 120; i++)
{
start = timeGetTime ();
while (timeGetTime () < (start + i));
finish = timeGetTime ();
duration = finish - start;
printf ("expected:%d actual:%ld\n", i, duration);
}
timeEndPeriod (1);
}
}
}
As MSDN suggests you need to include winmm.lib into the project. So add the following line anywhere into your source code:
#pragma comment(lib, "winmm.lib")
Looking at the docs it looks like you need to add Winmm.lib to the additional libraries to link in your project properties.
You need to add winmm.lib to your linker dependencies.
These functions are defined in winmm.dll. You need to add winmm.lib to your link list.

DirectX 9 "unresolved external symbol" problem

I have been working on learning DirectX for a couple days and have run into a problem. I have been following a books example and have solved several annoying problems that have come up, but have been unsuccessful at solving my most recent one. The compiler states that I have an unresolved external symbol whenever I try to compile. Here is the error code below.
1>game.obj : error LNK2019: unresolved external symbol "struct IDirect3DSurface9 * __cdecl
LoadSurface(char *,unsigned long)" (?LoadSurface##YAPAUIDirect3DSurface9##PADK#Z) referenced in
function "int __cdecl Game_Init(struct HWND__ *)" (?Game_Init##YAHPAUHWND__###Z)
1>C:\Users\Christopher\Documents\Visual Studio 2008\Projects\Work\Debug\Work.exe : fatal error
LNK1120: 1 unresolved externals
I am running: [compiler: Visual Studios 2008] [Operating system: Windows 7 64bit professional]
Here a sample of the code and where it seem to be taking place. (I hope I gave enough info)
LPDIRECT3DSURFACE9 kitty_image[7];
SPRITE kitty;
LPDIRECT3DSURFACE9 back;
//timing variable
long start = GetTickCount();
//initializes the game
int Game_Init(HWND hwnd)
{
char s[20];
int n;
//set random number seed
srand(time(NULL));
//load the sprite animation
for (n=0; n<6; n++)
{
sprintf(s, "cat%d.bmp",n+1);
kitty_image[n] = LoadSurface(s, D3DCOLOR_XRGB(255,0,255));
if (kitty_image[n] == NULL)
return 0;
}
back = LoadSurface("background.bmp", NULL );
//initialize the sprite's properties
kitty.x = 100;
kitty.y = 150;
kitty.width = 96;
kitty.height = 96;
kitty.curframe = 0;
kitty.lastframe = 5;
kitty.animdelay = 2;
kitty.animcount = 0;
kitty.movex = 8;
kitty.movey = 0;
//return okay
return 1;
}
I have linked both d3d9.lib and d3dx9.lib to my project and have included all neccesary header files that I know of (d3d9.h, d3dx9.h). Any help is greatly appreciated! Thanks in advance!!!
It looks like the linker is complaining that there is no such function LoadSurface. Where did you define it?