[CMake] Avoiding error when using add_subdirectory twice on same path (CMake 2.6.4)

Clinton Stimpson clinton at elemtech.com
Tue Jun 16 19:54:01 EDT 2009


Tyler Roscoe wrote:
> On Tue, Jun 16, 2009 at 07:19:52PM -0400, Michael Jackson wrote:
>   
>> myApp:
>>
>> project(myApp)
>>
>> add_subdirectory(commonLib)
>> add_subdirectory(lib1)
>> add_subdirectory(lib2)
>>
>> add_executable(myApp ${files})
>> target_link_libraries(myApp lib1 lib2)
>>
>>
>> lib1/lib2:
>>
>> project(lib1)
>>
>> add_library(lib1 ${files})
>> target_link_libraries(lib1 commonLib)
>>
>> I am doing exactly this currently in a project and it seems to work  
>> fine. YMMV.
>>     
>
> Right, but this doesn't work in the case where I go down and run CMake
> on lib1/CMakeLists.txt instead of running it from myApp/CMakeLists.txt.
>
> I also feel that this is less clear than the example I posted because if
> I'm reading lib1's CMakeLists, I will have a wtf moment when I encounter
> commonLib since it is not defined in lib1. Instead, I have to "just
> know" that commonLib gets pulled in by this myApp thing which happens to
> be the thing that causes CMake to process lib1.
>
> Like I said, my thought process here is influenced by our (apparently
> goofy?) object-oriented design.
>
>   
If you still have to do that, perhaps you could create more 
CMakeLists.txt files as entry points for CMake, or add more logic to 
your existing CMake files by relying on CMAKE_HOME_DIRECTORY or 
something like that.

So for lib1/lib2
if(NOT CMAKE_HOME_DIRECTORY MATCHES "${CMAKE_CURRENT_SOURCE_DIR}")
  add_subdirectory(../commonlib commonlib)
endif(NOT CMAKE_HOME_DIRECTORY MATCHES "${CMAKE_CURRENT_SOURCE_DIR}")

Clint




More information about the CMake mailing list