티스토리 뷰

cmake

[CMake, ENG] Let's use "FUNCTION" in CMake

4whomtbts 2019. 12. 27. 02:22

I'm gonna cover "how to use FUNCTION in CMake"  at this post.

Function of CMake is not that different with a function of C, C++ and Java.

Function could have name and varidic arguments and It has Its own scope that means a function could refers 

variable of outer scope. But Its variables are terminated as soon as the function is terminated. 

By "argc" in function, You could know how many arguments are passed into function. 

By "argv", You could get every argument at once(each of argument is treated as element of list then argv is list

which contains all argument of function) 

EXAMPLE

cmake_minimum_required(VERSION 3.15)
project(cmaketoy)

set(CMAKE_CXX_STANDARD 11)

set (out_var "I'm not in a function")

function(hello_world)
    set (scope_test "In the scope")
    message("Hello world function!! hello world! = ${scope_test}")
    message(${out_var})
endfunction(hello_world)
message("Is scope_test alive?! = ${scope_test}")


function(fn_with_args a b c d)
    message(${ARGV}) # hello world We're args
    message(${ARGC}) # Total number of arguments = 4
    message(${ARGV0})
    message(${ARGV1})
    message(${ARGV2})
    message(${ARGV3})
    message("I will line all parameters by foreach")
    foreach(arg ${ARGV})
        message(${arg})
    endforeach()
endfunction()


hello_world()
fn_with_args("hello " "world " "We're " "args")
add_executable(cmaketoy main.cpp)

RESULT

 

Interesting uh? 

 

'cmake' 카테고리의 다른 글

[CMake, KOR/ENG] How to generate gRPC c++ code with CMake  (0) 2019.12.24
[CMake,ENG/KOR] compile protobuf with CMake  (0) 2019.12.24
[CMake, KOR] get_filename_component  (0) 2019.12.23
[CMake, KOR] foreach - 1  (0) 2019.12.23
[CMake,KOR] LIST  (0) 2019.12.23
댓글