Setting up OpenGL, GLEW, and FreeGLUT in Visual C++

This post walks you through setting up OpenGL, GLEW, and FreeGLUT support in Visual C++ in order to compile the samples properly. My original thought was to put this in an appendix after all of the chapters were completed, but there’s need for it now, so here’s the extremely verbose step-by-step explanation:

I’m assuming that you have Visual Studio installed or at least Visual C++ Express, which you can get for free right here, and and install the Windows SDK as well if you get the Visual C++ Express version, which you can get here.

Step 1: Get the Libraries

1.) Download the binary version of FreeGLUT from this website, make sure the version you get it 2.6.0 or higher.
2.) Download the latest binary version of GLEW from their site, make sure to download the one labeled Windows 32-bit.

Step 2: Copy the Libraries

Decompress the files you just downloaded and open up the folder for FreeGLUT:

step 1

Open up another explorer window, and type %PROGRAMFILES% in the address bar, or %PROGRAMFILES(X86)% only if you’re on a 64-bit system. Find the sub folder Microsoft SDKs\Windows\(highest version):

step 2

Copy the contents from FreeGLUT’s lib folder into the Windows SDK’s Lib folder.
Copy the contents from FreeGLUT’s include\GL folder into the Windows SDK’s Include\gl folder.

Now, in the Explorer window currently pointed to FreeGLUT’s location, navigate to the location where you extracted GLEW:

step 3

Copy the contents from GLEW’s lib folder into the Windows SDK’s Lib folder.
Copy the contents from GLEW’s include\GL folder into the Windows SDK’s Include\gl folder.

Step 3: Creating the Project

The next step is to set up the Visual C++ project, so start up Visual C++ Express or Visual Studio and in the menu bar, go to File -> New -> Project…, and click on the Visual C++ heading:

step 4

Make sure you have the Win32 Console Application option selected, type in a name for the project, and click on the OK button, and the following screen should appear:

step 5

Click on next, but do not click the Finish button until you have checked the Empty Project check-box and your options look exactly like this:

step 7

Step 4: Adding a Source File

Now that you have an empty project set up, we need to add a source file, which we’ll name main.c for this tutorial. Right-click on the project, navigate to Add and click on New Item…:

step 8

From the list, pick the option C++ File (.cpp), and change its Name to main.c (notice the change from .cpp to c):

step 9

It’s important to have this file in your project before you enter the settings screen since certain settings would not appear on the screen without at least one source file available. After you click the Add button, the new file should show up in your Solution Explorer and open up in the text editor:

step 10

Step 5: Project Settings (Compiler)

Right click on your project in the solution explorer again, and click on Properties in the context menu:

step 11

IMPORTANT: Make sure that before setting any settings to set the Configuration drop-down to All Configurations:

step 12

The first change we’re going to make is in the General property page under the Configuration Properties heading. in the property page, change the Character Set option under Project Defaults to Not Set (see highlighted screenshot):

step 13

Next, go to the C/C++ property page, click on the General sub-page and change the Warning Level setting to Level4 (/W4), and the Treat Warnings As Errors setting to Yes (/WX) (see highlighted screenshot):

step 14

Next, go to the Preprocessor sub-page, expand the drop-down list next to the Preprocessor Definitions option, and click on Edit…. In the screen that comes up, insert a new line at the top of the options list, and add _CRT_SECURE_NO_WARNINGS so that your list looks like this:

step 15

IMPORTANT: Click on the OK button, and go to the Advanced sub-page. Change the Compile As option to Compile as C Code (/TC) (see highlighted screenshot):

step 16

Step 6: Project Settings (Linker)

Go to the Linker property page, and click on the Input sub-page. Expand the drop-down list next to the Additional Dependencies option and click on Edit…. In the window, add glew32.lib followed by a newline, and freeglut.lib so that your list looks like this:

step 17

Click on the OK button, and when you’re back at the settings screen, click on the OK button as well to save your settings.

Step 7: Compiling/Running the Project

Now it’s time to test your setup, so grab some source code from one of the chapters (I suggest chapter 1 for simplicity) and paste it into main.c:

step 18

Go to the main menu, click on Build, click on Build Solution and your build should succeed without any warnings or errors:

step 19

The next step is not to debug, but to copy some extra files into your project’s output folder. So open up an explorer window, and point it to the location of your project output, which at this point should be the Debug folder in the solution’s root folder. You should see something similar to this:

step 20

Open another Explorer window to the location where you extracted GLEW, and copy glew32.dll from the bin folder into the output folder of your project. Now, navigate the explorer window to the location where you extracted FreeGLUT, and copy freeglut.dll from that folder to the output folder of your project, which should now look like this:

step 21

You should now be able to compile, debug, and run your program without warnings and errors:

step 22

Final Thoughts

