2016-12-15 3 views
6

निम्नलिखित उदाहरण में सीएमके में एक समारोह से प्रारंभिक वापसी कैसे होती है?सेमेक में किसी फ़ंक्शन से प्रारंभिक वापसी कैसे होती है?

function do_the_thing(HAS_PROPERTY_A) 
    # don't do things that have property A when property A is disabled globally 
    if (PROPERTY_A_DISABLED AND HAS_PROPERTY_A) 
     # What do you put here to return? 
    endif() 

    # do things and implement magic 
endfunction() 

उत्तर

5

आप return() (CMake मैनुअल पृष्ठ here) है, जो एक समारोह से वापस लौट आता है जब एक समारोह में कहा जाता है का उपयोग करें।

उदाहरण के लिए: में

cmake_minimum_required(VERSION 3.0) 
project(returntest) 

# note: your function syntax was wrong - the function name goes 
# after the parenthesis 
function (do_the_thing HAS_PROPERTY_A) 

    if (HAS_PROPERTY_A) 
     message(STATUS "Early Return") 
     return() 
    endif() 

    message(STATUS "Later Return") 
endfunction() 


do_the_thing(TRUE) 
do_the_thing(FALSE) 

परिणाम:

$ cmake ../returntest 
-- Early Return 
-- Later Return 
-- Configuring done 
... 

यह बाहर काम करता है, भी काम करता है: यदि आप एक include() एड फ़ाइल से इसे कहते हैं, यह Includer में लौटता है, अगर आप कहते हैं अगर add_subdirectory() के माध्यम से फ़ाइल से, यह आपको मूल फ़ाइल में वापस कर देगा।

+3

* डबल चेहरे * –

संबंधित मुद्दे