In this section, we'll take a look at how to set up OpenGL using GLFW as the provider to create a render window with relative linking. In the previous section, we discussed absolute linking, So, let's just have a quick overview of what absolute and relative linking actually are.
Absolute linking is a process where you specifically link your libraries to the project. For example, if you create a project and you are linking up libraries like GLFW and GLEW, while linking them up, you specifically put in the paths of the directory they are in. If they're in the C: drive, you would actually put the explicit directory. But, if you move the library files to any other location, then you would have to update your Visual Studio project with the changed path.
With relative linking, the libraries are actually linked, but relative to the project. So, you don't say libraries are in the C: drive; rather, you say those relatively link to your project from a particular folder. So even if you move your libraries, it won't affect your project. It is a great method for transporting the project from one machine to an other. This method of development is preferable when you're working on a platform that doesn't really have a good visual editor; for example, platforms, such as Unity or Unreal.
So, let's get started with relatively linking our libraries and creating an OpenGL render window. Let's open up Visual Studio and follow these steps:
- Click on Create new project... and go to Visual C++ | Windows Desktop | Windows Console Application. Name the project GLApp (since we are learning how to relatively link the libraries, we've created a different project).
- Then, in the New Project window, click on the Browse... button. Go to the OpenGL folder that we've created on the desktop (we are using this folder structure format to understand relative linking). Just select the folder and then click OK.
- One more thing you need to do before starting the project is to create a folder called External Libraries within the OpenGL folder on desktop where you have saved your project. Extract the library files for GLEW and GLFW in the External Libraries folder.
- Now, we'll right-click on the project in the Solution Explorer window. Go to Add | New Item. Select C++ File and name it main.cpp, and then click on the Add button.
- Next, right-click on the project in the Solution Explorer window and go to Properties.
- A Property Pages window will pop up; click on C/C++ | General and then go to Additional Include Directories. In it, click on the dropdown and then click on <Edit>:
Adding include directories
- Then, click on the new button. As we are doing relative linking in this section, we won't click on the three dots. Clicking on them is for absolute linking only, as we have to browse to the directory where we have stored the libraries.
- In the textbox highlighted in the preceding screenshot, type $(SolutionDir); this command refers to the folder that contains our .sln file. So if we were to specify a folder in the path, whenever we do something new in the project it'd be relatively linked to wherever that file is located.
- To include the files in our project, add the paths as shown in the following screenshot and then click on the OK button:
- Next, we'll link up the libraries. So, in the Property Pages window, we'll go to Linker | General, and then go to Additional Library Directories. Click on the dropdown, click on Edit, and then click on New. Add the paths as shown in the following screenshot, and then click OK and then Apply:
- Now, we've got one more thing to do, and that is to link up the .lib files. So, go to Linker | Input, and then go to Additional Dependencies. Click on the dropdown and then click on <Edit>. Now, in the textbox, just type opengl32.lib. This library file isn't downloaded with GLFW or GLEW; it's built into Windows. Next, on a new line, just type glew32s.lib and now for GLFW lib-vc2015, type glfw3.lib. Then, click OK and click on the Apply button.
Whichever linking process you are comfortable with, you can follow that. With either of the methods that you use to link the libraries, there's one last step that we need complete before we can begin with coding, and that is to copy and paste the dynamic link library into our project.