I am useing OpenGL with GLEW and GLFW. I have created two shaders and they compile fine:
#include <iostream> #include <GL/glew.h> #include <OpenGL/OpenGL.h> #include <GLFW/glfw3.h> static unsigned int CompileShader( unsigned int type, const std::string source){ unsigned int id = glCreateShader(type); const char* src = source.c_str(); glShaderSource(id, 1, &src, nullptr); glCompileShader(id); int result; glGetShaderiv(id, GL_COMPILE_STATUS, &result); if (result == GL_FALSE){ int length; glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length); char* message = (char*)alloca(length * sizeof(char)); glGetShaderInfoLog(id, length, &length, message); std::cout <<"failed to compile " << (type == GL_VERTEX_SHADER ? "vertex" : "fragment") << " shader" <<"\n"; std::cout << message << "\n"; glDeleteShader(id); return 0; } return id; } static int CreateShader(const std::string& VertexShader, const std::string& FragmentShader){ unsigned int program = glCreateProgram(); unsigned int vs = CompileShader(GL_VERTEX_SHADER, VertexShader); unsigned int fs = CompileShader(GL_FRAGMENT_SHADER, FragmentShader); glAttachShader(program, vs); glAttachShader(program, fs); glLinkProgram(program); glValidateProgram(program); glDeleteShader(vs); glDeleteShader(fs); return program; } int main(void){ /* Initialize the library */ GLFWwindow* window; if (!glfwInit()){return -1;} glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); #ifdef __APPLE__ glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); #endif /* Create a windowed mode window and its OpenGL context */ window = glfwCreateWindow(640, 480, "Gaston", NULL, NULL); if (!window){ glfwTerminate(); return -1; } /* Make the window's context current */ glfwMakeContextCurrent(window); if (glewInit() != GLEW_OK){ std::cout <<"glewunit(); failed\n"; } float positions[6] = { -0.5f, -0.5f, -.9f, .5f, 0.5f, 0.5f }; unsigned int buffer; glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(positions) * sizeof(float), positions, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2, 0); glBindBuffer(GL_ARRAY_BUFFER, 0); std::string vertexshader = "#version 330 core\n" "\n" "layout(location = 0) in vec4 position;\n" "\n" "void main()\n" "{\n" " gl_Position = position;\n" "}\n"; std::string fragmentshader = "#version 330 core\n" "\n" "layout(location = 0) out vec4 colour;\n" "\n" "void main()\n" "{\n" " colour = vec4(1.0, 0.0, 0.0, 1.0);\n" "}\n"; unsigned int shader = CreateShader(vertexshader, fragmentshader); glUseProgram(shader); /* Loop until the user closes the window */ while (!glfwWindowShouldClose(window)){ /* Render here */ glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_TRIANGLES, 0, 3); /* Swap front and back buffers */ glfwSwapBuffers(window); /* Poll for and process events */ glfwPollEvents(); } glfwTerminate(); return 0; } ...but the triangle does not show up. All I get is a blank screen.
The ideal result is a red triangle.

sizeof(positions)is already the correct size. Multiplying that bysizeof floatis wrong. Makingpositionavec4and then only supplying avec2is misleading but not per se wrong.