PDA

View Full Version : g++ link with '-lGL' problem in ubuntu12.04



water
05-06-2012, 08:40 AM
I'm a beginer of openGL using glfw in g++, when I use g++ link with OpenGL Igot all of the openGL functions such as glCreateShader Unimplemented,
What can I do with "g++ link with '-lGL'". Additional: gcc link with openGL is normal;
707
Sorry for my bad English at last. g++ main.cpp main_draw.cpp -o main -lglfw -lGL
main_draw.cpp: 在成员函数‘GLuint main_draw::load_shader(GLenum, const char*)’中:
main_draw.cpp:25:37: 错误: ‘glCreateShader’在此作用域中尚未声明
main_draw.cpp:26:38: 错误: ‘glShaderSource’在此作用域中尚未声明
main_draw.cpp:27:24: 错误: ‘glCompileShader’在此作用域中尚未声明
main_draw.cpp:28:50: 错误: ‘glGetShaderiv’在此作用域中尚未声明
main_draw.cpp:34:51: 错误: ‘glGetShaderInfoLog’在此作用域中尚未声明
main_draw.cpp:38:24: 错误: ‘glDeleteShader’在此作用域中尚未声明
main_draw.cpp: 在成员函数‘GLuint main_draw::link_program()’中:
main_draw.cpp:58:26: 错误: ‘glDeleteShader’在此作用域中尚未声明
main_draw.cpp:63:26: 错误: ‘glDeleteShader’在此作用域中尚未声明
main_draw.cpp:67:35: 错误: ‘glCreateProgram’在此作用域中尚未声明
main_draw.cpp:68:55: 错误: ‘glBindAttribLocation’在此作用域中尚未声明
main_draw.cpp:73:34: 错误: ‘glAttachShader’在此作用域中尚未声明
main_draw.cpp:75:23: 错误: ‘glLinkProgram’在此作用域中尚未声明
main_draw.cpp:77:49: 错误: ‘glGetProgramiv’在此作用域中尚未声明
main_draw.cpp:83:51: 错误: ‘glGetProgramInfoLog’在此作用域中尚未声明
main_draw.cpp:87:26: 错误: ‘glDetachShader’在此作用域中尚未声明
main_draw.cpp:89:26: 错误: ‘glDeleteShader’在此作用域中尚未声明
main_draw.cpp:93:65: 错误: ‘glGetUniformLocation’在此作用域中尚未声明
main_draw.cpp: 在构造函数‘main_draw::main_draw()’中:
main_draw.cpp:133:23: 错误: ‘glUseProgram’在此作用域中尚未声明
main_draw.cpp:149:65: 错误: ‘glVertexAttribPointer’在此作用域中尚未声明
main_draw.cpp:151:42: 错误: ‘glEnableVertexAttribArray’在此作用域中尚未声明
main_draw.cpp:154:43: 错误: ‘glDisableVertexAttribArray’在此作用域中尚未声明

water
05-06-2012, 08:46 AM
#include "main_draw.h"

char * main_draw::read_source_code(const char *file_path)
{
FILE *fp = fopen(file_path, "rt");
if(NULL == fp){
return NULL;
}
char a_char;
int size = 1;
while(fread(&a_char, sizeof(char), 1, fp)){
++ size;
}
char *src = (char *)malloc(sizeof(char) * size);
fseek(fp, 0, SEEK_SET);
int ret = fread(src, sizeof(char), size, fp);
src[ret] = '\0';
fclose(fp);
return src;
}

GLuint main_draw::load_shader(GLenum type, const char *src)
{
GLint status;
GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &src, NULL);
glCompileShader(shader);
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if(GL_FALSE == status){
int length;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
if(length > 1) {
char *log = (char *)malloc(sizeof(char) * length);
glGetShaderInfoLog(shader, length, &length, log);
printf("%s\n", log);
free(log);
}
glDeleteShader(shader);
shader = GL_FALSE;
}
return shader;
return 0;
}

