Setting up CLion for OpenGL Programming on Windows 11

In this how-to, I will be showing how to setup the JetBrains CLion IDE for modern OpenGL programming in C++

Prerequisites

Before we can proceed to setting up CLion for an OpenGL project we have to get some required libraries, namely GLFW and GLAD.

GLFW is an open source multi-platform library for OpenGL. It provides a simple API for creating windows, contexts, and surfaces, receiving input and events. It is written in C and supports Windows, macOS, and Linux. This makes creating a window very easy on any platform otherwise you would have to write lots of code on each platform just to create a simple window.

GLAD is used to load and manage runtime function pointers for OpenGL and related graphics APIs. It acts as a bridge so your code can call modern GPU driver function that are not hardcoded into your OS.

I used GLFW 3.5.1 binaries for Win64 but you can use whatever the latest version is at the time you do this. Go to this link to get the files. Looks like the latest version as I write this is 3.5.1. Click on the "64-bit Windows binaries" button and save the zip file somewhere on your computer.

I used GLAD for OpenGL version 3.3 since we won't need any advanced features present in the other versions. Feel free to use whatever version of OpenGL you want to though. Go to this link. At the glad homepage you will be presented with a form to fill out and generate the glad files to download for the version of OpenGL you select. Choose these values:

  1. For Language: C++
  2. For API gl: 3.3
  3. For Specification: OpenGL
  4. For Profile: Core
  5. Don't worry about Extensions
  6. Make sure the "Generate a loader" checkbox is checked
  7. Click on the "Generate" button at the bottom right

Save the glad.zip file somewhere on your computer.


Below is a diagram of the CLionProjects folder that CLion automatically creates when you make a a new project, if you have not changed it on your computer. You can see in the diagram above where to place the GLFW and GLAD files that you downloaded. Make sure you use this exact file structure and notice that the GLFW and GLAD files are not in the "HelloWindow" project folder (except for the glad.c file). I purposely placed the "include" and "lib" folders one level above any project folders so I could use them in any OpenGL project in the "CLionProjects" folder. That way you don't have to place them in each project. Pay attention to the contents of the "CMakeLists.txt" file in the screenshot of the project window below it uses this folder structure to tell CLion where the library files are located.

Creating a new C++ project in CLion

Now that all of the GLFW and GLAD files are in place, open the CLion IDE and if it opens in an existing project you may have been working on, close that project and you will be back at the welcome screen where you can see any existing projects you have. If you don't have any existing projects, you will go right to the welcome screen when you start the CLion IDE. Below is a screenshot of the welcome screen.

To create our project click on the "New Project" button at the top pointed out by the red arrow symbol I have added to the picture. You will be presented with the New Project dialog shown below.

In the "New Project" dialog you will see I have added some red arrows numbered 1 to 4.

  1. Select "C++ Executable" on the left.
  2. Replace the "untitled" project name in the "Location" with the name of your project. In this example I have chosen the name "HelloWindow".
  3. Select C++17 for the "Language Standard"
  4. Then click the "Create" button at the bottom of the dialog.

You have created the project in CLion as show in the screenshot below.

The image above shows the contents of the "CMakeLists.txt" file. Make sure you use this exact file contents. Notice that it points to the "include" and "lib" folders one level above the project folder (../include) and (../lib).
Also notice that the glad.c file is in the "HelloWindow" project along side of the main.cpp file that the project creates for you. You have to delete the default code that CLion puts in the main.cpp file and replace it with the code below:
Contents of the main.cpp file you need to use in your project.

If you have setup your CLion project as instructed, you will be able to run the program and see the OpenGL window as displayed in the following screenshot:

Good luck and enjoy your journey learning OpenGL with C++ in CLion IDE.