This is of course one of many ways of setting up a project, but should point you in the right direction.

One thing that I like to do for certain projects is include the DLLs as part of my solution in a Solution Folder, and copy them after a successful build with a custom Post-Build step.

A cleaner way that doesn’t require copying the libraries is to compile the libraries yourself and include them in your Solution as a Visual C++ static library project and set your OpenGL projects to depend on the output (build order). It increases the size of your solution, but being able to tweak source code and control what’s inside a compiled binary is priceless.

If you have remarks, questions, or concerns, post them in the comment section below.

This entry was posted in Information and tagged , , , . Bookmark the permalink.
  • Ferran Ferri

    Hello! 
    Just discovered this book and is a great work!About the “final thoughts”, I like include the full source codes into my MSVC solution. Both projects are added as a dependency for my opengl project, so I have fresh DLLs generated if I change some code of the libs.Also, all projects could share a “bin” directory to output generated binarys, so the DLLs are always installed in every execution.

    • http://openglbook.com/ E. Luten

      Exactly, that way you won’t have to copy .lib files around.

  • Duxillyz

    Following the instructions to the letter (reinstalled glew 1.6.0-win32 and freeglut 2.6.0-3 a few times to be sure); I encountered a problem where, using the first example code from chapter 1, the console failed to create another window and instead echoed the message..
     ”freeglut (C:UsersXDocumentsVisual Studio 2010ProjectsOpenGLBookDebugOpenGLBook.exe): Unable to create OpenGL 4.0 context (flags 2, profile 1) ” 
    ..just before closing the application.

    I also received some messages from the debug window:

    it said this at the beginning:
    “‘OpenGLBook.exe’: Loaded ‘C:UsersXDocumentsVisual Studio 2010ProjectsOpenGLBookDebugOpenGLBook.exe’, Symbols loaded.”

    ..repeated this for some  .dll files, always not finding/opening the PDB file:
    “‘OpenGLBook.exe’: Loaded ‘C:WindowsSysWOW64.dll’, Cannot find or open the PDB file”

    ..and also had this somewhere between the .dll file warnings:
    “‘OpenGLBook.exe’: Loaded ‘C:UsersXDocumentsVisual Studio 2010ProjectsOpenGLBookDebugfreeglut.dll’, Binary was not built with debug information.”

    ..ending with this:
    “The thread ‘Win32 Thread’ (0x1b64) has exited with code 1 (0×1).
    The program ‘[4804] OpenGLBook.exe: Native’ has exited with code 1 (0×1).”

    if it helps, the compiler I’m using is Visual C++ 2010 Express. I followed the project settings exactly but for whatever reason it’s still running incorrectly.. any way to fix this?

    • http://openglbook.com/ E. Luten

      “Unable to create OpenGL 4.0 context” means that your graphics card likely doesn’t support OpenGL 4.0. Check your GPU’s vendor to see if it does, if not, try running the compatibility examples for OpenGL 3.3, which run on DirectX 10 level hardware.

  • Judy

    Hi Eddy,
    I noticed in some other tutorials, they say to put the glew.dll into the systems folder but your’s doesn’t say that.  Is that assumed or not needed because I’m using VC++ express and it will link? 

    • Przemysław Lib

      When you put glew.dll in VS folders, all apps runed by VS (via run button, or any other way VC starts apps) will use glew.dll first from VS folders, then if there are not there VS will try to use OS folders.

      On the other hand if you try to run your app in system where you do not have VS, then you have to provide your own glew.dll with .exe or make sure glew.dll is in OS folders.

      VS folders are for development purposes, and they may contain different .dlls than you OS foderls contain.

      • Judy

        okay that makes sense.  But I don’t understand the paragraphs that were in the final thoughts:
        One thing that I like to do for certain projects is include the DLLs as part of my solution in a Solution Folder, and copy them after a successful build with a custom Post-Build step.A cleaner way that doesn’t require copying the libraries is to compile the libraries yourself and include them in your Solution as a Visual C++ static library project and set your OpenGL projects to depend on the output (build order). It increases the size of your solution, but being able to tweak source code and control what’s inside a compiled binary is priceless.How do I do paragraph 1 or 2 so I don’t have to recopy for each new project?

        • http://openglbook.com/ E. Luten

          Paragraph 1: It is possible to add existing items of any type to your solution, so simply add a solution folder, right click it, click on add existing items, and add the DLL files mentioned above to it. What you can do then in your projects is add a new post-build rule with the following commands:

          copy /Y “$(SolutionDir)*.dll” “$(OutDir)”

          Paragraph 2: I meant to create a new project for FreeGLUT in your solution, as well as a project for GLEW in your solution. By default, the output directories are the same for all of the projects in your solution, so the DLL (or LIB if y0u compile them as static libs) is *always* in the directory of your EXE.
          Hope that clears things up for you.