# Lines that begin with a # are comments # set the minimum required version of cmake, usually the first line cmake_minimum_required(VERSION 3.22) # project_name sets the name of the project and causes cmake to # find the c and c++ compilers project(project_name) # Find dependencies. # Many libraries ship with files that allow CMake to find them # Then general behavior is to call "find_package" but the options # provided are package specific. Usually there is then a CMAKE variable # That is defined to reference the library # here: we find the eigen library as per the instruction # https://eigen.tuxfamily.org/dox/TopicCMakeGuide.html find_package(Eigen3 3.3 REQUIRED NO_MODULE) # Create a library. Can specify if it is shared or static but usually # you don't need or want to. # name is the name of the library without the extension or lib prefix # name creates a cmake "target" (in this case the target is called "libname" add_library(libname src/libfile1.cpp src/libfile2.cpp) # Use target_include_directories so that #include"mylibrary/header.hpp" works # The use of the and is because when # using the library from the build directory or after installation the header # files are actually in different locations. # During the build, the headers are read from the source code directory # When used from the installed location, headers are in the # system include/ directory target_include_directories(libname PUBLIC $ $) # specify additional compilation flags for the library # Public causes the flags to propagate to anything # that links against this library target_compile_options(libname PUBLIC -Wall -Wextra -pedantic) # Enable c++23 support. # PUBLIC causes the features to propagate to anything # that links against this library # whereas PRIVATE would make it apply only to the library itself target_compile_features(libname PUBLIC cxx_std_23) # Create an executable from the following source code files # The Name of the executable creates a cmake "target" add_executable(Name src/Name_main.cpp) # Use target_link_libraries to add dependencies to a "target" # (e.g., a library or executable) # This will automatically add all required library files # that need to be linked # and paths to th locations of header files. # Because libname has PUBLIC cxx_std_23 as a compile feature # Name will also be compiled with c++23 as the standard. target_link_libraries(Name Eigen3::Eigen libname) # install the include files by copying the whole include directory install(DIRECTORY include/mylibrary DESTINATION include) # Create a CMake Exported Target containing the lib and exe. # Also create CMake Export called project_name-targets # The CMake Export contains files that allow other CMake projects # to find this project. It must be installed separately. install(TARGETS Name libname EXPORT project_name-targets) # The project_name-targets created by install(TARGETS) needs to be installed. # install(EXPORT ...) will generate a file called project_name-config.cmake # that contains the exported targets. # After installation this file will then be found when calling # find_package(project_name) from another cmake project # A user can then target_link_libraries(target project_name::library) # to use the libraries installed here install(EXPORT project_name-targets FILE project_name-config.cmake NAMESPACE project_name:: DESTINATION lib/cmake/${PROJECT_NAME})