Acces macro/functions to subfolders

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Acces macro/functions to subfolders

Olivier Pierard
Dear all,

In order to be able to use a macro/function from several sub-folders, I
would like to define it only once in the main CMakeLists.txt.  However,
after some basic tests, It seems impossible to call this macro/function
from subfolders.  Is is possible to do it ?

By the way, what's the basic difference between macro/function in cmake
?  Related to preprocessing like in C/C++ ?  What's the advantage of
distinguish them here ?

Thanks for your help,

Olivier

--
Olivier Pierard

CENAERO, Virtual Manufacturing Group

Bâtiment EOLE, 1er étage
Rue des Frères Wright, 29
B-6041 Gosselies
BELGIUM

Tel: +32.(0)71.919374
Fax: +32.(0)71.919370

http://www.cenaero.be/

+-+-+- Disclaimer +-+-+-

http://www.cenaero.be/disclaimer

_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Acces macro/functions to subfolders

Tyler Roscoe
On Wed, Dec 16, 2009 at 05:55:54PM +0100, Olivier Pierard wrote:
> In order to be able to use a macro/function from several sub-folders, I
> would like to define it only once in the main CMakeLists.txt.  However,
> after some basic tests, It seems impossible to call this macro/function
> from subfolders.  Is is possible to do it ?

It works for me.

> By the way, what's the basic difference between macro/function in cmake
> ?  Related to preprocessing like in C/C++ ?  What's the advantage of
> distinguish them here ?

The details are in the docs, though you have to read carefully to
notice. I think the biggest difference is that the contents of a macro
are dropped in, as though with #define. Functions are an actual function
call and functions have their own scope.

tyler
_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Acces macro/functions to subfolders

Michael Wild

On 16. Dec, 2009, at 18:28 , Tyler Roscoe wrote:

> On Wed, Dec 16, 2009 at 05:55:54PM +0100, Olivier Pierard wrote:
>> In order to be able to use a macro/function from several sub-folders, I
>> would like to define it only once in the main CMakeLists.txt.  However,
>> after some basic tests, It seems impossible to call this macro/function
>> from subfolders.  Is is possible to do it ?
>
> It works for me.

You have to make sure that the definition is BEFORE the add_subdirectory call.

>
>> By the way, what's the basic difference between macro/function in cmake
>> ?  Related to preprocessing like in C/C++ ?  What's the advantage of
>> distinguish them here ?
>
> The details are in the docs, though you have to read carefully to
> notice. I think the biggest difference is that the contents of a macro
> are dropped in, as though with #define. Functions are an actual function
> call and functions have their own scope.
>
> tyler

Two of the important differences:

- All variables are in the caller-scope (i.e. if you set/define a new variable, the change will be visible from the caller). In functions variables are local to the function and you need to use set(... PARENT_SCOPE) to propagate the value to the caller-scope.

- Macro arguments do NOT behave like normal variables. You have to always reference them with ${arg-name}, i.e. if(arg-name) or similar does not work. In functions the arguments are just regular variables.

Usually what you want is functions (unless you have to support an old CMake version that does not have them). Only in special cases where you want the automatic propagation of variable values to the caller-space should you use macros.

HTH

Michael
_______________________________________________
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
Reply | Threaded
Open this post in threaded view
|

Re: Acces macro/functions to subfolders

Marcel Loose
In reply to this post by Olivier Pierard
On Wed, 2009-12-16 at 09:28 -0800, Tyler Roscoe wrote:

> On Wed, Dec 16, 2009 at 05:55:54PM +0100, Olivier Pierard wrote:
> > In order to be able to use a macro/function from several sub-folders, I
> > would like to define it only once in the main CMakeLists.txt.  However,
> > after some basic tests, It seems impossible to call this macro/function
> > from subfolders.  Is is possible to do it ?
>
> It works for me.
>
> > By the way, what's the basic difference between macro/function in cmake
> > ?  Related to preprocessing like in C/C++ ?  What's the advantage of
> > distinguish them here ?
>
> The details are in the docs, though you have to read carefully to
> notice. I think the biggest difference is that the contents of a macro
> are dropped in, as though with #define. Functions are an actual function
> call and functions have their own scope.
>
> tyler

Another important difference is that, for functions, ARGV and ARGN and
ARGVn (n=1..) are real variables that have local scope.

Best regards,
Marcel Loose.




_______________________________________________
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