Documentation/4.8/Developers/Tutorials/ExternalModuleWriting

From Slicer Wiki
Jump to: navigation, search
Home < Documentation < 4.8 < 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

Initialization

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 Terminal.app), then type the following:

$ cd /path/to/Slicer4
$ python Utilities/Scripts/ModuleWizard.py 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\ModuleWizard.py 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)
 if (SLICER_FOUND)
   include ("${Slicer_USE_FILE}")
   if (SLICER_IS_SLICER4)
     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 "")
   set (OLD_CFLAGS ${CMAKE_C_FLAGS})
   set (OLD_CXXFLAGS ${CMAKE_CXX_FLAGS})

   include ("${Slicer_USE_FILE}")

   get_directory_property (SLICER_INCLUDE_DIRS INCLUDE_DIRECTORIES)
   set_directory_properties (PROPERTIES INCLUDE_DIRECTORIES
     "${OLD_INCLUDE_DIR}")
   set (CMAKE_C_FLAGS "${OLD_CFLAGS}" CACHE STRING "CMake CXX Flags" FORCE)
   set (CMAKE_CXX_FLAGS "${OLD_CXXFLAGS}" CACHE STRING "CMake CXX Flags" FORCE)
 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 [libpython2.6.so.1.0] in /usr/lib may be hidden by files in:
     /home/gsharp/build/slicer-4/Slicer4-Build/python-build/lib

 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
 ./bin/MY_MODULE_NAMECxxTests
 ./bin/libvtkSlicerMY_MODULE_NAMEModuleLogicPythonD.so
 ./bin/vtkSlicerMY_MODULE_NAMEModuleLogicPython.so
 ./bin/Python/slicer/modulelogic/vtkSlicerMY_MODULE_NAMEModuleLogic.py
 ./bin/Python/slicer/modulelogic/vtkSlicerMY_MODULE_NAMEModuleLogic.pyc
 ./lib/Slicer3/QTLoadableModules/libvtkSlicerMY_MODULE_NAMEModuleLogic.so
 ./lib/Slicer3/QTLoadableModules/libqSlicerMY_MODULE_NAMEModule.so

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.