Integrate ITK (Insight Toolkit) into own project - c++

i am having problems integrating ITK - Insight Toolkit into another image processing pipeline. ITK itself is a medical image processing toolkit and uses cmake as build system. My image pipeline project uses cmake as well. According to the user manual of ITK it is favorable to use the "UseITK.cmake" file in the build (out of source) directory of ITK. You can do that by adding the following lines the CMakeList.txt of your own project.
# 'SET(ITK_DIR ...)' if 'FIND_PACKAGE(ITK REQUIRED)' fails
FIND_PACKAGE(ITK REQUIRED)
INCLUDE(${ITK_USE_FILE})
My problem is, this approach points to the current installtion of ITK, but i have to integrate itk completly into my project, without dependencies outside my project.
Is there a build option in the cmake build system of itk, which dumps/delivers all the header and lib files into a build directory, so i can place them into my project on my own.
I have a lib and header include structure i do not want to break. I already tried to to manually copy the lib and header files into my project but it didn't work out.
I am new to itk and cmake so this question might sound vague. I hope you guys can help me anyway.
Thanks in advance!
Best regards,
zhengtonic

I don't know if you're still having the problem, but here's an easy way:
Build the ITK project, and then "make install" (or build the INSTALL.vcproj project in VS), and it will write to a directory you pass as CMAKE_INSTALL_PREFIX while configuring your project. This directory will contain /bin, /lib and /include. You can import those into your project directly.

Using CMAKE_INSTALL_PREFIX to point to you base directory is the best solution.
When you issue "make install" all the headers/configurationfiles/libraries will be copied over.

C:/Program Files (x86)/ITK is value of CMAKE_INSTALL_PREFIX for ITK and set automatically.

Related

How to include a .so lib using cmake

I am retired and been away from programming for several years. I am writing an application that stores all its info in XML files. I am using vscode and cmake under linux mint for building. It is big enough now that I want to run it through doxygen to see all the relationships.
I'm using leethomason/tinyxml2 from github to read/write/access my XML for the project. (A very nice lightweight XML lib, BTW.) Since it only consistes of a single header and source file I have been just compilling them with my code. But the doxygen docs show all the interelationships within tinyxml which is distracting for my purpose. So it is time to break it out into its on lib.
I can build the tinyxml github clone fine. It generates a libtinyxml2d.so, a cmake_install.cmake and cmake_uninstall.cmake (and all the other stuff) in the build directory. What I cannot figure out how to do is to tell my project cmake file how to include the lib.
The tinyxml2 CMakeLists.txt file says:
# Export cmake script that can be used by downstream project
# via `include()`
export(TARGETS tinyxml2
FILE ${CMAKE_BINARY_DIR}/${TARGETS_EXPORT_NAME}.cmake)
install(TARGETS tinyxml2
EXPORT ${TARGETS_EXPORT_NAME}
RUNTIME
DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT tinyxml2_runtime
LIBRARY
DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT tinyxml2_libraries
ARCHIVE
DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT tinyxml2_libraries)
But I cannot figure out how to use that info to link the lib into my cmake file. I search and read the documentation but that is like reading a foreign language because of all the stuff I've forgotten or never knew. (cmake, linux and vscode are all new worlds for me.)
Can someone give an old guy a few pointers? Where to look for examples that are not rudimentary "how to use cmake", or tutorials for lib installation/access.
Thanks,
Eddie
Well, I succeeded but I feel really dumb for it.
I did some more poking around and found how to use the install file.
I did:
cmake -P cmake_install.cmake
which copied it to the system directory like I wanted then simply added the lib name to the target command in the cmake file.
Turns out that wasn't the problem I was trying to cure.
I forgot to remove the tinyxml cpp ad h files from the directory that doxygen was using.
Well, Duh...
Eddie

Correct way to build OpenCV as a third party library along with your project in CMAKE

