[ITK-users] stand-alone lib -- ITKApps/ITKFilterLib -- Solved (maybe)

Robert.Atwood at diamond.ac.uk Robert.Atwood at diamond.ac.uk
Wed Mar 23 11:58:45 EDT 2016


Hi, Dženan, and List :

Thanks for the tips, but, building the executable by pointing to the itk-build folder for linking is exactly what I did not want to do. I want the compiled instances  of the specific templated objects to be put into a stand-alone library that could then be linked , in an environment where the ITK build directory may not be available, and within a project that’s not using cmake (yet? ☺  ) just for a small single-data-type-specific filter chain that would be a bit nontrivial to just re-implement myself!

I had some success with the following, for the example ITKFilterLib:

Using the itk-master-git-static as built before, (BUILD_SHARED_LIBS OFF)


In CMakeLists.txt for the project, add SHARED keyword to the library defninition:

15 ADD_LIBRARY(Example_ITKFilterLib SHARED ITKFilterLib.cxx )
Then use cmake + make to build the libExample_ITKFilterLib.so library.


Then I can invoke g++ as follows



build]$ g++ ../ITKFilterLib/ITKFilterLibTest.cxx -o testcomp -L . -l Example_ITKFilterLib -I ../ITKFilterLib/

Even if the itk-master-static is not available in that environment, this succeeds, and I believe this is what I want. I don’t want to have a statically linked final executable, but rather a .so library.


However …

I encountered another hitch trying to apply this to my actual project. If there were any FileWriter and FileReader present in the library, it would not build, complaining about position-dependent symbols in several IO libraries , most of which I was not actually wanting to use at all. This happened at the step of  trying to build the .so library using the makefile created by cmake:

