$ cd ~cs40/examples [examples]$ mkdir build [examples]$ cd build [build]$ cmake .. [build]$ make -j8If we already have a build directory and have previously run cmake .., we do not need to run cmake again. We can just run make in any of the subdirectories of the build folder.
Separate from the green folders are the build directories. Let's focus on the one on the right labeled cmake build. The contents of these subfolders are generated automatically by cmake, make and the compiler, using the CMakeLists.txt files in the green source tree as instructions. Since these files can be automatically generated from the green source tree, there is no need to include them in git. In fact, the .gitignore file in the source tree is usually set to ignore the entire build directory.
This design has a number of advantages:
The primary disadvantage of using a separate build directory is that it can sometimes get confusing if we are in the build tree or the source tree. Additionally it can be confusing to know where to run make or cmake. The figure above should help reduce confusion.
To avoid this problem, first create an empty build directory and cd into that directory. Next run cmake followed by the path to top-level CMakeLists.txt. In this case, it would be the CMakeLists.txt file in the examples directory. The figure above shows two cases.
In the first case, we made the build directory inside the examples folder. This is the style I use in class.
$ cd ~cs40/examples [examples]$ mkdir build [examples]$ cd build [build]$ cmake .. [build]$ make -j8
In the second case, we made a build-examples-Desktop-Default directory at the same depth as the examples folder. QtCreator prefers this style by default, though you can configure these preferences. In essence QtCreator did the following
$ cd ~cs40/ [cs40]$ mkdir build-examples-Desktop-Default [cs40]$ cd build-examples-Desktop-Default [build-examples-Desktop-Default]$ cmake ../examples [build-examples-Desktop-Default]$ make -j8Note that the cmake command in this case is ../examples as we have to go back one directory (into ~/cs40) and then down into the examples folder to find the CMakeLists.txt folder.
CMake will create makefiles inside many of the folders inside a build directory and you can run make or make -j8 in any of these directories and it will do the right thing. You can even run make in your src tree, but since there is no Makefile in your source tree, only CMakeLists.txt files, nothing will happen.
If you have still have questions, feel free to follow up on Piazza and I'll try to update this document
Soon we will need to edit our CMakeLists.txt and I'll post instructions on how to do that here as well. Stay tuned.