GLuint main_draw::link_program()
{
char *vertex_shader = read_source_code("./v0.vsh");
if(NULL == vertex_shader){
return GL_FALSE;
}
v_shader = load_shader(GL_VERTEX_SHADER, vertex_shader);
if(GL_FALSE == v_shader){
return GL_FALSE;
}

char *frag_shader = read_source_code("./f0.fsh");
if(NULL == frag_shader) {
glDeleteShader(v_shader);
return GL_FALSE;
}
f_shader = load_shader(GL_FRAGMENT_SHADER, frag_shader);
if(GL_FALSE ==f_shader){
glDeleteShader(v_shader);
return GL_FALSE;
}

GLuint program = glCreateProgram();
glBindAttribLocation(program, ATTRIB_VERTEX, "vertex");
glBindAttribLocation(program, ATTRIB_COLOR, "color");
glBindAttribLocation(program, ATTRIB_NORMAL, "normal");
glBindAttribLocation(program, ATTRIB_TEXTURE, "tex_coord");

glAttachShader(program, v_shader);
glAttachShader(program, f_shader);
glLinkProgram(program);
GLuint linked;
glGetProgramiv(program, GL_LINK_STATUS, &linked);
if(GL_FALSE == linked){
int length;
glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
if(length > 1){
char *info = (char *)malloc(sizeof(char) * length);
glGetProgramInfoLog(program, length, NULL, info);
printf("%s\n", info);
free(info);
}
glDetachShader(v_shader);
glDetachShader(f_shader);
glDeleteShader(v_shader);
glDeleteShader(v_shader);
program = GL_FALSE;
}
uniforms[UNIFORM_SPACE] = glGetUniformLocation(program, "space");
uniforms[UNIFORM_POSITION] = glGetUniformLocation(program, "position");
uniforms[UNIFORM_ROTATE] = glGetUniformLocation(program, "rotate");
uniforms[UNIFORM_CAMERA] = glGetUniformLocation(program, "camera");
uniforms[UNIFORM_PERSPECT] = glGetUniformLocation(program, "perspect");
free(frag_shader);
free(vertex_shader);
return program;
}

void main_draw::reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}

main_draw::main_draw()
{
int running = GL_TRUE;
int ret = glfwInit();
if(ret == GL_FALSE){
printf("glfw init error!");
}
glfwOpenWindowHint(GLFW_REFRESH_RATE, 50);
glfwOpenWindowHint(GLFW_WINDOW_NO_RESIZE, GL_FALSE);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 0);
if(!glfwOpenWindow(800, 800, 8, 8, 8, 8, 8, 8, GLFW_WINDOW)){
glfwTerminate();
}
glfwSetWindowTitle("main draw");
glfwSetWindowSize(800, 800);
glfwSetWindowPos(0, 0);

program = link_program();
if(GL_FALSE == program){
printf("link prgram error!");
exit(0);
}
glUseProgram(program);
glClearColor(0.0, 0.0, 0.0, 1.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
reshape(800, 800);

float vertex[] = {-0.5, -0.5, 0.0,
-0.5, 0.5, 0.0,
0.5, 0.5, 0.0,
0.5, -0.5, 0.0};
float color[] = {1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 1.0,
0.0, 0.0, 1.0, 1.0,
0.0, 0.0, 0.0, 1.0};

while(running){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glVertexAttribPointer(ATTRIB_VERTEX, 3, GL_FLOAT, 0, 0, vertex);
glVertexAttribPointer(ATTRIB_COLOR, 4, GL_FLOAT, 0, 0, color);
glEnableVertexAttribArray(ATTRIB_VERTEX);
glEnableVertexAttribArray(ATTRIB_COLOR);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
glDisableVertexAttribArray(ATTRIB_VERTEX);
glDisableVertexAttribArray(ATTRIB_COLOR);
glfwSwapBuffers();
running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
}
}

main_draw::~main_draw()
{
glfwTerminate();
}

agrum_
05-06-2012, 10:03 AM
I'm new with openGL too, but I think it comes from your includes. First make sure you include glew before glfw. Then, if It wasn't that, try adding -lglew and -lglfw.
I've never used glfw so I can't tell more, however, glUseProgram and stuff are glew functions, then I guess the problem comes from glew.

water
05-19-2012, 03:12 AM
I find the fininal revolustion: g++ main_draw.cpp main.cpp -o main -lGL -lglfw -DGL_GLEXT_PROTOTYPES,
but I don't quite understand this -DGL_GLEXT_PROTOTYPES

Dark Photon
05-19-2012, 06:17 PM
Just means for glext.h to pull in function prototypes (by default it doesn't).