Building and Compiling - ktopolov/DatabaseSystem GitHub Wiki

To create a compiled project, we must be able to build the project. This repo will use the cmake software to handle building and the g++ compiler for C++ code.

Overview of CMake

CMake requires a CMakeLists.txt within each directory that contains source code. When we invoke cmake, it generates a Makefile which tells the compiler where all of our files are located. Essentially, we could manually create this Makefile, but that is extremely annoying and difficult.

Assume you have a repo with code lying in the /src folder:

  • Create a CMakeLists.txt folder in the source code directory called /src/CMakeLists.txt. An example looks like:
# Specify CMAKE version
cmake_minimum_required(VERSION 3.16.3)

# Give the project a name. CMake will automativally store this name as the variable PROJECT_NAME
project(Database)

# add_executable(name_of_output_file, name_of_source_file)
# This will produce an executable file from main.cpp with same name as PROJECT_NAME variable
add_executable(outfile.o main.cpp)

The workflow is:

  • If any files have been moved/added or this is your first time, run cmake -S <PATH_TO_TOP_LEVEL_CMakeLists.txt> -B <DESIRED_BUILD_DIR> to have CMake generate a makefile. The -S flag tells CMake where is the first CMAleLists.txt file to look at. The -B flag tells it where the desired build directory is, and it will put the make files there. If the -B directory does not yet exist, it will be created.
  • Once the make files exist, to build the project trun make -C <{PATH_TO_BUILD_DIR>, which will make the project.

In the above example, there should be a outfile.o in the build directory that can be run