gpt4 book ai didi

c++ - MinGW - ld.exe 在链接 SFML 静态库时崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:45:18 24 4
gpt4 key购买 nike

问题

我正在尝试使用 MinGW 的 g++ 编译一段非常简单的代码,该代码使用 SFML 图形库(用于 OpenGL)。我的链接语句如下所示:

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);
}

我已经尝试过的

  1. 重新安装 MinGW
  2. 创建一个新文件夹并将所有必要的文件移入并从头开始编译/链接

最佳答案

我一直在努力解决同样的问题,并通过从链接库中删除 opengl32 解决了这个问题。我认为这可能是由于 SFML 已经链接了 OpenGL 和 GLEW。自己链接到 GLEW 会导致多个定义,而链接到 OpenGL 似乎会导致此崩溃。

关于c++ - MinGW - ld.exe 在链接 SFML 静态库时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22671976/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com