[CMake] Passing Function Arguments

Ryan Pavlik rpavlik at iastate.edu
Thu Jul 8 04:46:10 EDT 2010


  function (wcl _fileName _varName)
   set(out 0)
   file(READ "${_fileName}" fileData)
   if(fileData)
     string(REGEX MATCHALL "\n" lines "${fileData}")
     list(LENGTH lines out)
   endif()
   set(${+varName} ${out} PARENT_SCOPE)
endfunction()

Key parts you missed: "lines" (now _varName) contains a variable name 
when you enter the function, so to set the value of that variable, you 
need to set the value of ${lines}; must set with PARENT_SCOPE since a 
function opens a new scope; don't re-use variables if you want to be 
able to read your code in the future; it's a nice habit to put a _ in 
front of incoming variable names, for readability.

Hope this helps!

Ryan

On 7/8/10 3:35 AM, Johny wrote:
> I am trying to write a cmake function which imitates the functionality 
> of wc -l for unix. However i can't seem to be able to figure out how 
> to store the result into the variable which is passed  to the function.
>
> function (wcl fileName lines)
>   file(READ ${fileName} fileData)
>   if(fileData)
>     string(REGEX MATCHALL "\n" lines ${fileData})
>     list(LENGTH lines lines)
>   endif(fileData)
> endfunction()
>
> i call the function as
>
> wcl (${fileName} fileLen)
>
> and printing it gives no output
>
> message("${fileLen}")
>
> What am i doing wrong ?
>
> Regards
> Johny
>
>
> _______________________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at 
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at: 
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake


-- 
Ryan Pavlik
Human-Computer Interaction Graduate Student
Virtual Reality Applications Center
Iowa State University

rpavlik at iastate.edu
http://academic.cleardefinition.com/



More information about the CMake mailing list