The name of the project set by the current project().
CMAKE_PROJECT_NAME
the name of the first project set by the project() command, i.e. the top level project.
PROJECT_SOURCE_DIR
The source director of the current project.
PROJECT_BINARY_DIR
The build directory for the current project.
name_SOURCE_DIR
The source directory of the project called “name”. In this example the source directories created would be sublibrary1_SOURCE_DIR, sublibrary2_SOURCE_DIR, and subbinary_SOURCE_DIR
name_BINARY_DIR
The binary directory of the project called “name”. In this example the binary directories created would be sublibrary1_BINARY_DIR, sublibrary2_BINARY_DIR, and subbinary_BINARY_DIR
# Create a sources variable with a link to all cpp files to compile
set(SOURCES
src/Hello.cpp
src/main.cpp
)
# Add an executable with the above sources
add_executable(hello_headers ${SOURCES})
# Set the minimum version of CMake that can be used
# To find the cmake version run
# $ cmake --version
cmake_minimum_required(VERSION 3.5)
# Set the project name
project (hello_headers)
# Create a sources variable with a link to all cpp files to compile
set(SOURCES
src/Hello.cpp
src/main.cpp
)
# Add an executable with the above sources
add_executable(hello_headers ${SOURCES})
# Set the directories that should be included in the build command for this target
# when running g++ these will be included as -I/directory/path/
target_include_directories(hello_headers
PRIVATE
${PROJECT_SOURCE_DIR}/include
)
cmake_minimum_required(VERSION 3.5)project(hello_library)############################################################# Create a library#############################################################Generate the static library from the library sourcesadd_library(hello_library STATIC
src/Hello.cpp
)target_include_directories(hello_library
PUBLIC
${PROJECT_SOURCE_DIR}/include
)############################################################# Create an executable############################################################# Add an executable with the above sourcesadd_executable(hello_binary
src/main.cpp
)# link the new hello_library target with the hello_binary targettarget_link_libraries( hello_binary
PRIVATE
hello_library
)
cmake_minimum_required(VERSION 3.5)project(hello_library)############################################################# Create a library#############################################################Generate the shared library from the library sourcesadd_library(hello_library SHARED
src/Hello.cpp
)add_library(hello::library ALIAS hello_library)target_include_directories(hello_library
PUBLIC
${PROJECT_SOURCE_DIR}/include
)############################################################# Create an executable############################################################# Add an executable with the above sourcesadd_executable(hello_binary
src/main.cpp
)# link the new hello_library target with the hello_binary targettarget_link_libraries( hello_binary
PRIVATE
hello::library
)