Windows 7上的glew和Visual Studio链接器错误 [英] Linker error with glew and Visual Studio on windows 7

查看:125
本文介绍了Windows 7上的glew和Visual Studio链接器错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我阅读说明时,我很困惑因为它在安装说明中说到<VC_ROOT>\include\GL\glew.h,然后在Windows上说到#include <glew.h>而不是#include <GL/glew.h>,所以我不知道这应该在#include <GL/glut.h>之前还是之后.我可以构建freeglut示例,以便似乎正确安装了glut,而我的问题似乎出在带毛刺的构建上.我尝试运行的完整程序是

When I read the instructions I am confused since it says in the instructions to install glew at <VC_ROOT>\include\GL\glew.h and then it says on windows to #include <glew.h> and not #include <GL/glew.h> and I don't know whether this should be before or after #include <GL/glut.h>. I can build the freeglut example so glut appears to be correctly installed and my problem seems to be with building with glew. The complete program that I try to run is

// Two-Dimensional Sierpinski Gasket       
// Generated using randomly selected vertices and bisection


#include <windows.h>
#include "Angel.h"
#include <glew.h>
#include <GL/glut.h>

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

const int NumPoints = 5000;

//----------------------------------------------------------------------------

void
init( void )
{

    vec2 points[NumPoints];

    // Specifiy the vertices for a triangle
    vec2 vertices[3] = {
        vec2( -1.0, -1.0 ), vec2( 0.0, 1.0 ), vec2( 1.0, -1.0 )
    };

    // Select an arbitrary initial point inside of the triangle
    points[0] = vec2( 0.25, 0.50 );

    // compute and store N-1 new points
    for ( int i = 1; i < NumPoints; ++i ) {
        int j = rand() % 3;   // pick a vertex at random

        // Compute the point halfway between the selected vertex
        //   and the previous point
        points[i] = ( points[i - 1] + vertices[j] ) / 2.0;
    }

    // Create a vertex array object
    GLuint vao;
    glGenVertexArrays( 1, &vao );
    glBindVertexArray( vao );

    // Create and initialize a buffer object
    GLuint buffer;
    glGenBuffers( 1, &buffer );
    glBindBuffer( GL_ARRAY_BUFFER, buffer );
    glBufferData( GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW );

    // Load shaders and use the resulting shader program
    GLuint program = InitShader( "vshader21.glsl", "fshader21.glsl" );
    glUseProgram( program );

    // Initialize the vertex position attribute from the vertex shader
    GLuint loc = glGetAttribLocation( program, "vPosition" );
    glEnableVertexAttribArray( loc );
    glVertexAttribPointer( loc, 2, GL_FLOAT, GL_FALSE, 0,
                           BUFFER_OFFSET(0) );

    glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white background
}

//----------------------------------------------------------------------------

void
display( void )
{
    glClear( GL_COLOR_BUFFER_BIT );     // clear the window
    glDrawArrays( GL_POINTS, 0, NumPoints );    // draw the points
    glFlush();
}

//----------------------------------------------------------------------------

void
keyboard( unsigned char key, int x, int y )
{
    switch ( key ) {
    case 033:
        exit( EXIT_SUCCESS );
        break;
    }
}

//----------------------------------------------------------------------------

int
main( int argc, char **argv )
{

    glutInit(&argc, argv);
    glutInitDisplayMode( GLUT_RGBA );
    glutInitWindowSize( 512, 512 );

    // If you are using freeglut, the next two lines will check if 
    // the code is truly 3.2. Otherwise, comment them out

     glutInitContextVersion( 3, 1 );
     glutInitContextProfile( GLUT_CORE_PROFILE );

    glutCreateWindow( "Sierpinski Gasket" );




    glewInit();

    init();

    glutDisplayFunc( display );
    glutKeyboardFunc( keyboard );

    glutMainLoop();
    return 0;
}

我遇到的构建错误是当我尝试执行构建解决方案"时(如果我右键单击该文件并选择编译",则可以编译两个.cpp文件)

The build error I get is when I try to do "build solution" (the two .cpp files can compile if I right-click the file and select compile)

