Re: [CMake] Building Matlab Mex extensions that link to ITK?

2013-11-20 Thread Williams, Norman K
The problem is even stranger than that.

In my CMakeLists.txt I am using (sort of) the 'stub + library' method
suggested by Matt McCormick. But I'm not doing exactly what he did; I try
and get all the libraries needed to link and pass them in to MEX.  This
ALMOST works, but ITK_LIBRARIES doesn't list all the libraries needed,
hence I still get undefined externals -- stuff from vnl and gdcm.
Apparently there are library dependencies that are handled down in the
murk of CMake's build logic.

CMAKELISTS.TXT---
find_package(Matlab REQUIRED)

set(MEX_SOURCES nrrdLoadWithMetadata nrrdSaveWithMetadata
itkLoadWithMetadata itkSaveWithMetadata)
set(MEX_SHARED_OBJECTS "")
# get library locations for ITK libraries
set(_ITK_LIBS)
foreach(itklib ${ITK_LIBRARIES})
  if(TARGET ${itklib})
get_property(itk_lib_path TARGET ${itklib} PROPERTY LOCATION)
list(APPEND _ITK_LIBS "${itk_lib_path}")
  else()
list(APPEND _ITK_LIBS "${itklib}")
  endif()
endforeach()

foreach(MEX_PREFIX ${MEX_SOURCES})
  # stick actual code in static lib
  add_library(${MEX_PREFIX} STATIC ${MEX_PREFIX}.cxx)
  # link in all necessary libraries
  target_link_libraries(${MEX_PREFIX} ${Teem_LIBRARIES}
${ITK_LIBRARIES}
${ZLIB_LIBRARY})

  get_property(mex_lib_path TARGET ${MEX_PREFIX} PROPERTY LOCATION)

  set(MEX_SRC_PATH ${CMAKE_CURRENT_SOURCE_DIR}/${MEX_PREFIX}Stub.cxx)
  set(MEX_OBJECT_NAME ${MEX_PREFIX}.${MATLAB_MEX_EXT})
  list(APPEND MEX_SHARED_OBJECTS ${MEX_OBJECT_NAME})

  add_custom_command(OUTPUT ${MEX_PREFIX}.${MATLAB_MEX_EXT}
## HACK: The next line needs to be more general
COMMAND ${MATLAB_MEX_PATH} -cxx ${CMAKE_CXX_FLAGS_Release}
${MEX_SRC_PATH}
${mex_lib_path}
${_ITK_LIBS}
-L${Teem_LIBRARY_DIR} -lteem
${ZLIB_LIBRARY}
DEPENDS ${MEX_PREFIX} ${MEX_SRC_PATH}
IMPLICIT_DEPENDS C ${MEX_SRC_PATH}
WORKING_DIRECTORY ${${PRIMARY_PROJECT_NAME}_SOURCE_DIR}/MATLAB_SCRIPTS
COMMENT "BUILDING MEX_FILE: ${MEX_SRC_PATH}" )
endforeach()

add_custom_target(MEX_BLD ALL
  DEPENDS ${MEX_SHARED_OBJECTS}
  #[WORKING_DIRECTORY dir]
  COMMENT "BUILDING TARGET MEX_FILE: ${MEX_SRC}"
  SOURCES ${MATLAB_MEX_PATH} )

-Build
Output-

make-- Configuring done
-- Generating done
-- Build files have been written to:
/Volumes/scratch/kent/CompressedSensingDWI/build/CompressedSensingDWI-build
[  0%] Built target CompressedSensingDWIFetchData
[ 11%] Built target itkSaveWithMetadata
[ 22%] Built target nrrdSaveWithMetadata
[ 33%] Built target nrrdLoadWithMetadata
[ 44%] Built target itkLoadWithMetadata
[ 55%] BUILDING MEX_FILE:
/Volumes/scratch/kent/CompressedSensingDWI/CompressedSensingDWI/mexFiles/it
kSaveWithMetadataStub.cxx
[ 66%] BUILDING MEX_FILE:
/Volumes/scratch/kent/CompressedSensingDWI/CompressedSensingDWI/mexFiles/nr
rdLoadWithMetadataStub.cxx
[ 77%] BUILDING MEX_FILE:
/Volumes/scratch/kent/CompressedSensingDWI/CompressedSensingDWI/mexFiles/nr
rdSaveWithMetadataStub.cxx
[ 88%] BUILDING MEX_FILE:
/Volumes/scratch/kent/CompressedSensingDWI/CompressedSensingDWI/mexFiles/it
kLoadWithMetadataStub.cxx
Undefined symbols for architecture x86_64:
  "vnl_vector::set_size(unsigned int)", referenced from:
  itk::Array::SetSize(unsigned int) in
libITKIOHDF5-4.5.a(itkHDF5ImageIO.cxx.o)
  "vnl_vector::vnl_vector(vnl_vector const&)", referenced from:
  itk::Array::Array(itk::Array const&) in
libITKIOHDF5-4.5.a(itkHDF5ImageIO.cxx.o)
  "vnl_vector::vnl_vector(unsigned int)", referenced from:
  itk::Array::Array(unsigned int) in
libITKIOHDF5-4.5.a(itkHDF5ImageIO.cxx.o)
  "vnl_vector::~vnl_vector()", referenced from:
  itk::Array::~Array() in
