From Slicer Wiki
Jump to: navigation, search
Home < Documentation < 4.2 < Developers < Tutorials < ExternalModuleWriting

For the latest Slicer documentation, visit the read-the-docs.

This tutorial describes how to make a (Slicer4 / QT) loadable module that is compiled outside of the slicer build tree.

Consider also reading

QtPortInSlicer - How to write a module
How to write a loadable module inside Slicer


1) Create the module directory

Use the module wizard to generate files and directory based on a template.

Linux Mac Windows

Start terminal (bash or, then type the following:

$ cd /path/to/Slicer4
$ python Utilities/Scripts/ MY_MODULE_NAME

Start cmd.exe terminal, then type the following:

$ cd C:\path\to\Slicer4
$ ..\Slicer4-Superbuild\python-build\PCBuild\python.exe Utilities\Scripts\ MY_MODULE_NAME

Replace MY_MODULE_NAME with the name of the module you want (e.g. HelloWorld)
After running the script, a new directory MY_MODULE_NAME will be created in Slicer4/Modules/Loadable

2) Setting up your CMakeLists.txt

  • Download FindSlicer.cmake
  • Edit CMakeLists.txt, and make it (1) detect Slicer, (2) load the Slicer "Use file", and (3) add your module subdirectory.
 find_package (Slicer QUIET)
   include ("${Slicer_USE_FILE}")
     add_subdirectory (MY_MODULE_NAME)
   endif ()
 endif ()
  • You might need to undo some of Slicer's settings. If you don't think you need this, then you probably don't. But if you need it, you can do something like this:
 if (Slicer_USE_FILE)
   get_directory_property (OLD_INCLUDE_DIR INCLUDE_DIRECTORIES)
   set_directory_properties (PROPERTIES INCLUDE_DIRECTORIES "")

   include ("${Slicer_USE_FILE}")

   set_directory_properties (PROPERTIES INCLUDE_DIRECTORIES
 endif ()
  • You might need to copy over TestingMacros.h, it depends how you set up your include directories above.
 $ cp ${SD}/TestingMacros.h MY_MODULE_NAME

3) Compile your module

  • Compile your module. At this point, it will compile. You may see some warnings from CMake
CMake Warning at /home/gsharp/build/slicer-4/Slicer4/CMake/vtkMacroKitPythonWrap.cmake:86 (ADD_LIBRARY):
 Cannot generate a safe runtime search path for target
 vtkSlicerMY_MODULE_NAMEModuleLogicPython because files in some directories
 may conflict with libraries in implicit directories:

   runtime library [] in /usr/lib may be hidden by files in:

 Some of these libraries may not be found correctly.
Call Stack (most recent call first):
 /home/gsharp/build/slicer-4/Slicer4/CMake/SlicerMacroPythonWrapModuleLibrary.cmake:66 (vtkMacroKitPythonWrap)
 /home/gsharp/build/slicer-4/Slicer4/CMake/SlicerMacroBuildModuleLogic.cmake:76 (SlicerMacroPythonWrapModuleLibrary)
 src/slicer/QTModules/MY_MODULE_NAME/Logic/CMakeLists.txt:28 (SlicerMacroBuildModuleLogic)

These warnings are benign. Anyone know how to turn them off?

  • The above setup will put the shared libraries directly into the slicer build directory. I guess this isn't what you want, but now is a good time to test that everything is working. Make sure the files are there.

On unix:

 $ cd ${SLICER_BUILD}/Slicer-build
 $ find . -name "*MY*" -print

When you fire up slicer, you should see the module. For me, it looks like this:

ExternalModuleWriting screenshot.png

  • It is possible (with some work), to tell your program to build the module in your own directory instead of Slicer's. However, there is not yet any way to tell slicer to look in your own directory. So we leave the tutorial and await further developments.