visual-studio-2010 - Windows 7 上的 glew 和 Visual Studio 链接器错误

标签 visual-studio-2010 visual-studio visual-c++ windows-7-x64 glew

当我阅读 instructions我很困惑,因为它在说明中说要在 <VC_ROOT>\include\GL\glew.h 安装 glew然后它在 Windows 上显示为 #include <glew.h>而不是 #include <GL/glew.h>我不知道这应该在 #include <GL/glut.h> 之前还是之后.我可以构建 freeglut 示例,因此 glut 似乎已正确安装,而我的问题似乎与使用 glew 构建有关。我尝试运行的完整程序是

// 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 文件可以编译)

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 的库目录中:

enter image description here

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

enter image description here

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

enter image description here

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

我还注意到这个差异:Opengl32.lib 不在我的 VC\lib 目录中,而手册上说它应该在。

问题似乎和这个问题一样:

Glew problems, unresolved externals

我在 OpenGL 论坛上看到有人遇到了这个问题,这是因为他们混淆了 64 位版本和 32 位版本,我最初也可能这样做,但现在我检查了一下,它似乎是 32 位的如果它只是我们应该使用的 32 位版本,这让我很清楚 glew 的 64 位版本是干什么用的。

最佳答案

不是#include(头文件)的问题。

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

通常通过添加以下内容来解决此问题:-lGLEW 或 -lglew32 链接器选项,以便链接器访问该库的“编译代码”。
这也可以通过代码开头的 pragma 命令来完成,但这个技巧只适用于 Visual Studio,如果你尝试将你的代码与 gcc 链接,则不会起作用。

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

关于visual-studio-2010 - Windows 7 上的 glew 和 Visual Studio 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16816296/

相关文章:

qt - 在 Qt Creator 4.8 中禁用 MSVC 链接器警告

c++ - 在标识符 ')' 之前缺少 '��'

windows - 如何从 Visual C++ 应用程序执行另一个程序

C++ 和 Lua : automatically register functions

.net - 为什么并行堆栈不显示任何任务?

c++ - Visual Studio Community 2019 C++ 控制台应用程序模板立即关闭

c# - 如何在 visual studio 中仅在字符串文字中搜索单词?

c++ - 如何获取进程相对时间

visual-studio-2010 - VS2010代码分析,有什么方法可以自动修复某些警告?

visual-studio - 是否可以在 Visual Studio 的堆栈中复制和粘贴文本?