# Specify Minimum Version, Not Required
cmake_minimum_required(VERSION 3.2)
# Project Name
project(chat)
# Define the Executable File Generated by This Project
#add_executable(ExecutableName SourceFileName.c)
add_executable(chat_app)
# When there are many files, define the file names as variables
#set(VariableName SourceFile)
set(SRC_LIST add.cpp div.cpp main.cpp mult.cpp sub.cpp)
#add_executable(ExecutableName SourceFiles) Use $ as a value symbol and wrap variables in {}
add_executable(chat_app ${SRC_LIST})
# Specify the C++ Standard to Use
# Add -std=c++11
set(CMAKE_CXX_STANDARD 11)
# Add -std=c++14
set(CMAKE_CXX_STANDARD 14)
# Add -std=c++17
set(CMAKE_CXX_STANDARD 17)
# When this standard is used in the CMake command line
# Add -std=c++11
cmake CMakeLists.txt file_path -DCMAKE_CXX_STANDARD=11
# Add -std=c++14
cmake CMakeLists.txt file_path -DCMAKE_CXX_STANDARD=14
# Add -std=c++17
cmake CMakeLists.txt file_path -DCMAKE_CXX_STANDARD=17
# Use set to specify the output path
set(HOME /home/robin/Linux/Sort)
set(EXECUTABLE_OUTPUT_PATH ${HOME}/bin)
# There are two ways to search for source files: aux & file
#aux_source_directory(FilePath VariableName)
aux_source_directory(${CMAKE_CURRENT_DIR}/src SRC_LIST)
#file(GLOB/GLOB_RECURSE Search Method VariableName FilePath and File Type)
#GLOB generates a list of all file names that meet the conditions in the specified directory and stores them in a variable
#GLOB_RECURSE searches recursively, searching the current directory and all its subdirectories
file(GLOB SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
############################################ Template One ####################################################
# Top-Level Directory
cmake_minimum_required(VERSION 3.2)
project(chat)
aux_source_directory(${PROJECT_SOURCE_DIR} SRC)
include_directories(${PROJECT_SOURCE_DIR}/include)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
add_executable(chatapp ${SRC})
########################################## Static and Dynamic Libraries ################################################
# The command to create a static library is: add_library(LibraryName STATIC/SHARED SourceFile1 SourceFile2)
# A library consists of three parts: lib + LibraryName + Extension.
# Dynamic library extensions: Linux–> .so, win–> .dll;
# Static library: Linux–> .a, win–> .lib;
############################################ Template Two — Dynamic Library Generation ############################################
# Top-Level Directory
cmake_minimum_required(VERSION 3.2)
project(chat)
#aux_source_directory(${PROJECT_SOURCE_DIR} SRC)
# Source Files
file(GLOB SRC ${PROJECT_SOURCE_DIR}/src/*.cpp)
# Header Files
include_directories(${PROJECT_SOURCE_DIR}/include)
#set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# File Output Path
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#add_executable(chatapp ${SRC})
add_library(calc SHARED ${SRC})
############################################ Template Two — Static Library Generation ##########################################
# Top-Level Directory
cmake_minimum_required(VERSION 3.2)
project(chat)
#aux_source_directory(${PROJECT_SOURCE_DIR} SRC)
# Source Files
file(GLOB SRC ${PROJECT_SOURCE_DIR}/src/*.cpp)
# Header Files
include_directories(${PROJECT_SOURCE_DIR}/include)
#set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# File Output Path
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#add_executable(chatapp ${SRC})
add_library(calc STATIC ${SRC})
############################################ Template Three — Link Static Library to Generate Executable #################################
# Top-Level Directory
cmake_minimum_required(VERSION 3.2)
project(chat)
#aux_source_directory(${PROJECT_SOURCE_DIR} SRC)
# Source Files, these source files do not include files in libraries, but can include files like main.c
file(GLOB SRC ${PROJECT_SOURCE_DIR}/src/*.cpp)
# Header Files
include_directories(${PROJECT_SOURCE_DIR}/include)
#set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# File Output Path
#set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# Link Library Files, if not system files, it may cause missing file issues, need to specify static library path
link_libraries(calc)
# Specify the static library path, assuming the static library is in the lib directory of the project
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)
add_executable(chatapp ${SRC})
############################################ Template Three — Link Dynamic Library to Generate Executable #################################
# Top-Level Directory
cmake_minimum_required(VERSION 3.2)
project(chat)
#aux_source_directory(${PROJECT_SOURCE_DIR} SRC)
# Source Files, these source files do not include files in libraries, but can include files like main.c
file(GLOB SRC ${PROJECT_SOURCE_DIR}/src/*.cpp)
# Header Files
include_directories(${PROJECT_SOURCE_DIR}/include)
#set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# File Output Path
#set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# Link Library Files, if not system files, it may cause missing file issues, need to specify static library path
link_libraries(calc)
# Specify the static library path, assuming the static library is in the lib directory of the project
link_directories(${CMAKE_CURRENT_SOURCE_DIR}/lib)
add_executable(chatapp ${SRC})
# At the end of the CMake code, add the name of the dynamic library to link
# Specify the name of the dynamic library that the executable program should link, assuming the dynamic library name is pthread
#target_link_libraries(ProgramName DynamicLibraryName)
target_link_libraries(app pthread)
Below are two links from Da Bing:
CMake Nanny Level Tutorial (Part 1) | Da Bing Loves Programming
CMake Nanny Level Tutorial (Part 2) | Da Bing Loves Programming
Makefile | Da Bing Loves Programming
Salute to Teacher Da Bing!!!!