libITKIOHDF5-4.5.a(itkHDF5ImageIO.cxx.o)
  "vnl_vector::operator=(vnl_vector const&)", referenced from:
  itk::Array::operator=(itk::Array const&) in
libITKIOHDF5-4.5.a(itkHDF5ImageIO.cxx.o)
  "vnl_vector::set_size(unsigned int)", referenced from:
  itk::Array::SetSize(unsigned int) in
libITKIOHDF5-4.5.a(itkHDF5ImageIO.cxx.o)
  "vnl_vector::vnl_vector(vnl_vector const&)", referenced
from:
  itk::Array::Array(itk::Array const&) in
libITKIOHDF5-4.5.a(itkHDF5ImageIO.cxx.o)
  "vnl_vector::vnl_vector(unsigned int)", referenced from:
  itk::Array::Array(unsigned int) in
libITKIOHDF5-4.5.a(itkHDF5ImageIO.cxx.o)
  "vnl_vector::~vnl_vector()", referenced from:
  itk::Array::~Array() in
libITKIOHDF5-4.5.a(itkHDF5ImageIO.cxx.o)
  "vnl_vector::operator=(vnl_vector const&)", referenced
from:
  itk::Array::operator=(itk::Array const&) in
libITKIOHDF5-4.5.a(itkHDF5ImageIO.cxx.o)
  "gdcm::DataElement::SetVLToUndefined()", referenced from:
  gdcm::ImageHelper::SetSpacingValue(gdcm::DataSet&,
std::vector > const&) in
libitkgdcmMSFF-4.5.a(gdcmImageHelper.cxx.o)
  gdcm::SetDataElementInSQAsItemNumber(gdcm::DataSet&,
gdcm::DataElement const&, gdcm::Tag const&, unsigned int) in
libitkgdcmMSFF-4.5.a(gdcmImageHelper.cxx.o)
  gdcm::ImageHelper::SetDirectionCosin

Re: [CMake] Building Matlab Mex extensions that link to ITK?

2013-11-20 Thread Bradley Lowekamp
Kent,

It appears that you are trying to link of all of ITK. SimpleITK ITK took the 
approach of producing a library that fully encapsulates ITK. So that a  single 
shared library could be produced and not depend on ITK. While you could use 
SimpleITK's library and try to use the infrastructure to customize if for you 
needs. I doubt that is what you need... ( But it'd be very cool IMO ).

What you may consider is compiling ITK as static and creating a facade 
interface which only exposes your code and methods you need. This facade 
interface would be compiled into a self contained shared library. So you only 
need to link the mex stuff to this library and not all of ITK. Hence just 
simplify the process by encapsulating ITK.

If you want any further info just let me know.
Brad

On Nov 19, 2013, at 12:59 PM, "Williams, Norman K" 
 wrote:

> Has anyone else come up with an elegant solution for this?
> 
> If I want to call ITK from a Matlab MEX extension written in C++, I run
> into problems with telling it the libraries with which to link.
> 
> The ITK_LIBRARIES variable is a list of imported library targets which
> CMake converts to actual library paths when it links a library or
> executable.
> 
> But to compile a MEX file, we're using a custom command along the lines of
> 
>  add_custom_command(OUTPUT ${MEX_PREFIX}.${MATLAB_MEX_EXT}
>COMMAND ${MATLAB_MEX_PATH} -cxx
> ${CMAKE_CXX_FLAGS_Release} ${MEX_SRC_PATH}
> ${ITK_INCLUDES} ${ITK_LIBRARIES}
>-I${Teem_INCLUDE_DIR} -L${Teem_LIBRARY_DIR}  -lteem
> ${ZLIB_LIBRARY}
>DEPENDS ${MEX_SRC_PATH}
>IMPLICIT_DEPENDS C ${MEX_SRC_PATH}
>COMMENT "BUILDING MEX_FILE: ${MEX_SRC_PATH}" )
> 
> But that doesn't work because CMake gives the target names in
> ITK_LIBRARIES instead of the actual libraries.
> 
> I tried this:
> 
> find_package(ITK NO_MODULE REQUIRED)
> include(${ITK_USE_FILE})
> 
> set(ITK_LOCAL_LIBS)
> foreach(lib ${ITK_LIBRARIES})
> get_target_property(_lib ${lib} IMPORTED_LOCATION)
> 
> list(APPEND ITK_LOCAL_LIBS ${_lib})endforeach()
> 
> But all the IMPORTED_LOCATION for ITK libraries is _lib-NOTFOUND
> 
> 
> 
> --
> Kent Williams norman-k-willi...@uiowa.edu
> 
> 
> 
> 
> 
> 
> 
> 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
> 
> Please keep messages on-topic and check the CMake FAQ at: 
> http://www.cmake.org/Wiki/CMake_FAQ
> 
> Kitware offers various services to support the CMake community. For more 
> information on each offering, please visit:
> 
> CMake Support: http://cmake.org/cmake/help/support.html
> CMake Consulting: http://cmake.org/cmake/help/consulting.html
> CMake Training Courses: http://cmake.org/cmake/help/training.html
> 
> Visit other Kitware open-source projects at 
> http://www.kitware.com/opensource/opensource.html
> 
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: 
http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more 
information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

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

Follow this link to subscribe/unsubscribe:
http://www.cmake.org/mailman/listinfo/cmake