/usr/bin/ld: /dls_sw/apps/itk/itk-master-static/lib/libitkgdcmjpeg8-4.10.a(jerror.c.o): relocation R_X86_64_32S against `.text' can not be used when making a shared object; recompile with -fPIC
/dls_sw/apps/itk/itk-master-static/lib/libitkgdcmjpeg8-4.10.a: could not read symbols: Bad value


Removing any reference to FileReader and FileWriter gets rid of this problem.

Thanks!


As a further note:

There are also some  directions in the comments of the ITKFilterLib example  that did not turn out to be necessary in my project. I did not have to create global objects for poitners or image containiers outside the scope of the class and still did not have to #include itkImage.h in the main program. I placed the typedef’s inside the class and the instantiations of the smart poitners in the class constructor. I don’t see that this extra complication in the example is necessary unless it is platform dependent?

ITKFilterLib.cxx from ItkApps example:


23 #include "ITKFilterLib.h"
24
25 // Nasty local globals inside .cxx file to hide itk from the real world.
26 //   Can't put this variable in the class definition otherwise we'd have to
27 //   include itkImage.h in every program using this class.
28 // The typedef h_itkFilterLibeDataType is in itkFilterLib.h to hide
29 //   templates
30 typedef itk::Image<g_ItkFilterLibDataType,3>   g_ImageType;
31 g_ImageType::Pointer g_InputImage;
32 g_ImageType::Pointer g_OutputImage;

I did not find this to be necessary in my project.


33
34
35 //
36 //
37 // The first filter in the .lib
38 //
39 //
40
41 /** The next lines are used to include the definition of the filter
42  *   we want to install in the .lib, define a typedef for it, and create
43  *   a "smart" pointer to hold it.
44  * CHANGE HERE: The filter to be instantiated.
45  **/
46 #include "itkCurvatureAnisotropicDiffusionImageFilter.h"
47 typedef itk::CurvatureAnisotropicDiffusionImageFilter<g_ImageType,
48     g_ImageType> g_AnisoFilterType;
49 g_AnisoFilterType::Pointer g_anisoFilter;
50

I also did not find it to be necessary to have this sort of typedef and instantiation of the object  at global scope in my project.






From: Dženan Zukić [mailto:dzenanz at gmail.com]
Sent: 23 March 2016 14:39
To: Atwood, Robert (DLSLtd,RAL,SCI)
Cc: Insight-users
Subject: Re: [ITK-users] stand-alone lib -- ITKApps/ITKFilterLib

Hi Robert,

BUILD_SHARED_LIBS OFF is the crucial setting. You can keep the rest of options on defaults.

Your CMake code looks OK. You should execute the following from your folder:
cmake .
make

During CMake step you should configure options (point to the itk-build folder which was configured with BUILD_SHARED_LIBS OFF). Afterwards, do not invoke g++ directly - invoke make, which supplies correct include directories and link libraries so you don't run into a thousand errors.

Regards,
Dženan

On Tue, Mar 22, 2016 at 6:25 AM, <Robert.Atwood at diamond.ac.uk<mailto:Robert.Atwood at diamond.ac.uk>> wrote:
Hi,
Thanks, Dženan, for the hint. But, does anyone have specific information on exactly which options to set in each of: the ITK cmake options; and the project cmake options, or the plain g++ for building an external program to link to the library, for this to work ? There appear to be a few that might be related to this. Because, so far, simply changing the build options of the ITK installation didn’t work. See below for even more details .
Thanks
Robert



Options selected in  the project:
ITK_DIR                          /dls_sw/apps/itk/itk-master-static/lib/cmake/ITK-4.10



In the ITK with installation to itk-master-static:
I have tried some combinations of these options but, you know, it takes a while to rebuild ITK even though the computer is pretty good.

BUILD_POSITION_DEPENDENT_CODE    OFF  (I don’t think this should be ON because that really wouldn’t work? )
BUILD_SHARED_LIBS                OFF (or ON)
CMAKE_CXX_FLAGS                  -fPIC (or default)
CMAKE_EXE_LINKER_FLAGS           -fPIC (or default)
CMAKE_STATIC_LINKER_FLAGS  (should this be set too? )



The CMakeLists in the project file – only small modification from the original to ‘BUILD_OUTSIDE_INSIGHT_APPICATIONS’. The project builds using the Cmake makefile,  producing the library and the executable
But trying to build either the ‘main’ procedure provided with linking to that library, or even simplifying the ‘main’ to do virtually nothing except instantiate the object, results in 1000 error messages of ‘undefined symbols’

  1 PROJECT(Example_ITKFilterLib)
  2
  3 #REMOVED 'IF (BUILD_OUTSIDE_INSIGHT_APPLICATIONS)'
  4
  5   FIND_PACKAGE(ITK)
  6   IF(ITK_FOUND)
  7     INCLUDE(${ITK_USE_FILE})
  8   ELSE(ITK_FOUND)
  9     MESSAGE(FATAL_ERROR
10             "Cannot build ITKApps without ITK.  Please set ITK_DIR.")
11   ENDIF(ITK_FOUND)
12
13 #REMOVED 'ENDIF(BUILD_OUTSIDE_INSIGHT_APPLICATIONS)'
14
15 ADD_LIBRARY(Example_ITKFilterLib ITKFilterLib.cxx )
16 INSTALL_TARGETS(/lib Example_ITKFilterLib)
17 TARGET_LINK_LIBRARIES(Example_ITKFilterLib ${ITK_LIBRARIES})
18
19 ADD_EXECUTABLE(Example_ITKFilterLibTest ITKFilterLibTest.cxx )
20 INSTALL_TARGETS(/bin Example_ITKFilterLibTest)
21 TARGET_LINK_LIBRARIES(Example_ITKFilterLibTest Example_ITKFilterLib)


build]$ ls -l
total 2508
-rw-rw-r-- 1 kny48981 kny48981   14393 Mar 22 10:02 CMakeCache.txt
drwxrwxr-x 6 kny48981 kny48981    4096 Mar 22 10:05 CMakeFiles
-rwxrwxr-x 1 kny48981 kny48981 1417263 Mar 22 10:05 Example_ITKFilterLibTest
drwxrwxr-x 2 kny48981 kny48981    4096 Mar 22 10:02 ITKIOFactoryRegistration
-rw-rw-r-- 1 kny48981 kny48981    7933 Mar 22 10:02 Makefile
-rw-rw-r-- 1 kny48981 kny48981 1092874 Mar 22 10:05 libExample_ITKFilterLib.a



build]$ g++ -o testcomp ../ITKFilterLib/ITKFilterLibTest.cxx -I ../ITKFilterLib -L . -l Example_ITKFilterLib >& out &

… generates 1100 messages …


   1 ./libExample_ITKFilterLib.a(ITKFilterLib.o): In function `itk::MemoryAllocationError::MemoryAllocationError(std::basic_string<char, std::char_traits<char>, std::allocat
   2 ITKFilterLib.cxx:(.text._ZN3itk21MemoryAllocationErrorC2ERKSsjS2_S2_[_ZN3itk21MemoryAllocationErrorC5ERKSsjS2_S2_]+0x39): undefined reference to `itk::ExceptionObject:
…
1106 ITKFilterLib.cxx:(.text._ZNK3itk12NeighborhoodIPfLj3ENS_21NeighborhoodAllocatorIS1_EEE9PrintSelfERSoNS_6IndentE[itk::Neighborhood<float*, 3u, itk::NeighborhoodAllocato
1107 collect2: ld returned 1 exit status


Minimal example main:

  1 #include "ITKFilterLib.h"
  2 int main(int, char **) {
  3   int inputDimSize[3];
  4   float inputOrigin[3];
  5   float inputSpacing[3];
  6   inputDimSize[0] = 5;
  7   inputDimSize[1] = 5;
  8   inputDimSize[2] = 5;
  9   inputOrigin[0] = 0;
10   inputOrigin[1] = 0;
11   inputOrigin[2] = 0;
12   inputSpacing[0] = 1;
13   inputSpacing[1] = 1;
14   inputSpacing[2] = 1;
15
16   itkFilterLib filter(inputDimSize, inputOrigin, inputSpacing);
17   return (true);
18 }

g++ -o testmin minimal_example.cxx -I ../ITKFilterLib -L . -l Example_ITKFilterLib >& out


Same output AFAICT:


   1 ./libExample_ITKFilterLib.a(ITKFilterLib.o): In function `itk::MemoryAllocationError::MemoryAllocationError(std::basic_string<char, std::char_traits<char>, std::alloca
   2 ITKFilterLib.cxx:(.text._ZN3itk21MemoryAllocationErrorC2ERKSsjS2_S2_[_ZN3itk21MemoryAllocationErrorC5ERKSsjS2_S2_]+0x39): undefined reference to `itk::ExceptionObject:
….

From: Dženan Zukić [mailto:dzenanz at gmail.com<mailto:dzenanz at gmail.com>]
Sent: 21 March 2016 20:21
To: Atwood, Robert (DLSLtd,RAL,SCI)
Cc: Insight-users
Subject: Re: [ITK-users] stand-alone lib -- ITKApps/ITKFilterLib

Hi Robert,

maybe you need to link to ITK statically? To accomplish this you probably need to build ITK as static libraries (it is a configuration option in CMake).

Regards,
Dženan

On Mon, Mar 21, 2016 at 12:50 PM, <Robert.Atwood at diamond.ac.uk<mailto:Robert.Atwood at diamond.ac.uk>> wrote:
Hi,
This example, ITKApps/ITKFilterLib, appears to be an example of what I want to do , bundle or encapsulate  a specific ITK-based filter into a library so that another program may just call it without having to have the ITK source installation available.
However, a basic test for me is not working.

I just tried to compile the test example from command line after building the library with Cmake as usual. I get hundreds of 'undefined reference', as if I have to link with all the ITK libs anyways.
Am I missing something?
Thanks




g++ -o testcomp ../ITKFilterLib/ITKFilterLibTest.cxx -I ../ITKFilterLib/ -L . -l Example_ITKFilterLib


First few lines of output:

./libExample_ITKFilterLib.a(ITKFilterLib.o): In function `itk::MemoryAllocationError::MemoryAllocationError(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
ITKFilterLib.cxx:(.text._ZN3itk21MemoryAllocationErrorC2ERKSsjS2_S2_[_ZN3itk21MemoryAllocationErrorC5ERKSsjS2_S2_]+0x39): undefined reference to `itk::ExceptionObject::ExceptionObject(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_trait
ITKFilterLib.cxx:(.text._ZN3itk21MemoryAllocationErrorC2ERKSsjS2_S2_[_ZN3itk21MemoryAllocationErrorC5ERKSsjS2_S2_]+0x44): undefined reference to `vtable for itk::MemoryAllocationError'
./libExample_ITKFilterLib.a(ITKFilterLib.o): In function `itk::ProcessAborted::ProcessAborted(char const*, unsigned int)':
ITKFilterLib.cxx:(.text._ZN3itk14ProcessAbortedC2EPKcj[_ZN3itk14ProcessAbortedC5EPKcj]+0x33): undefined reference to `itk::ExceptionObject::ExceptionObject(char const*, unsigned int, char const*, char const*)'
ITKFilterLib.cxx:(.text._ZN3itk14ProcessAbortedC2EPKcj[_ZN3itk14ProcessAbortedC5EPKcj]+0x3e): undefined reference to `vtable for itk::ProcessAborted'
ITKFilterLib.cxx:(.text._ZN3itk14ProcessAbortedC2EPKcj[_ZN3itk14ProcessAbortedC5EPKcj]+0x4f): undefined reference to `itk::ExceptionObject::SetDescription(char const*)'
ITKFilterLib.cxx:(.text._ZN3itk14ProcessAbortedC2EPKcj[_ZN3itk14ProcessAbortedC5EPKcj]+0x62): undefined reference to `itk::ExceptionObject::~ExceptionObject()'



This is on a Linux Red Hat with ITK git master today (March 21 2016)

cmake --version
cmake version 2.8.12.2

/proc/version:

Linux version 2.6.32-573.18.1.el6.x86_64 (mockbuild at x86-010.build.bos.redhat.com<mailto:mockbuild at x86-010.build.bos.redhat.com>) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) ) #1 SMP Wed Jan 6 11:20:49 EST 2016



--
This e-mail and any attachments may contain confidential, copyright and or privileged material, and are for the use of the intended addressee only. If you are not the intended addressee or an authorised recipient of the addressee please notify us of receipt by returning the e-mail and do not use, copy, retain, distribute or disclose the information in or attached to the e-mail.
Any opinions expressed within this e-mail are those of the individual and not necessarily of Diamond Light Source Ltd.
Diamond Light Source Ltd. cannot guarantee that this e-mail or any attachments are free from viruses and we cannot accept liability for any damage which you may sustain as a result of software viruses which may be transmitted in or with the message.
Diamond Light Source Limited (company no. 4375679). Registered in England and Wales with its registered office at Diamond House, Harwell Science and Innovation Campus, Didcot, Oxfordshire, OX11 0DE, United Kingdom

_____________________________________
Powered by www.kitware.com<http://www.kitware.com>

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/insight-users




--

This e-mail and any attachments may contain confidential, copyright and or privileged material, and are for the use of the intended addressee only. If you are not the intended addressee or an authorised recipient of the addressee please notify us of receipt by returning the e-mail and do not use, copy, retain, distribute or disclose the information in or attached to the e-mail.
Any opinions expressed within this e-mail are those of the individual and not necessarily of Diamond Light Source Ltd.
Diamond Light Source Ltd. cannot guarantee that this e-mail or any attachments are free from viruses and we cannot accept liability for any damage which you may sustain as a result of software viruses which may be transmitted in or with the message.
Diamond Light Source Limited (company no. 4375679). Registered in England and Wales with its registered office at Diamond House, Harwell Science and Innovation Campus, Didcot, Oxfordshire, OX11 0DE, United Kingdom


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/insight-users/attachments/20160323/1c7bac31/attachment-0001.html>


More information about the Insight-users mailing list