lamyj / odil

Odil is a C++11 library for the DICOM standard
Other
85 stars 21 forks source link

find_package #70

Closed ferdymercury closed 4 years ago

ferdymercury commented 4 years ago

Is there a way to include Odil in an external project, using CMake find_pacakge function? Something like: find_package(ODIL REQUIRED)

ferdymercury commented 4 years ago

For the moment, I use following workaround:

if(NOT DEFINED ODIL_DIR)
set (ODIL_DIR "/opt/odil" CACHE STRING "ODIL git repository")
endif()
include_directories(${ODIL_DIR}/install/include) # https://github.com/lamyj/odil/issues/70
link_directories(${ODIL_DIR}/install/lib)

(assuming I clone the repo, and then create the build and install folders inside it, with cmake ../ CMAKE_INSTALL_PREFIX as ../install).

Or alternatively, I replace the two last lines with:

add_library(odil SHARED IMPORTED) # https://stackoverflow.com/a/28606916/7471760
set_target_properties(odil PROPERTIES
  IMPORTED_LOCATION "${ODIL_DIR}/install/lib/libodil.so"
  INTERFACE_INCLUDE_DIRECTORIES "${ODIL_DIR}/install/include"
lamyj commented 4 years ago

Not in the current version, but it should work with FindPackageHandleStandardArgs. Can you tell me if it works by storing the following in FindOdil.cmake and adapting the contents of CMAKE_MODULE_PATH?

# - Try to find Odil
# Once done this will define
#  Odil_FOUND - System has Odil
#  Odil_INCLUDE_DIRS - The Odil include directories
#  Odil_LIBRARIES - The libraries needed to use Odil
#  Odil_DEFINITIONS - Compiler switches required for using Odil

find_path(Odil_INCLUDE_DIR "odil/odil.h")
find_library(Odil_LIBRARY NAMES odil)

set(Odil_LIBRARIES ${Odil_LIBRARY} )
set(Odil_INCLUDE_DIRS ${Odil_INCLUDE_DIR} )

include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set Odil_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(
    Odil DEFAULT_MSG Odil_LIBRARY Odil_INCLUDE_DIR)

mark_as_advanced(Odil_INCLUDE_DIR Odil_LIBRARY)
ferdymercury commented 4 years ago

Thanks!!

If I put the content above in a file FindOdil.cmake inside the install directory, and then in my cmake:

set (CMAKE_MODULE_PATH /opt/odil/install)
find_package( Odil REQUIRED )

I get the following error:

CMake Error at /usr/share/cmake-3.10/Modules/FindPackageHandleStandardArgs.cmake:137 (message):
  Could NOT find Odil (missing: Odil_LIBRARY Odil_INCLUDE_DIR)
Call Stack (most recent call first):
  /usr/share/cmake-3.10/Modules/FindPackageHandleStandardArgs.cmake:378 (_FPHSA_FAILURE_MESSAGE)
  FindOdil.cmake:17 (find_package_handle_standard_args)
  CMakeLists.txt:48 (find_package)

Maybe because it is a non-standard installation folder? Normally, CMake supports this type of local installs by calling

cmake -DOdil_DIR='/opt/odil/install'

But it did not work in this case.

lamyj commented 4 years ago

Current version (ca65736) adds CMake integration. You should be able to install to a non-standard location, and then use something like find_package(Odil 0.12.0 REQUIRED) without any FindOdil.cmake. You will however need to specify either CMAKE_PREFIX_PATH or Odil_DIR. I've tested with an installation in ~/Downloads/odil and a dummy project with the following CMakeLists.txt: can you tell me if it works for you?

cmake_minimum_required(VERSION 3.5)
project(test-odil-external)
set (CMAKE_CXX_STANDARD 11)
find_package(DCMTK REQUIRED)
find_package(Odil 0.12.0 REQUIRED)
add_executable(foo foo.cpp)
include_directories(${ODIL_INCLUDE_DIRS})
target_link_libraries(foo libodil)
ferdymercury commented 4 years ago

Thanks! I am trying to build the new version after pulling from git, but I get this error:

CMake Error at CMakeLists.txt:108 (export):
  export given target "pyodil" which is not built by this project.

(I have python wrappers off.)

ferdymercury commented 4 years ago

Changing in the CMakeLists.txt to:

if(BUILD_PYTHON_WRAPPERS)
    export(TARGETS libodil pyodil FILE "${PROJECT_BINARY_DIR}/OdilTargets.cmake")
endif()

seems to solve the error message.

ferdymercury commented 4 years ago

find_package works now if I specify Odil_DIR like this: cmake -DOdil_DIR=/opt/odil/install/lib/CMake/Odil

I am not sure why doing the following does not work: cmake -DOdil_DIR=/opt/odil/install/

Maybe it does not search recursively for .cmake files?

lamyj commented 4 years ago

I've tried a similar approach removing pyodilentirely (I don't think anybody would need to link against it), but this introduces boost-related errors. I'll merge your PR and then spend some quality time with the CMake documentation to find out exactly what goes on.