尝试在VC中运行OpenGL示例时生成错误

时间:2013-05-05 23:13:23

标签: visual-studio visual-c++ opengl glew freeglut

我无法理解为什么两个单独的.cpp文件都会编译但是解决方案没有构建。即使我遵循instructions,也无法编译的代码是

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



#include "Angel.h"
#include <GL/glut.h>
#include <GL/glew.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;
}

当我尝试从Edward Angel的OpenGL网站构建example project时,我收到了这条奇怪的错误消息:

1>------ Build started: Project: 6E test, Configuration: Release Win32 ------
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewBindBuffer
1>example1.obj : error LNK2001: unresolved external symbol __imp____glutCreateWindowWithExit@8
1>example1.obj : error LNK2001: unresolved external symbol __imp____glutInitWithExit@12
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__glutInitContextProfile@4
1>example1.obj : error LNK2001: unresolved external symbol __imp____glewUseProgram
1>example1.obj : error LNK2001: unresolved external symbol __imp__glutInitContextVersion@8
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__glutInitDisplayMode@4
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__glutKeyboardFunc@4
1>example1.obj : error LNK2001: unresolved external symbol __imp__glutMainLoop@0
1>example1.obj : error LNK2001: unresolved external symbol __imp__glutInitWindowSize@8
1>example1.obj : error LNK2001: unresolved external symbol __imp__glutDisplayFunc@4
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: 29 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

在我看来,我试图链接不兼容的库,上面的内容对你来说是什么样的?我尝试运行的完整源代码(使用Windows 7)是

// Two-Dimensional Sierpinski Gasket       
// Generated using randomly selected vertices and bisection
#pragma comment(lib, "glew32.lib")
#include "Angel.h"

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


#pragma comment(lib, "glew32.lib")
#include "Angel.h"

namespace Angel {

// Create a NULL-terminated string by reading the provided file
static char*
readShaderSource(const char* shaderFile)
{
    FILE* fp = fopen(shaderFile, "r");

    if ( fp == NULL ) { return NULL; }

    fseek(fp, 0L, SEEK_END);
    long size = ftell(fp);

    fseek(fp, 0L, SEEK_SET);
    char* buf = new char[size + 1];
    fread(buf, 1, size, fp);

    buf[size] = '\0';
    fclose(fp);

    return buf;
}


// Create a GLSL program object from vertex and fragment shader files
GLuint
InitShader(const char* vShaderFile, const char* fShaderFile)
{
    struct Shader {
    const char*  filename;
    GLenum       type;
    GLchar*      source;
    }  shaders[2] = {
    { vShaderFile, GL_VERTEX_SHADER, NULL },
    { fShaderFile, GL_FRAGMENT_SHADER, NULL }
    };

    GLuint program = glCreateProgram();


    for ( int i = 0; i < 2; ++i ) {
    Shader& s = shaders[i];
    s.source = readShaderSource( s.filename );
    if ( shaders[i].source == NULL ) {
        std::cerr << "Failed to read " << s.filename << std::endl;
        exit( EXIT_FAILURE );
    }

    GLuint shader = glCreateShader( s.type );

    glShaderSource( shader, 1, (const GLchar**) &s.source, NULL );
    glCompileShader( shader );

    GLint  compiled;
    glGetShaderiv( shader, GL_COMPILE_STATUS, &compiled );
    if ( !compiled ) {
        std::cerr << s.filename << " failed to compile:" << std::endl;
        GLint  logSize;
        glGetShaderiv( shader, GL_INFO_LOG_LENGTH, &logSize );
        char* logMsg = new char[logSize];
        glGetShaderInfoLog( shader, logSize, NULL, logMsg );
        std::cerr << logMsg << std::endl;
        delete [] logMsg;

        exit( EXIT_FAILURE );
    }

    delete [] s.source;

    glAttachShader( program, shader );
    }

    /* link  and error check */
    glLinkProgram(program);

    GLint  linked;
    glGetProgramiv( program, GL_LINK_STATUS, &linked );
    if ( !linked ) {
    std::cerr << "Shader program failed to link" << std::endl;
    GLint  logSize;
    glGetProgramiv( program, GL_INFO_LOG_LENGTH, &logSize);
    char* logMsg = new char[logSize];
    glGetProgramInfoLog( program, logSize, NULL, logMsg );
    std::cerr << logMsg << std::endl;
    delete [] logMsg;

    exit( EXIT_FAILURE );
    }

    /* use program object */
    glUseProgram(program);

    return program;
}

}  // Close namespace Angel block

更新

我将代码更改为如下所示,但仍然没有构建,我仍然遇到构建错误。

#pragma comment(lib, "glew32.lib")
#define GLEW_STATIC 1
#include "Angel.h"
#include <GL\glew.h>

更新150529

我可以构建freeglut GLUT示例,因此GLUT似乎与VC一起正确安装,但是当我双击其中一个示例构建时,它说我的系统上没有安装freeglut.dll。在尝试构建Angel的示例时,我仍然遇到相同的编译错误。为什么?我究竟做错了什么?我该怎么办?

enter image description here

1>------ Build started: Project: 6E test, Configuration: Release Win32 ------
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>  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(13): 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.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 ==========

更新

我再次尝试询问这个问题,但仍然无效:

Linker error with glew and Visual Studio on windows 7

2 个答案:

答案 0 :(得分:2)

你错过了GLEW(The OpenGL Extension Wrangler Library)。您可以将此链接与源代码中的以下pragma链接:

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

或者您可以修改项目设置中的链接器标志。这假定您已安装GLEW库。在我的系统上,我按以下路径安装它:

C:\Program Files (x86)/Microsoft Visual Studio 10.0/VC/lib/glew32.lib

您的系统上的路径可能有所不同,如果您不想安装它,还有其他与GLEW链接的方式。

答案 1 :(得分:2)

你可以放开GLEW。它所做的就是加载GL(和WGL)扩展。不过你可以自己做:

1.包括标题(谷歌文件名下载):

#include<GL/glext.h>
#include<GL/wglext.h>

2.declare函数指针:

PFNGLGENVERTEXARRAYSPROC glGenVertexArrays;
PFNGLBINDVERTEXARRAYPROC glBindVertexArray;
PFNWGLMAKECONTEXTCURRENTARBPROC wglMakeContextCurrentARB;
//etc.

3.通过wglGetProcAddress:

加载函数指针
glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC)wglGetProcAddress("glGenVertexArrays");
glBindVertexArray = (PFNGLBINDVERTEXARRAYPROC)wglGetProcAddress("glBindVertexArray");
wglMakeContextCurrentARB = (PFNWGLMAKECONTEXTCURRENTARBPROC)wglGetProcAddress("wglMakeContextCurrentARB");
//etc

4.let示例代码使用它们(无更改)!

glGenVertexArrays( 1, &vao );
//etc