[Insight-developers] Forgetting to include ITK_USE_FILE in project CMakeLists.txt causes all file IO to fail?

Williams, Norman K norman-k-williams at uiowa.edu
Thu Nov 29 15:28:05 EST 2012


The particular ITK version I was using is
84eb17892f93e8ab035857c568ea5f1f88c7c62f

And it was failing if the CMakeLists.txt didn't include ITK_USE_FILE and
succeeding if you did.

------------------CMakeLists.txt-----------------------------
cmake_minimum_required(VERSION 2.8)
PROJECT(test)
find_package(ITK REQUIRED)
include(${ITK_USE_FILE})
add_executable(test1 test1.cpp)target_link_libraries(test1
${ITK_LIBRARIES} )
-----------------test1.cpp----------------------------------
#include <itkImageFileWriter.h>
#include <itkImageFileReader.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>

#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>

#include <cstdlib>

#include <limits>

typedef int16_t DataType;
typedef itk::Image<DataType, 4> FImageT;
typedef itk::Image<DataType, 3> LImageT;

/* Helper function to write an image "out" to prefix + filename */
template <typename T>
void writeImage(std::string name, typename T::Pointer in)
{
    std::cerr << "Writing " << name << std::endl;
    typename itk::ImageFileWriter<T>::Pointer writer;
    writer = itk::ImageFileWriter<T>::New();
    writer->SetFileName(name);
    writer->SetInput( in );
    std::cerr << "Done " << std::endl;
    writer->Update();
}

using namespace std;

int main(int argc, char* argv[])
{
  LImageT::SizeType size3 = {{6, 1, 1}};
  FImageT::SizeType size4 = {{size3[0], size3[1], size3[2], 180}};
  std::vector<std::string> fnames;

  for(int i = 1 ; i < argc ; i++)
    {
    fnames.push_back(argv[i]);
    }

  if(fnames.size() < 1)
    {
    cout << "Not enough input files" << endl;
    return -3;
    }

  FImageT::Pointer fimg = FImageT::New();
  fimg->SetRegions(size4);
  fimg->Allocate();
  fimg->FillBuffer(0);

  try
    {
    writeImage<FImageT>(fnames[0], fimg);
    }
  catch(itk::ExceptionObject &excp)
    {
    std::cerr << "exception in file writer " << std::endl;
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }
}





--
Kent Williams norman-k-williams at uiowa.edu






On 11/29/12 2:15 PM, "Bradley Lowekamp" <blowekamp at mail.nih.gov> wrote:

>Kent,
>
>I don't think a segfault is an intended behavior.
>
>When is the tag you are using ITK from to test this?
>
>I encountered this when I was working on the ObjectFactory 2 weeks ago,
>and I I fixed it for my case.
>
>I actually did a find_package only for the IO module, and included the
>${ITK_USE_FILE},  but no IO modules got registered. And a segfault
>happened.
>
>May be only similar, yet different.
>
>Brad
>
>On Nov 29, 2012, at 3:07 PM, "Williams, Norman K"
><norman-k-williams at uiowa.edu> wrote:
>
>> I ran into this working through this bug:
>> https://itk.icts.uiowa.edu/jira/browse/ITK-2859
>>
>> Turns out Micah Chambers was right; it segfaults.  I finally figured out
>> what the problem was: he does this:
>>
>> find_package(ITK REQUIRED)
>> if(NOT ITK_FOUND)
>>  message(FATAL_ERROR "Cannot build without ITK. Please set ITK_DIR.")
>> endif(NOT ITK_FOUND)
>>
>> without doing this:
>>
>>
>> include(${ITK_USE_FILE})
>>
>> This is kind of baffling to me, but there's a lot of crazy computer
>> science-y thingies going on under the covers with how the CMake
>> configuration works.
>>
>> I will say this: It's kind of scary that the find_package succeeds, and
>> the compilation and linking works without complaint, but without the
>> UseITK.cmake the program craps itself if it tries to read or write a
>>file.
>>
>> Is this an intended behavior?  Is there a way that CMake could be forced
>> to either do the work of UseITK.cmake, or complain if it isn't included?
>>
>>
>>
>> ________________________________
>> Notice: This UI Health Care e-mail (including attachments) is covered
>>by the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is
>>confidential and may be legally privileged.  If you are not the intended
>>recipient, you are hereby notified that any retention, dissemination,
>>distribution, or copying of this communication is strictly prohibited.
>>Please reply to the sender that you have received the message in error,
>>then delete it.  Thank you.
>> ________________________________
>> _______________________________________________
>> Powered by 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://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://www.itk.org/mailman/listinfo/insight-developers
>



________________________________
Notice: This UI Health Care e-mail (including attachments) is covered by the Electronic Communications Privacy Act, 18 U.S.C. 2510-2521, is confidential and may be legally privileged.  If you are not the intended recipient, you are hereby notified that any retention, dissemination, distribution, or copying of this communication is strictly prohibited.  Please reply to the sender that you have received the message in error, then delete it.  Thank you.
________________________________


More information about the Insight-developers mailing list