cmake_minimum_required(VERSION 4.0)
project(cmake)
set(CMAKE_CXX_STANDARD 20)
if (" ${CMAKE_SOURCE_DIR}" STREQUAL " ${CMAKE_BINARY_DIR}")
MESSAGE(FATAL_ERROR "错误: 禁止源码内编译
请为构建文件创建一个单独的目录
")
endif ()
add_executable(cmake main.cpp
mylib.cpp
mylib.h)
#使用 option变量
option(BUILD_MYLIB "构建mylib目标")
if (BUILD_MYLIB)
add_library(MyLib
mylib.cpp
mylib.h)
else ()
message("忽略构建mylib目标")
endif ()
# for循环
set(list1 2 4 6 8)
set(list2 1 3 5 7)
foreach (v IN LISTS list1 list2)
message("v = ${v}")
endforeach ()
message("============================")
foreach (v IN ITEMS 1 2 3 4 5 6 7 8)
message("v = ${v}")
endforeach ()
message("==========================")
foreach (v 1 2 3 4 5 6 7)
message("v = ${v}")
endforeach ()
message("==========================")
foreach (v IN ITEMS ${list1})
message("v= ${v}")
endforeach ()
message(" zip list ========分别获取列表中的2个元素 ")
foreach (v IN ZIP_LISTS list1 list2)
message("v : (${v_0},${v_1})")
endforeach ()
message("设置步长")
set(start 1)
set(end 8)
set(step 2)
foreach (v RANGE ${start} ${end} ${step})
message("v=${v}")
endforeach ()
#设置步长
#v=1
#v=3
#v=5
#v=7
#=========函数
function(my_function a b)
message("========================")
message("a=${a},b=${b}")
message("========================")
message("argc:${ARGC}")
message("argv: ${ARGV}")
message("argv0: ${ARGV0},argv1:${ARGV1}")
message("argn:${ARGN}")
endfunction()
my_function(hello world 3 4 5)
#=====未命名参数
# ARGC 参数的数量
# ARGV 参数列表
# ARGN 未命名参数列表
# 还可以用ARGV0 ARGV1 来代码第0个,第一个参数
#=======关键字参数
function(my_function2 targetName)
message("==========my function2 ===========")
message("targetName:${targetName}")
message("argv:${ARGV}")
endfunction()
my_function2(USE_LIB
MYLIB_PATH "/usr/local/lib"
SOURCE "main.cpp"
INCLUDES "include" "include2"
)
# 输出
#==========my function2===========
#targetName:USE_LIB
#argv:USE_LIB;MYLIB_PATH;/usr/local/lib;SOURCE;main.cpp;INCLUDES;include;include2
# 参数解析: cmake_parse_arguments
function(my_function3 targetName)
set(options USE_MYLIB)
set(oneValueArgs MYLIB_PATH)
set(multiValueArgs SOURCES INCLUDES)
cmake_parse_arguments(arg_myfunction3 "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
message("
USE_MYLIB:${arg_myfunction3_USE_MYLIB}
MYLIB_PATH:${arg_myfunction3_MYLIB_PATH}
SOURCE:${arg_myfunction3_SOURCES}
INCLUDES:${arg_myfunction3_INCLUDES}
targetName:${targetName}
")
endfunction()
my_function3(USE_LIB
MYLIB_PATH "/usr/local/lib"
SOURCES "main.cpp"
INCLUDES "include" "include2"
)
#输出
#USE_MYLIB:FALSE
#MYLIB_PATH:/usr/local/lib
#SOURCE:main.cpp
#INCLUDES:include;include2
#targetName:USE_LIB
# un parsed arguments 和 keywords missing values
function(my_function4 targetName)
set(options USE_MYLIB)
set(oneValueArgs MYLIB_PATH)
set(multiValueArgs SOURCES INCLUDES)
cmake_parse_arguments(arg_myfunction4 "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})
message("=my function4=============================")
message("
UNPARSED_ARGUMENTS:${arg_myfunction4_UNPARSED_ARGUMENTS}
KEYWORDS_MISSING_VALUES: ${arg_myfunction4_KEYWORDS_MISSING_VALUES}
")
endfunction()
my_function4(hello
MYLIB_PATH "/usr/local/lib" "/usr/local/lib2"
SOURCES
INCLUDES "include" "include2"
)
#=my function4=============================
#
#UNPARSED_ARGUMENTS:/usr/local/lib2
#KEYWORDS_MISSING_VALUES: SOURCES
# 函数返回值: 方式1
function(my_fun1 returnValue)
set(${returnValue} "hello world" PARENT_SCOPE)
endfunction()
my_fun1(result)
message("result:${result}") # hello world
#方式2 return 命令
function(my_fun2 returnValue)
set(${returnValue} "hello world2")
return(PROPAGATE ${returnValue})
endfunction()
my_fun2(result)
message("result2=${result}") #result2=hello world2
# 宏
function(my_fun3 a)
message("================my_fun3==============")
if(a)
message("===> a is true")
elseif ()
message("===> a is false")
endif ()
endfunction()
macro(my_macro a )
message("===============my macro==================")
if(a)
message("===> a is true")
else()
message("===> a is false")
endif ()
endmacro()
my_fun3(hello) # true 在函数中参数a用做变量来判断。不是FALSE 常量,所以为true
my_macro(hello) # false 宏参数最终会替换成字符串hello, 字符串除了常量TRUE以外都为假
## 宏里面不能使用return 否则会导致整个代码提前返回
# list
message("===============list ================")
set(listVar a b c)
list(LENGTH listVar listLen) # 获取长度
message("list length is ${listLen}") # list length is 3
list(GET listVar 0 firstItem) # 获取第0个元素
message("first item is ${firstItem}") # first item is a
list(APPEND listVar d) # 末尾追加元素
list(JOIN listVar "-" outtStr) # 使用连接符链接起来
message("outtStr:${outtStr}") # outtStr:a-b-c-d
# string 字符串
message("=================string 字符串===================")
string(FIND interesting in fIndex) # 在字符串‘interesting’中查找 ‘in’ 的下标
string(FIND interesting in rIndex REVERSE) # 反向查找
message("fIndex=${fIndex}") # fIndex=0
message("rIndex=${rIndex}") # fIndex=8
# math 命令
message("================math===========")
math(EXPR a 1+2*6/2)
math(EXPR b ${a}*3)
message("a=${a}\
b=${b}") #a=7 b=21
# file命令
file(GLOB varFilelist LIST_DIRECTORIES false CONFIGURE_DEPENDS RELATIVE ${CMAKE_SOURCE_DIR} "*.cpp")
message("var file list :${varFilelist}") # var file list :main.cpp;mylib.cpp
# CONFIGURE_DEPENDS 如果文件列表发送了变化,则重新构建生成,不建议使用
# add_executable(MyApp ${varFilelist})