I have a C++ project, where I use opencv, VTK. I would like to build and install these dependent libraries (opencv,vtk) automatically, which are situated in my thirdparty project folder. I know that opencv and VTK use cmake build system to build their libraries. I even saw the function ExternalProject to add them to my CMakesLists.txt but I had problem of linking the target with opencv libs.
Searching on stack overflow I could not find a proper defined method to do it to implement this for version 3.0, the proposed solutions where Version < 3.0. I would like to know how you will structure your project to build opencv automatically as a thirdparty library, exports its targets and link it with my project target.
I think that the best way is to look for what's being done on larger project on github. I would advise to look at this repo in which they use opencv version 3.2, automatically download it and compile it. I believe that this is the correct way because you just have to change the version to try the compatibility with the newer versions if you want to update your code.
What you are looking for is probably what is called a superbuild. This post might help.
I would suggest to build and install opencv in a subfolder of your repo or your build directory. Then you could set the OpenCV_DIR environment variable to the <opencv_install_prefix>/lib/cmake/opencv4 folder (or at least point to the folder where OpenCVConfig.cmake is)
Then in your CMakeLists.txt you can directly use :
find_package(OpenCV 4 REQUIRED
COMPONENTS core imgproc ximgproc)
target_link_libraries(${PROJECT_NAME} PUBLIC ${OpenCV_LIBS})
The OpenCV_DIR variable tells CMake where to find the OpenCV configuration files needed for find_package function.
For the build and installation step, you could use ExternalProject or FetchContent (I prefer the later) but since OpenCV could be very long to build, you might want to keep the build artifact outside of your project's build folder. Then you can erase your build folder to rebuild your project without rebuilding the whole OpenCV library.
On my side, I'm using a shell script that build OpenCV if needed before building the project that needs it.

Build OpenCV using qmake

I make part of a 'big' project based on QT-Creator.
It is an image adquisition/processing project.
I am working in a particular library where we need to use OpenCV functions.
I found out how to build, install and include OpenCV using CMAKE thanks to answers like this one.
My problem is that we need that each person on the team can get the OpenCV source and build/install/include automatically (something we normally do with our own libraries) AND this is 100% based on qmake then I should not add CMAKE to our project.
The question is does someone know how to build OpenCV directly with qmake in a project on QT-Creator?

Configuring a CMake project with dependencies on multiple other CMAKE projects

I have a CMAKE project that depends on other projects built with CMAKE. These are : glfw, oglplus, portaudio etc.
How should I set up my project to work well in a cross platform fashion? What is the recommended way to go about it? I have been trying to read the CMAKE documentation but could only find examples to simple scenarios.
Just add the dependencies to your project README and expect the user stored them (already compiled) in system scope.
Add CMake options to request the path to dependency files.
Use add_subdirectory to chain your project with dependencies.

Using GDCM via CMake

I downloaded source of GDCM 2.4.1 and used CMake 2.8.12.1 and Visual Studio 2010 to build the libraries. I want to use GDCM in my C++ project.
Unfortunately, it seems that after building the GDCM solution with VS, there are only the lib-files in the bin folder, but no typical "include" folder with the header files, what I usually expect. Thus, I can't integrate GDCM libaries in my own project (I tried to use FindPackage(GDCM) in my own CMakeproject, but header files can't be found).
I do not want to copy the header files manually or target the source directory of GDCM.
Does anybody know help?
I found out that the problem has something to do with VTK, I also use the this library in my project. So I need to enable "GDCM_USE_VTK" in CMake of GDCM. That option leads to a compiler error when I try to build the "INSTALL" subproject in the GDCM solution:
CMake Error at cmake_install.cmake:31 (FILE): file INSTALL cannot find "D:/Libs/VTK_5.6.0/BIN/bin/vtkCommon.dll".
I took a look in the VTK directory and found out that the path mentioned above does not exists. Instead the dll is located in:
D:\Libs\VTK_5.6.0\BIN\bin\Release\vtkCommon.dll or
D:\Libs\VTK_5.6.0\BIN\bin\Debug\vtkCommon.dll
That means GDCM solution does not know the dll is located in a special debug or release folder. If I disable "GDCM_USE_VTK" everything works fine and all files will be copied to the target folder. But I do need the VTK dll.
Any thoughts?
Michael
For anyone who ever touches the same problem, here's the final solution. Build "ALL_BUILD" in the GDCM solution, then integrate GDCM libs in your CMakeLists.txt the following way:
FIND_PACKAGE(GDCM REQUIRED)
IF(GDCM_FOUND)
INCLUDE(${GDCM_USE_FILE})
SET(GDCM_LIBRARIES
gdcmcharls
gdcmCommon
gdcmDICT
gdcmDSED
gdcmexpat
gdcmgetopt
gdcmIOD
gdcmjpeg12
gdcmjpeg16
gdcmjpeg8
gdcmMEXD
gdcmMSFF
gdcmopenjpeg
gdcmzlib
socketxx
vtkgdcm
Rpcrt4)
ELSE(GDCM_FOUND)
MESSAGE(FATAL_ERROR "Cannot find GDCM, did you set GDCM_DIR?")
ENDIF(GDCM_FOUND)