[CMake] function vs macro

Michael Jackson mike.jackson at bluequartz.net
Tue Mar 3 21:11:36 EST 2009


The subtle difference is this line from the "macro" help section:

Note that the parameters to a macro and values such as ARGN are not  
variables in the usual CMake sense. They are string replacements much  
like the c preprocessor would do with a macro. If you want true CMake  
variables you should look at the function command.

I know I ran into this problem before but it is hard to explain. Maybe  
one of the regular CMake guys can do a better job.

What happened to me was the following:

macro(MyMacro ${var})
	configure_file(.. )
endmacro()

set(var "Something")
MyMacro(${var})

and in the file that was used as input I had something like:

#define VAR @var@

But when I ran CMake the configured file ended up only being:

#define VAR

instead of

#define VAR Something

The issue was that CMake generated the following CMake code for the  
macro.

macro(MyMacro Something)
configure_file(... )
endmacro()

So when the configure_file was run there was no ${var} variable  
defined. I ended up having to do:

set (var ${var}) which looks weird but then actually defined a cmake  
variable called 'var'.

I _think_ a function would have done what I wanted. Comments Anyone?

_________________________________________________________
Mike Jackson                  mike.jackson at bluequartz.net
BlueQuartz Software                    www.bluequartz.net
Principal Software Engineer                  Dayton, Ohio



On Mar 3, 2009, at 8:51 PM, Robert Dailey wrote:

> Hi,
>
> After reading the CMake 2.6 documentation, I'm not really clear on  
> the differences between a macro and a function. Could someone  
> explain this in a bit more detail? Based on the differences, which  
> should I use?
> _______________________________________________
> 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



More information about the CMake mailing list