Question

The Issue

I am trying to a compile a very simple bit of code using MinGW's g++ that uses the SFML graphics library (for OpenGL). My link statement looks like this:

g++ -o hello.exe hello.o -Llib -lsfml-window-s -lsfml-system-s -lglew32s -lgdi32 -lopengl32

When I run this I a windows message saying "ld.exe has stopped working" and the command line response is:

c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: BFD (GNU Binutils) 2.23.2 assertion fail ../../binutils-2.23.2/bfd/cofflink.c:2
89
collect2.exe: error: ld returned 5 exit status

The following linking statement does work (however it requires me to use the dynamic libraries which is not ideal):

g++ -o hello.exe hello.cpp -Iinclude -Llib -lsfml-window -lsfml-system -lglew32s -lgdi32 -lopengl32

My code looks like this: (It is from the tutorials on the website open.gl)

// Link statically with GLEW
#define SFML_STATIC
#define GLEW_STATIC

// Headers
#include <GL/glew.h>
#include <SFML/Window.hpp>
#include <iostream>

// Shader sources
const GLchar* vertexSource =
    "#version 150 core\n"
    "in vec2 position;"
    "void main() {"
    " gl_Position = vec4(position, 0.0, 1.0);"
    "}";
const GLchar* fragmentSource =
    "#version 150 core\n"
    "out vec4 outColor;"
    "void main() {"
    " outColor = vec4(1.0, 1.0, 1.0, 1.0);"
    "}";

int main()
{
    sf::Window window(sf::VideoMode(800, 600, 32), "OpenGL", sf::Style::Titlebar | sf::Style::Close);

    // Initialize GLEW
    glewExperimental = GL_TRUE;
    glewInit();

    // Create Vertex Array Object
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    // Create a Vertex Buffer Object and copy the vertex data to it
    GLuint vbo;
    glGenBuffers(1, &vbo);

    GLfloat vertices[] = {
        0.0f, 0.5f,
        0.5f, -0.5f,
        -0.5f, -0.5f
    };

    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    // Create and compile the vertex shader
    GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexSource, NULL);
    glCompileShader(vertexShader);

    // Create and compile the fragment shader
    GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
    glCompileShader(fragmentShader);

    // Link the vertex and fragment shader into a shader program
    GLuint shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glBindFragDataLocation(shaderProgram, 0, "outColor");
    glLinkProgram(shaderProgram);
    glUseProgram(shaderProgram);



   // Specify the layout of the vertex data
    GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
    glEnableVertexAttribArray(posAttrib);
    glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);

    while (window.isOpen())
    {
        sf::Event windowEvent;
        while (window.pollEvent(windowEvent))
        {
            switch (windowEvent.type)
            {
            case sf::Event::Closed:
                window.close();
                break;
            }
        }

        // Clear the screen to black
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // Draw a triangle from the 3 vertices
        glDrawArrays(GL_TRIANGLES, 0, 3);

        // Swap buffers
        window.display();
    }

    glDeleteProgram(shaderProgram);
    glDeleteShader(fragmentShader);
    glDeleteShader(vertexShader);

    glDeleteBuffers(1, &vbo);

    glDeleteVertexArrays(1, &vao);
}

What I have already tried

  1. Reinstalling MinGW
  2. Creating a new folder and moving all the necessary files in and compiling/linking from scratch
Was it helpful?

Solution

I've just been wrestling this same issue and solved it by removing opengl32 from the link libraries. I think this might be caused by SFML already linking both OpenGL and GLEW. Linking to GLEW yourself causes multiple definitions, while linking to OpenGL seems to cause this crash.

OTHER TIPS

Farmer's solution also worked for me. However, to get it to link successfully, I had to put the GLEW link option before the SFML link options. Here's what my command line looks like...

g++ -o Open.GL_02_SFML.exe Open.GL_02_SFML_MAIN.o -lglew64s -lsfml-window-s -lsfml-system-s

Note that I am linking the 64-bit GLEW library instead of the 32-bit. I don't know if that makes any difference, but you can't be too careful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top