问题
我正在尝试使用使用SFML图形库(用于OpenGL)的MinGW&#g ++编译一个非常简单的代码。我的链接语句如下所示:
g++ -o hello.exe hello.o -Llib -lsfml-window-s -lsfml-system-s -lglew32s -lgdi32 -lopengl32
当我运行这个时,我发出一条Windows消息说" ld.exe已停止工作"命令行响应是:
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
以下链接语句确实有效(但它要求我使用不理想的动态库):
g++ -o hello.exe hello.cpp -Iinclude -Llib -lsfml-window -lsfml-system -lglew32s -lgdi32 -lopengl32
我的代码如下所示:(来自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);
}
我已尝试过的内容
答案 0 :(得分:2)
我一直在努力解决同样的问题,并通过从链接库中删除opengl32来解决它。我认为这可能是因为SFML已经连接了OpenGL和GLEW。自己链接到GLEW会导致多个定义,而链接到OpenGL似乎会导致此崩溃。
答案 1 :(得分:0)
Farmer的解决方案对我也有用。但是,要使其成功链接,我必须在 SFML链接选项之前添加GLEW链接选项 。这是我的命令行的样子......
g++ -o Open.GL_02_SFML.exe Open.GL_02_SFML_MAIN.o -lglew64s -lsfml-window-s -lsfml-system-s
请注意,我正在链接64位GLEW库而不是32位。我不知道这是否有所不同,但你不能太小心。