-1

I modified a program trying to render a triangle and I get the following errors :

 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

I have only one file named triangle_with_shader.cpp and the code in this file:

#include <iostream>
#include <string.h>
#include <GL/glew.h>
#include <GL/glut.h>
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, &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, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);
    // Check for compile time errors
    GLint success;
    GLchar infoLog[512];
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
    if (!success)
   {
    glGetShaderInfoLog(vertexShader, 512, NULL, infoLog);
    std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    // Fragment shader
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);
    // Check for compile time errors
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (!success)
   {
    glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog);
    std::cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
   }
  // Link shaders
  GLuint shaderProgram = glCreateProgram();
  glAttachShader(shaderProgram, vertexShader);
  glAttachShader(shaderProgram, fragmentShader);
  glLinkProgram(shaderProgram);
  // Check for linking errors
  glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
  if (!success) {
    glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
    std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
  }
  glDeleteShader(vertexShader);
  glDeleteShader(fragmentShader);
 }

int main(int argc, char** argv)
{
  glutInit(&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 << "Error: '%s'\n" << glewGetErrorString(res) << std::endl;
    return 1;
  }

 std::cout << "GL version: %s\n" << glGetString(GL_VERSION);

 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

 CreateVertexBuffer();

 shader();

 glutMainLoop();

 return 0;
 }

can anyone help me solve this problem? Thank you very much

tiger
  • 25
  • 6

1 Answers1

0

This:

 g++ -Wall -o triangle_with_shader.cpp -lglut -lGLU -lGLEW

is wrong - you are trying to make your output executable file a .cpp file. You want something like:

 g++ -Wall triangle_with_shader.cpp -lglut -lGLU -lGLEW  -o myprog

and you then run myprog.

  • hi , I run with the correct compile comand and another error occurs. – tiger Jan 19 '17 at 20:06
  • @Holy He edited into his question. –  Jan 19 '17 at 20:10
  • @HolyBlackCat hi, see above I edited it.Am i losing some libraries ? – tiger Jan 19 '17 at 20:11
  • @tiger Oops, sorry. You need to also add `-lopengl32`. The order may be important, so try to put it between different libraries. – HolyBlackCat Jan 19 '17 at 20:11
  • I tried to put -lopengl32 in front of -lglut , between -lglut and -lGLU, between -lGLU and -lGLEW , got the same error ? – tiger Jan 19 '17 at 20:15
  • @tiger You've also tried to put it at the end, right? – HolyBlackCat Jan 19 '17 at 20:17
  • @HolyBlackCat yes, right in front of -o – tiger Jan 19 '17 at 20:18
  • 1
    You didn't mention which OS your on, so it might also be -lGL. opengl32 is windows, GL is linux. – BDL Jan 19 '17 at 20:23
  • I'm using ubuntu. g++ -Wall triangle_with_shader.cpp -lGL -lglut -lGLU -lGLEW -o triangle compiled successfully, but there is shader linking errors. Thanks! – tiger Jan 19 '17 at 20:26
  • If you need help for this, please ask a new question and provide the relevant shader (-loading) code. – BDL Jan 19 '17 at 20:29
  • 1
    @BDL Ok. Thanks, Ill try if I can fix that by myself first. – tiger Jan 19 '17 at 20:30
  • @HolyBlackCat sorry that I forget to tell my operating system. with g++ -Wall triangle_with_shader.cpp -lGL -lglut -lGLU -lGLEW -o triangle I compiled that. Thanks – tiger Jan 19 '17 at 20:31