2

當我看到instructions我很困惑,因爲它在<VC_ROOT>\include\GL\glew.h安裝GLEW的說明說,然後它說在窗口#include <glew.h>而不是#include <GL/glew.h>,我不知道是否這應該在#include <GL/glut.h>之前或之後。我可以構建freeglut示例,以使glut看起來可以正確安裝,而且我的問題似乎與使用glew構建相同。我嘗試運行完整的程序是連接錯誤與Windows GLEW和Visual Studio 7

// 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 [email protected] 
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 T的連接喜好:

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位無處不在,這讓我不知道什麼64位版本的glew是如果它只是我們應該使用的32位版本。

回答

4

這不是#include(頭文件)的問題。

'無法解析的外部符號'錯誤是因爲鏈接程序找不到(解析)您調用的這些GLEW方法(直接或間接)。

一個人通常會通過在鏈接器選項中添加如下內容來修復此問題:-lGLEW或-lglew32 ,以便鏈接器訪問此庫的「已編譯代碼」。
這也可以通過編碼開頭的pragma命令來完成,但是這個技巧只能在Visual Studio中使用,如果您嘗試將代碼與gcc鏈接起來,它將不起作用。

但無論如何,您必須確保有問題的文件(此處爲glew32.lib)位於鏈接器的庫路徑中。

+0

我在Visual Studio中構建它,並且如果我右鍵單擊單個文件並選擇編譯,但它無法構建解決方案,則編譯這些文件。我已將glew32.lib放入VC:\ lib目錄,並將其添加到鏈接器選項中。我不明白爲什麼它不起作用。 –

+1

不幸的是,我對Visual Studio的瞭解不多(除了我以前用過的老6.0之外)。確保glew32.lib文件所在的文件夾位於解決方案的庫路徑中。對此,一個快速的解決方法是將文件放在解決方案的Debug /目錄中,甚至是C:\ Windows \ System32(至少要驗證它是否確實沒有找到該文件,除非找到更永久的解決方案)。 –

+0

即使我將glew32.lib放入C:\ Windows \ System32中,它仍然無法正常工作,並且在將glew32.lib放入Debug目錄時,它也不起作用。我還將下載目錄作爲鏈接器的附加依賴項,但它不起作用。 –

1

無法解析的外部符號錯誤表示找到的頭文件glew.h很好,但無法找到並鏈接您的編譯指示引用的glew32.lib文件。確保glew32.lib位於Visual Studio可以引用的路徑中。

相關問題