-1

I am about to set up and test an existing C++ project locally. It already has a CMakeLists.txt file, and my question is then

Do I have to use this file in any way to make the project run?

I struggle with some library inclusions ("cannot open source file" error in VS Code), and as this is similar to what the CMakeLists.txt file describes, I suspect I am supposed to run this file somehow. Still, all I find are articles about making and editing this file.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Anna Madsen
  • 384
  • 3
  • 15
  • 3
    Install cmake, learn cmake basics. That file describes how the program should be built. – sweenish Jan 13 '23 at 15:02
  • @sweenish replaced the dupe with a more appropriate one. – πάντα ῥεῖ Jan 13 '23 at 15:11
  • 1
    ***Do I have to use this file in any way to make the project run?*** It will make things much easier for you to use the `CMakeLists.txt` instead of spending lots of time to manually edit your `tasks.json` and `c_cpp_properties.json` to recreate the build commands defined in the CMakeLists.txt. Make sure you installed and enabled the CMakeTools extension of VSCode so it uses the CMakeLists.txt – drescherjm Jan 13 '23 at 15:11
  • 3
    Not my area of expertise but if you are using VSCode and CMake then you should install the [CMake Tools Extension](https://devblogs.microsoft.com/cppblog/cmake-tools-extension-for-visual-studio-code/) – john Jan 13 '23 at 15:25
  • After you have the CMake Tools extension installed if you have an issue you should show your logs and explain the problem. We can't help debug an issue without information from you. – drescherjm Jan 13 '23 at 15:39

1 Answers1

3

cmake describes itself as a way to build, test, and package software. Software like this is needed because compiling larger C++ projects quickly becomes a complicated affair, and no one wants to re-type long compile commands, or rebuild parts of the program that don't need to be rebuilt. People also don't want to write makefiles by hand for many of the same reasons.

You will need to download and install cmake, and if you're on Windows, a reboot for good measure.

Actually using cmake to simply build your project does assume that you have a proper C++ environment already installed. From a terminal, you can navigate to the project's root directory and issue the following commands.

cmake -B build -S .
cmake --build build

The first command will create a build directory called build where cmake will store its cache and create a build solution according to your C++ environment. We want to keep cmake output separate from the source as it's a lot cleaner from a directory tree or general organization standpoint. The same CMakeLists.txt file can create a makefile for Linux/Mac users, a VS solution for pure Visual Studio, an XCode solution for Mac, etc. CMakeLists.txt describes how the program should be built, and cmake will create the actual build instructions for you.

The second command will actually compile and link your code.

As mentioned in the comments, VS Code offers a cmake extension. It should detect your 'kit' (C++ environment) automatically, but if it doesn't it can be configured accordingly. Building is then a click away. By default, it does create a new directory to store build files and artifacts.

cmake documentation can be a bear to go through, but I have found this to be a great reference for basic usage.

sweenish
  • 4,793
  • 3
  • 12
  • 23