CMake is useful in compiling source code for get binaries. Compiling a library from the source code guarantees that the resulting library is perfectly tailored for your CPU/OS, a luxury pre-compiled binaries don't always provide. It is also important that binaries you get target x64 platform. In this guide you learn how to install CMake, process source code, build a solution, get binaries, and test them. It is assumed your platform is Windows 10 or 11, and IDE Visual Studio 2022. Check the Desktop development with C++ workload box as you download it, also click Language pack" and check "English" box.
Steps
Part 1
Part 1 of 6:
Configuring Visual Studio
-
1Highlight what you expect to do. Highlight step or sub-step you expect to do and then do it. See as example picture above.
-
Download Visual Studio 2022. If you have not done so you can download it from https://www.visualstudio.com/downloads .
- Check the Desktop development with C++ workload box as you download. If necessary scroll down the page.
- Click "Language pack" and check "English" box.
Advertisement
Part 2
Part 2 of 6:
Installing CMake
-
1Compiling a library from the source code guarantees that the resulting library is perfectly tailored for your CPU/OS, a luxury pre-compiled binaries don't always provide. It is also important that binaries you get target x64 platform.
-
2Create folders GL and GLP. Open Windows's File Explorer > Navigate to disk (directory) C.
- If folders GL and GLP exist it's okay.
- If they do not, right click in empty area > select New > Folder > type GL > hit ↵ Enter . By same way create folder GLP .
-
3Download CMake. Right-click on following address and select Open Link in New Window https://cmake.org/download/ . Scroll down the page and find "Latest Release (3.25.1)", (or latest). In second "Platform" list, find (see image above) "Windows x64 ZIP" and click the beside entry cmake-3.25.1-windows-x86_64.zip (or latest) > in opening wizard select Save file .
-
4Copy and unzip the zip folder.
- If the downloaded folder doesn't contain line with stripes (unzipped folder) click it > right click > Copy .
- If downloaded folder's icon contains vertical line with stripes (zipped folder), double click it for get unzipped folder, (or alternatively, click folder > right click > in drop-down menu select Extract all
).
- When unzipping (extracting files) is finished, click unzip folder cmake-3.25.1-windows-x86_64.zip (or latest) > right click > Copy .
- Go to drive (directory) C: > GL > right click > Paste .
- (Alternatively click folder "cmake-3.25.1-windows-x86_64" and drag into folder "GL").
- When copying is finished click folder "cmake-3.25.1-windows-x86_64" > right click > select "Rename" > type CMake > hit ↵ Enter > double click it > double click folder bin > inside you should see CMake's logo next to file name cmake-gui > double click this file. If wizard "Windows protect your PC" appears, click More information > Run anyway. Now on your screen you have CMake GUI.
- Each time you need CMake, navigate to C:\ > GL > double click folder CMake > double click "bin" > double click file cmake-gui (the one with CMake's logo).
Advertisement
Part 3
Part 3 of 6:
Processing Source Code
-
1Download source code. As an example we use GLFW. Right-click on following address and select Open Link in New Window https://www.glfw.org/download.html . Select "Source package". In opened wizard select Save File .
-
2Unzip and copy folder.
- In downloading window click zip folder glfw-3.3.8 (or latest) > if you get no stripped (unzipped) icon, it's okay. If not, right click > select Extract all .
- Copy unzip folder and paste in C:\GL.
- (Alternatively click downloaded folder and drag into folder C:\GL).
- Now in directory C:\GL, you have unzip folder glfw-3.3.8 (or latest). Click on its name > delete name > type: GLFWsrc > hit ↵ Enter > close downloading window.
-
3Compile source code. See above image.
- Where is the source code . Copy (attention: do not copy any space) C:\GL\GLFWsrc and paste in CMake GUI first text field.
- Where to build the binaries . Copy (attention: do not copy any space) C:\GL\GLFWsrc\build an paste in second text field.
- Configure and generate. In CMake GUI, click Configure > in wizard Create Directory click Yes . Be sure you have downloaded Visual Studio 2022 > in wizard "Specify the generator for this project" , check whether visual studio version is 17 2022 > click Finish .
- When, in CMake GUI, you read: "Configuring done", click Generate
. You should read: "Generating done".
- If, instead, wizard "Error" is thrown, click OK > check whether you have downloaded visual studio 2022. If you have, in CMake GUI click "File" > click "Delete Cache" > in thrown wizard "Delete Cache", click Yes > close CMake GUI > open it again > repeat this step.
-
4Build your solution.
- Copy C:\GL\GLFWsrc\build and paste in File Explorer Address Bar > hit ↵ Enter > Double click "GLFW.sln", or "GLFW", or "ALL_BUILD.vcxproj". An instance of Visual Studio appears. Wait until in main menu Build entry appears. Click it > "Build Solution".
- Wait till you read the last line in "Output" window: ========== Build: 32 succeeded, 0 failed, 0 up-to-date, 3 skipped" ==========
- Numbers of "succeeded" and "skipped" change in glfw versions. Today (12-Nov-2022) is 32 and 3 respectively.
- Copy C:\GL\GLFWsrc\build\src\Debug and paste in File Explorer Address Bar > hit ↵ Enter . You should see file glfw3.lib .
- Close cmake GUI.
Advertisement
Part 4
Part 4 of 6:
Creating Project with compiled Binaries
-
1Create empty project. See above image. Title "Visual Studio 2019" doesn't matter. .
- In Visual Studio main menu click File > New > Project… .
- In Create a new project wizard, select Empty Project > click Next .
- In Configure your new project
wizard,
- In Project name text box type (or copy and paste): GLFWsrc-0
- Copy C:\GLP and paste in Location text box .
- Check box Place solution and project in the same directory .
- Click Create .
-
Add your source file to the Project. See above image. Project's name in it is "SDLproject", but it doesn't matter.
- In Solution Explorer wizard, right click the Source Files folder (the last one) > click Add > New Item…
- In the Add New Item - GLFWsrc-0 wizard, click C++ File (.cpp) (the first one) from the middle of the window. In the Name text box, type Main.cpp .
- The Location should be C:\GLP\GLFWsrc-0. If it's not, copy C:\GLP\GLFWsrc-0 and paste.
- Click the Add button. The file will open in the text editor but leave it blank for now.
Advertisement
Part 6
Part 6 of 6:
Testing project GLFWsrc-0 and Correcting errors
-
Test code. Right-click on following address and select Open Link in New Window https://www.glfw.org/docs/3.0/quick.html#quick_example > copy code > go to Visual Studio GUI and paste in Main.cpp code area. Hit Ctrl + F5 . If everything gone well two windows appear: one black and other with a rotating colored triangle in black background. Good job.
-
Correct errors if any. Files are missing. Copy C:\GLP\GLFWsrc-0 and paste in File Explorer Address Bar. You should see file you added Main.cpp , and 4 other files added by Visual Studio. If they are missing you missed add file Main.cpp . Go to Part 3, step 2, and add it now.
- In "Error List" if you see error about
- file with extension .h go to Part 3, step 3, sub-steps 1 and 2 and follow instructions.
- file with extension .lib go Part 3, step 3, sub-step 3, and follow instructions. Also to sub-step 4.
- "entry point must be defined" go to Part 3, step 3, sub-step 5.
- For other errors, if you can't correct them, close Visual Studio > delete project folder GLFWsrc-0 which lives in C:\GLP\ > open Visual Studio > repeat set up from Part 3 .
Advertisement - In "Error List" if you see error about
Expert Q&A
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit
Advertisement
Video
About this article
Thanks to all authors for creating a page that has been read 14,200 times.
Advertisement