# a basic CMakeLists.txt# set minimum cmake versioncmake_minimum_required(VERSION 3.5)# set project nameproject(hello-world)#elf file will be hello_elf, sourcefile should be hello-world.cadd_executable(hello_elf hello-world.c)
# cmake_minimum_required could be used as below# FATAL_ERROR reportcmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(hello-world)add_executable(hello_elf hello-world.c)
project()
# project cmd variable setcmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(hello-world)# project(hello-world) cmd sets PROJECT_NAME as "hello-world": elf will be "hello-world"add_executable(${PROJECT_NAME} hello-world.c)
LANGUAGES
#default languagecmake_minimum_required(VERSION 3.5 FATAL_ERROR)# default language *.c as CCproject(hello-world)add_executable(hello-world hello-world.c)
#clc languagecmake_minimum_required(VERSION 3.5 FATAL_ERROR)# specify clcproject(recipe-01 LANGUAGES C)add_executable(hello-world hello-world.c)
# c++ languagecmake_minimum_required(VERSION 3.5 FATAL_ERROR)# specify c++ language compilerproject(hello-world LANGUAGES CXX)add_executable(hello-world hello-world.cpp)
# variable set and headers cmake_minimum_required(VERSION 3.5)
project (hello_headers)# Create a sources variable with a link to all cpp files to compileset(SOURCES
src/Hello.cpp
src/main.cpp
)# Add an executable with the above sourcesadd_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
)
target_link_libraries
# a basic static libcmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(hello-world LANGUAGES CXX)# generate a library from sources , it is a static lib --- "libmessage.a"add_library(message
STATIC
Message.hpp
Message.cpp
)add_executable(hello-world hello-world.cpp)#link hello-world.o to "libmessage.a"target_link_libraries(hello-world message)
# using private publiccmake_minimum_required(VERSION 3.5)project(hello_library)#Generate the static library from the library sources --- "libhello_library.a"add_library(hello_library STATIC
src/Hello.cpp
)#libhello_library.a or any elf that links to libhello_library.a automatically set -I ${PROJECT_SOURCE_DIR}/includetarget_include_directories(hello_library
PUBLIC
${PROJECT_SOURCE_DIR}/include
)add_executable(hello_binary
src/main.cpp
)# link the new hello_library target with the hello_binary targettarget_link_libraries( hello_binary
PRIVATE
hello_library
)
# a basic shared libcmake_minimum_required(VERSION 3.5)project(hello_library)#Generate the shared library from the library sources, that is --- "libhello_library.so"add_library(hello_library SHARED
src/Hello.cpp
)# name hello::library is the same as hello_libraryadd_library(hello::library ALIAS hello_library)target_include_directories(hello_library
PUBLIC
${PROJECT_SOURCE_DIR}/include
)add_executable(hello_binary
src/main.cpp
)# link the new hello_library target with the hello_binary targettarget_link_libraries( hello_binary
PRIVATE
hello::library
)
# object lib, used for build libcmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(hello-world LANGUAGES CXX)# generate an object library from sourcesadd_library(message-objs
OBJECT
Message.hpp
Message.cpp
)# this is only needed for older compilers# but doesn't hurt either to have itset_target_properties(message-objs
PROPERTIES
POSITION_INDEPENDENT_CODE 1)# obj lib -> shared lib "libmessage.so"add_library(message-shared
SHARED
$<TARGET_OBJECTS:message-objs>)set_target_properties(message-shared
PROPERTIES
OUTPUT_NAME "message")# obj lib -> static lib "libmessage.a"add_library(message-static
STATIC
$<TARGET_OBJECTS:message-objs>)set_target_properties(message-static
PROPERTIES
OUTPUT_NAME "message")add_executable(hello-world hello-world.cpp)#link to target , not lib nametarget_link_libraries(hello-world message-static)
# a fotran shared libcmake_minimum_required(VERSION 3.5 FATAL_ERROR)project(hello-world LANGUAGES Fortran)# libmessage.soadd_library(message
SHARED
message.f90
)add_executable(hello-world hello-world.f90)target_link_libraries(hello-world message)
add_subdirectory
cmake_minimum_required (VERSION 3.5)project(subprojects)# Add sub directoriesadd_subdirectory(sublibrary1)add_subdirectory(sublibrary2)add_subdirectory(subbinary)