error: ‘glGenBuffers’ was not declared in this scope

#include<GLFW/glfw3.h>
#include<stdio.h>
#include<GL/glu.h>
#include<vector>
using namespace std;

typedef struct __attribute__((__packed__)) { 
	unsigned short type; 
	unsigned long size; 
	unsigned short reserved1; 
	unsigned short reserved2; 
	unsigned long offsetbits; 
} BITMAPFILEHEADER1;

typedef struct __attribute__((__packed__)) {
	unsigned long size; 
	unsigned long width; 
	unsigned long height; 
	unsigned short planes; 
	unsigned short bitcount; 
	unsigned long compression; 
	unsigned long sizeimage; 
	long xpelspermeter; 
	long ypelspermeter; 
	unsigned long colorsused; 
	unsigned long colorsimportant; 
} BITMAPINFOHEADER1;
 
typedef struct { 
	unsigned char blue; 
	unsigned char green; 
	unsigned char red; 
} SINGLE_PIXEL1;


void draw_all() {
	FILE *fp;
	unsigned char p;
	int x=0,y=0,c=0;
	float r,g,b;	
	
	BITMAPFILEHEADER1 bitmp;	
	BITMAPINFOHEADER1 bitm;	
	
	glClearColor(1.0,1.0,1.0,0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	
	fp = fopen("a.bmp","rb");//Filename is given	
	
	fread(&bitmp,14,1,fp);	
	/*
	printf("Type in hexadecimal: %x
",bitmp.type);
	printf("Size in hexadecimal: %x
",bitmp.size);
	printf("Reserved1 in hexadecimal: %x
",bitmp.reserved1);
	printf("Reserved2 in hexadecimal: %x
",bitmp.reserved2);
	printf("Offsetbits in hexadecimal: %x
",bitmp.offsetbits);	
	*/
	
	fread(&bitm,40,1,fp);
	//printf("Width: %x, Height: %x
",bitm.width,bitm.height);
	//printf("Width: %d, Height: %d
",bitm.width,bitm.height);	
	
	gluOrtho2D(0.0,bitm.width,0.0,bitm.height);
	glMatrixMode(GL_PROJECTION);
	
	glLoadIdentity();
	glViewport(10,10,bitm.width,bitm.height);
	   		
	vector<float> v_data;
	int width,height;
	
	width=bitm.width;
	height=bitm.height;
	
	//glBegin(GL_POINTS);
	while(!feof(fp))
	{
		fread(&p,1,1,fp);		
		b = p/255.0;		
		fread(&p,1,1,fp);
		g = p/255.0;		
		fread(&p,1,1,fp);
		r = p/255.0;
		v_data.push_back(r);
		v_data.push_back(g);
		v_data.push_back(b);
		/*
		glColor3f(r,g,b);		
		glVertex2i(x++,y);
		if(x == bitm.width)
		{ 
			x = 0;
			y++;
		}
		*/
	}	
	//glEnd();	
	unsigned int texture;
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    
	glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_RGB,GL_FLOAT,&v_data[0]);
		
	glEnable(GL_TEXTURE_2D);
	glActiveTexture(GL_TEXTURE);
	/*
	glBegin(GL_QUADS);

    glTexCoord2i(0,0);
    glVertex2i(0,0);
    
    glTexCoord2i(0,1);
    glVertex2i(0,height);
    
    glTexCoord2i(1,1);
    glVertex2i(width,height);
    
    glTexCoord2i(1,0);
    glVertex2i(width,0);

    glEnd();
   	*/
   	
   	float vertices[] = {
     0.0f,  0.5f, // Vertex 1 (X, Y)
     0.5f, -0.5f, // Vertex 2 (X, Y)
    -0.5f, -0.5f  // Vertex 3 (X, Y)
	};
	
	GLuint vbo;
	glGenBuffers(1, &vbo);
	
	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
       
	glFlush();
	fclose(fp);	
}

int main(void)
{
    GLFWwindow* window;
    /* Initialize the library */
    if (!glfwInit())
        return -1;
    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    /* Make the window's context current */
    glfwMakeContextCurrent(window);
    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {
        /* Render here */
   		draw_all();
        /* Swap front and back buffers */
        glfwSwapBuffers(window); 		
        /* Poll for and process events */
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;
}
g++ glfw.cpp -o glfw -lglfw -lGL -lGLU

glfw.cpp: In function ‘void draw_all()’:
glfw.cpp:134:22: error: ‘glGenBuffers’ was not declared in this scope
  glGenBuffers(1, &vbo);
                      ^
glfw.cpp:136:35: error: ‘glBindBuffer’ was not declared in this scope
  glBindBuffer(GL_ARRAY_BUFFER, vbo);
                                   ^
glfw.cpp:137:74: error: ‘glBufferData’ was not declared in this scope
  glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

What should i do? Which header file should i include?

I suspect that you’ll need to use a loading library such as GLEW.

Modern versions of the header files typically don’t provide prototypes for any OpenGL functions beyond OpenGL 1.1, as that’s all that Windows’ opengl32.dll exports. For functions added in later versions, they just declare a function pointer type, e.g (for glGenBuffers).


typedef void (APIENTRYP PFNGLGENBUFFERSPROC) (GLsizei n, GLuint *buffers);

To actually use the function, you need to either use e.g. wglGetProcAddress() or glXGetProcAddress() to obtain a pointer to the function, or use a library such as GLEW which handles this for you.

On Unix/X11, libGL typically exports all “known” functions, so you can just define the macro GL_GLEXT_PROTOTYPES to have the header define prototypes for them. But that’s fragile, as it means that program will fail to run if the client library is too old even if the OpenGL driver provides the function in question. So obtaining a function pointer from the driver is preferred.