Segmentation fault when running GLFW/GLEW on macos M1 chip

  apple-m1, c++, glfw, macos, segmentation-fault

So I am trying to compile and run a C++/OpenGL application on the new macbook air with the m1 chip. I am using GLFW and GLEW. I used Homebrew to install both libraries on my mac. This is the source code I am using:

#include <iostream>
#include <GL/glew.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>

int main(){

    glfwInit();
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    GLFWwindow* window = glfwCreateWindow(800, 600, "Window", NULL, NULL);
    glfwMakeContextCurrent(window);
    glewInit();

    glViewport(0, 0, 800, 600);

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    float vertices[] = {
        0.0f, 0.0f,
        1.0f, 1.0f,
        1.0f, 0.0f,
    };

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

    while(!glfwWindowShouldClose(window)){
        glfwPollEvents();

        glDrawArrays(GL_TRIANGLES, 0, 3);
    }

    glfwTerminate();

    return 0;
}

I don’t have a linux/windows machine to test this code on.

Source: Windows Questions C++

LEAVE A COMMENT