1>------ Build started: Project: 6E test, Configuration: Release Win32 ------
1>  InitShader.cpp
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\Angel.h(65): warning C4305: 'initializing' : truncation from 'double' to 'const GLfloat'
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(698): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(699): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(700): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(721): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(723): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(726): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(742): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>..\CODE\InitShader.cpp(16): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1>          C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\stdio.h(218) : see declaration of 'fopen'
1>  example1.cpp
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\Angel.h(65): warning C4305: 'initializing' : truncation from 'double' to 'const GLfloat'
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(698): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(699): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(700): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(721): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(723): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(726): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>c:\users\student\downloads\6e_example1_vc10\6e test\code\mat.h(742): warning C4244: '=' : conversion from 'double' to 'GLfloat', possible loss of data
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewBindBuffer
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewVertexAttribPointer
1>example1.obj : error LNK2001: unresolved external symbol __imp__glewInit@0
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewGenVertexArrays
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewUseProgram
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewBufferData
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewBindVertexArray
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewGetAttribLocation
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewGenBuffers
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewEnableVertexAttribArray
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewCreateShader
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewGetShaderInfoLog
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewLinkProgram
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewCompileShader
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewShaderSource
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewGetProgramiv
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewGetShaderiv
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewGetProgramInfoLog
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewCreateProgram
1>InitShader.obj : error LNK2001: unresolved external symbol __imp____glewAttachShader
1>C:\Users\student\Downloads\6E_example1_VC10\6E test\Release\6E test.exe : fatal error LNK1120: 20 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

我已将glew32.lib放在Visual Stuiod的库目录中:

I have put the glew32.lib in Visual Stuiod's library directory:

在Visual Studio项目的链接器首选项中,我还具有glew32.lib:

I also have glew32.lib in Visual Studio project's linker preferences:

我也将glew32.lib复制到了C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib,但它不起作用并生成了构建错误.我究竟做错了什么?即使我将glew32.lib放在C:\ Windows \ System32中,它也不起作用,而当我将glew32.lib放在Debug目录中时,它也不起作用.我什至将下载目录作为VC链接器的附加依赖项,但仍然无法正常工作:

I've also copied glew32.lib to C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib but it's not working and generates the build error. What am I doing wrong? It's not working even though I put glew32.lib in C:\Windows\System32 and neither does it work when I put the glew32.lib in the Debug directory. I've even put the download directory as an additional dependency for the VC linker and still it is not working:

我认为应将glew32.lin放置在的目录是C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib,但将其放置在这里没有区别.我尝试使用Visual Studio 2010和2012.

The directory where I suppose that glew32.lin should be placed is C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib but placing it there makes no difference. I tried with both Visual Studio 2010 and 2012.

我也注意到这种差异:当手册说应该存在Opengl32.lib时,它不在我的VC \ lib目录中.

I also notice this discrepancy: Opengl32.lib is not in my VC\lib directory when the manual says that it should be.

问题似乎与此问题相同:

The problem seems to be the same as in this question:

问题,外部问题未解决

我在OpenGL论坛上读到有人遇到了这个问题,这是因为他们混用了64位版本和32位版本,最初我也可能这样做,但现在我检查了一下,看来是32位如果只是想使用的32位版本,那么无处不在的64位版本的glew会给我带来什么.

I read on the OpenGL forum that somebody had this problem and it was because they had mixed up the 64 bit version and the 32 bit version and I might too have done that initially but now I checked and it seems to be 32 bit everywhere which makes me woner what the 64 bit version of glew is for if it's only the 32 bit version that we're suppose to use.

推荐答案

#include(头文件)不是问题.

It is not a problem of #include (header files).

未解析的外部符号"错误是因为链接器无法找到(解析)您(直接或间接)调用的GLEW方法.

The 'unresolved external symbol' errors are because the linker can't find (resolve) these GLEW methods you call (directly or indirectly).

通常可以通过添加以下内容来解决此问题:-lGLEW或-lglew32 链接器选项,以使链接器可以访问此库的编译代码".
也可以通过在代码的开头使用pragma命令来完成此操作,但是此技巧仅在Visual Studio中有效,并且在您尝试将代码与gcc链接时无效.

One usually fixes this by adding something like: -lGLEW or -lglew32 to the linker options, in order to give the linker access to the 'compiled code' of this library.
This can also be done via the pragma command at the beginning of your code, but this trick only works in Visual Studio, and won't work if you try to link your code with gcc for instance.

但是无论如何,您都必须确保在链接器的库路径中找到有问题的文件(此处为glew32.lib).

But in any case, you have to make sure the file in question (here glew32.lib) is found in the library path of the linker.

这篇关于Windows 7上的glew和Visual Studio链接器错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