XCode C++ (Part 2)

Neil HaddleyMarch 8, 2023

OpenGL GLFW

Building an OpenGL application using C++ and XCode

I compiled a minimal OpenGL on my M1 MacBook Air.

I used GLFW (a cross platform library for OpenGL).

Overview

Here GLFW is used to create an application window with a given width and height and the title "Hello World".

In a loop the SwapBuffers function is called until the window is closed.

New Command Line Tool project

New Command Line Tool project

Named Hello OpenGL

Named Hello OpenGL

General Settings

General Settings

Default hello world code

Default hello world code

GLFW web site

GLFW web site

Hello OpenGL example

Hello OpenGL example

Missing library/header files

Missing library/header files

brew install glfw

brew install glfw

installed to folder /opt/homebrew/Cellar/glfw/3.3.8

installed to folder /opt/homebrew/Cellar/glfw/3.3.8

Add reference to /opt/homebrew/Cellar/glfw/3.3.8/ header files

Add reference to /opt/homebrew/Cellar/glfw/3.3.8/ header files

/opt/homebrew/Cellar/glfw/3.3.8/include

/opt/homebrew/Cellar/glfw/3.3.8/include

/opt/homebrew/Cellar/glfw/3.3.8/include reference added

/opt/homebrew/Cellar/glfw/3.3.8/include reference added

Add reference to libraries

Add reference to libraries

OpenGL.framework

OpenGL.framework

Add items

Add items

Add files

Add files

add a reference to dynamically loaded library /opt/homebrew/Cellar/glfw/3.3.8/lib/libglfw.3.3.dylib

add a reference to dynamically loaded library /opt/homebrew/Cellar/glfw/3.3.8/lib/libglfw.3.3.dylib

Library references updated

Library references updated

Hello OpenGL application running

Hello OpenGL application running

main.cpp

TEXT
1#include <GLFW/glfw3.h>
2
3int main(void)
4{
5    GLFWwindow* window;
6
7    /* Initialize the library */
8    if (!glfwInit())
9        return -1;
10
11    /* Create a windowed mode window and its OpenGL context */
12    window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
13    if (!window)
14    {
15        glfwTerminate();
16        return -1;
17    }
18
19    /* Make the window's context current */
20    glfwMakeContextCurrent(window);
21
22    /* Loop until the user closes the window */
23    while (!glfwWindowShouldClose(window))
24    {
25        /* Render here */
26        glClear(GL_COLOR_BUFFER_BIT);
27
28        /* Swap front and back buffers */
29        glfwSwapBuffers(window);
30
31        /* Poll for and process events */
32        glfwPollEvents();
33    }
34
35    glfwTerminate();
36    return 0;
37}