Step 3 Create CMakeLists.txt

Create CMakeLists.txt

CMake is a cross-platform tool for project building, it uses CMakeLists.txt which specifies the include and library directories as well as your source code files.

Under the project root folder, such as proj1, create CMakeLists.txt

putting the following contents into the file, or download CMakeLists.txt from Blackboard

cmake_minimum_required(VERSION 3.26.0)
project(proj01 VERSION 1.0.0)
cmake_policy(SET CMP0072 NEW)

# place for finding Findglfw3.cmake
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake)

# external opengl related libraries 
find_package(OpenGL REQUIRED)
find_package(glfw3 REQUIRED)


# adding source files to our exectuable programs
# Note: it is not a good practice to put glad.c in the src folder
# Ideally, it should be put under external/ and used as an external library
# to avoid unnecessary compiling
add_executable(run01 src/main.cpp src/glad.c)

# specify include directories
target_include_directories(run01 PRIVATE include)

# specify library directories
target_link_libraries(run01 ${GLFW3_LIBRARY} OpenGL::GL)

Install CMakeTools extension in VSCode

Revise cmake/Findglfw3.cmake if you have a different external or include folder

Revise the CMAKE include and library directories of GLFW3 if you are putting GLFW in a different location other than "external" or are using a difference vrsion of visual studio

Test if CMake is working

Adding glad and GLFW headers in main.cpp

Open the project with VSCode or Visual Studio (choose "Open Folder")

If the project compiles successful, CMake is working

Last updated