[CMake] How to get a variable that is defined in another CMakeLists.txt file

Sylvain Benner benner at virtools.com
Thu Dec 21 04:44:45 EST 2006




> On Wednesday 20 December 2006 23:38, Mike Jackson wrote:
>   
>> So in Project B's CMakeLists.txt file I have:
>> ....
>> SUBDIRS(${PROJECT_SOURCE_DIR}/Project_A)
>> ....
>> ADD_EXECUTABLE( MyProgram ${SOURCES})
>> TARGET_LINK_LIBRARIES( MyProgram ${A_LIB_OUTPUT_NAME})
>>     
>
> As far as I understand it variables defined in the main directory (=CMakeLists.txt) will be defined 
> in the sub directory (=CMakeLists.txt), BUT, variables defined in sub directories will NOT be 
> defined in the main directory This creates scopes for variables  like in most other programming languages.
>
> so this should do it. (It does for me, however I use ADD_SUBDIRECTORY)
>
> SUBDIRS(${PROJECT_SOURCE_DIR}/Project_A)
> GET_DIRECTORY_PROPERTY(outvar DIRECTORY ${PROJECT_SOURCE_DIR}/Project_A DEFINITION A_LIB_OUTPUT_NAME)
>
> ADD_EXECUTABLE( MyProgram ${SOURCES})
> TARGET_LINK_LIBRARIES( MyProgram ${outvar})
>   
 From the man pages:

SUBDIRS
       Add a list of subdirectories to the build.

         SUBDIRS(dir1    dir2     ...[EXCLUDE_FROM_ALL     exclude_dir1
       exclude_dir2 ...] [PREORDER] )

       Add  a list of subdirectories to the build. *The ADD_SUBDIRECTORY
       command should be used instead of SUBDIRS although SUBDIRS  will
       still  work.

*So you should not continue to use SUBDIRS and use ADD_SUBDIRECTORY as 
Axel does.

-Sylvain
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://public.kitware.com/pipermail/cmake/attachments/20061221/8968685c/attachment.htm


More information about the CMake mailing list