title: opengl error: undefined reference to `main' collect2: error: ld returned 1 exit status
body: <p>I modified a program trying to render a triangle and I get the following errors :</p>
  
  <pre><code> g++ -Wall triangle_with_shader.cpp -lglut -lGLU -lGLEW  -o triangle
  
  
  /usr/bin/ld: /tmp/cczvlWs7.o: undefined reference to symbol 'glDrawArrays'
  //usr/lib/x86_64-linux-gnu/mesa/libGL.so.1: error adding symbols: DSO missing from command line
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>I have only one file named triangle_with_shader.cpp and the code in this file:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string.h&gt;
  #include &lt;GL/glew.h&gt;
  #include &lt;GL/glut.h&gt;
  GLuint VBO;
  const GLchar* vertexShaderSource = "#version 330 core\n"
      "layout (location = 0) in vec3 position;\n"
      "void main()\n"
      "{\n"
      "gl_Position = vec4(position.x, position.y, position.z, 1.0);\n"
      "}\0";
  const GLchar* fragmentShaderSource = "#version 330 core\n"
       "out vec4 color;\n"
       "void main()\n"
       "{\n"
       "color = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
       "}\n\0";
  
   void RenderSceneCB()
  {
      glClear(GL_COLOR_BUFFER_BIT);
      glEnableVertexAttribArray(0);
      glBindBuffer(GL_ARRAY_BUFFER, VBO);
      glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
  
      glDrawArrays(GL_TRIANGLES, 0, 3);
  
      glDisableVertexAttribArray(0);
  
      glutSwapBuffers();
  }
  
  
  void InitializeGlutCallbacks()
   {
       glutDisplayFunc(RenderSceneCB);
   }
  
      void CreateVertexBuffer()
  {
      GLfloat Vertices[] = {
      -0.5f, -0.5f, 0.0f, // Left  
       0.5f, -0.5f, 0.0f, // Right 
       0.0f,  0.5f, 0.0f  // Top   
      };
      glGenBuffers(1, &amp;VBO);
      glBindBuffer(GL_ARRAY_BUFFER, VBO);
      glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
  }
  
  void shader() {
     // Build and compile our shader program
     // Vertex shader
      GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(vertexShader, 1, &amp;vertexShaderSource, NULL);
      glCompileShader(vertexShader);
      // Check for compile time errors
      GLint success;
      GLchar infoLog[512];
      glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &amp;success);
      if (!success)
     {
      glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
      std::cout &lt;&lt; "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" &lt;&lt; infoLog &lt;&lt; std::endl;
      }
      // Fragment shader
      GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
      glShaderSource(fragmentShader, 1, &amp;fragmentShaderSource, NULL);
      glCompileShader(fragmentShader);
      // Check for compile time errors
      glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &amp;success);
      if (!success)
     {
      glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
      std::cout &lt;&lt; "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" &lt;&lt; infoLog &lt;&lt; std::endl;
     }
    // Link shaders
    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    // Check for linking errors
    glGetProgramiv(shaderProgram, GL_LINK_STATUS, &amp;success);
    if (!success) {
      glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
      std::cout &lt;&lt; "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" &lt;&lt; infoLog &lt;&lt; std::endl;
    }
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);
   }
  
  int main(int argc, char** argv)
  {
    glutInit(&amp;argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
    glutInitWindowSize(1024, 768);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("Tutorial 04");
  
    InitializeGlutCallbacks();
  
    // Must be done after glut is initialized!
    GLenum res = glewInit();
    if (res != GLEW_OK) {
      std::cout &lt;&lt; "Error: '%s'\n" &lt;&lt; glewGetErrorString(res) &lt;&lt; std::endl;
      return 1;
    }
  
   std::cout &lt;&lt; "GL version: %s\n" &lt;&lt; glGetString(GL_VERSION);
  
   glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  
   CreateVertexBuffer();
  
   shader();
  
   glutMainLoop();
  
   return 0;
   }
  </code></pre>
  
  <p>can anyone help me solve this problem? Thank you very much</p>