Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to build CMake project on Windows

📅 2014-Jun-09 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ cmake ⬩ 📚 Archive

The typical method of building a project that uses CMake is a bit different on Windows.

  1. Create a subdirectory in the project directory named build.

  2. Open a command prompt in the build directory and type cmake-gui ..

  3. In the CMake GUI, click Configure. Choose the Visual Studio version you want to use and build for.

  4. Have a look at the CMake variables marked in red. Remember that many of these variables might be filled using your environment variables. If you find that something is not set or set incorrectly, it might be due to a wrong value in one of your environment variables. Sometimes, the build will not succeed if one of these variables is not set correctly.

  5. Take note of the variable CMAKE_INSTALL_PREFIX. On Linux, the build results are installed to standard locations like /usr/include. On Windows, the results of the build will be installed to the directory in CMAKE_INSTALL_PREFIX. If you do not like its default location, set it to something else.

  6. Click Configure again, to allow CMake get the right values. It typically needs two clicks of Configure.

  7. Click Generate to generate the Visual Studio solution file. It will be placed in the build directory.

  8. Open the generated Visual Studio solution file. If your CMAKE_INSTALL_PREFIX was a system directory, like C:\Program Files, you will need to open this solution file as Administrator.

  9. The solution file might have one or many projects. The two main projects that concern us is ALL_BUILD and INSTALL.

  10. Build ALL_BUILD project in both Release and Debug modes.

  11. To install the header files, libraries and executables generated to the location pointed to in CMAKE_INSTALL_PREFIX, build the INSTALL project in both Release and Debug mode. If you get an error here, it typically means that you are running Visual Studio as normal user, who does not have permissions to place these files in a directory that needs Administrator permissions.

That is it. You have built a CMake project on Windows :)


© 2023 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 Mastodon📧 Email