How to build module dependencies with CMakeLists.txt?

Consider an extension with two modules Wheel and Car :

.
├── CMakeLists.txt
├── Car
│   ├── CMakeLists.txt
│   ├── Widgets
│   │   ├── CMakeLists.txt
│   │   ├── qSlicerCarFooBarWidget.cxx
│   │   └── qSlicerCarFooBarWidget.h
│   ├── qSlicerCarModule.cxx
│   ├── qSlicerCarModule.h
│   ├── qSlicerCarModuleWidget.cxx
│   └── qSlicerCarModuleWidget.h
└── Wheel
    ├── CMakeLists.txt
    ├── Widgets
    │   ├── CMakeLists.txt
    │   ├── qSlicerWheelFooBarWidget.cxx
    │   └── qSlicerWheelFooBarWidget.h
    ├── qSlicerWheelModule.cxx
    ├── qSlicerWheelModule.h
    ├── qSlicerWheelModuleWidget.cxx
    └── qSlicerWheelModuleWidget.h

The module Car depends on some of the classes in module Wheel (for example qSlicerWheelFooBarWidget). What do I need to put in ./Car/CMakeLists.txt and ./CMakeLists.txt in order to successfully build Car?

Extension Wizard just creates an extension with two independent module projects; my build fails because project Car can’t find header files located in project Wheel.

Should I also manually override the virtual qSlicerCarModule::dependencies() const method and add Wheel?

Nothing special is needed, just add the library that you want to use to the list of target libraries and add the directory that contains header files to the include directories. See for example how CropVolume uses the Volumes module.

1 Like

Thanks, this did actually work! Funny that I had actually been looking at the CMakeLists.txt of CropVolume but couldn’t get it right the first time.

I guess I should add Wheel to the implementation of qSlicerWheelModule::dependencies() too:

QStringList qSlicerCarModule::dependencies() const
{
    return Superclass::dependencies() << "Wheel";
}

Yes, you need to add the modules that your modules depends on in dependencies() method. It is used for determining startup order of the modules.

1 Like