How can i compile a file .cpp that use OTB?

hello everyone ! I’m Fabrizio.
How can I compile a file cpp, written by me that use OTB API, with GCC / Cmake?

thx for help

Hi Fabrizio

There is some documentation about this here:

Hope it helps!

1 Like

yes !!! thank you !

sorry but when i run cmake i have this errors :

By not providing “FindOTB.cmake” in CMAKE_MODULE_PATH this project has
asked CMake to find a package configuration file provided by “OTB”, but
CMake did not find one.

Could not find a package configuration file provided by “OTB” with any of
the following names:


Add the installation prefix of “OTB” to CMAKE_PREFIX_PATH or set “OTB_DIR”
to a directory containing one of the above files. If “OTB” provides a
separate development package or SDK, be sure it has been installed.

How can I solve ?

Hi Fabrizio,
How are you building your project? I recommand that you use ccmake which is the curse version of cmake, it is easier for the beginning.
I think that here your error is that you did not provide cmake with a valid path to OTB. If you are in command line try something like:
cmake -DOTB_DIR=path/to/otb_binaries/lib/cmake/OTB-X.Y/ path/to/your/project/.

-DOTB_DIR will tell cmake where to find the OTBConfig.cmake file and will set your project up to use OTB.

Hi Antoine,
with this solution :

cmake -DOTB_DIR=path/to/otb_binaries/lib/cmake/OTB-X.Y/ path/to/your/project/.

I have no error, but I have one more question, where can I find the executable program of my project ?

I use an CMakeLists.txt identical like this :

thanks for help !!!

When using CMake you can set your project up through lot of variable. You can set it through command line with -DYOURVARIABLE=value or you can use a interface. In your project build directory (where you type cmake ....) you can do ccmake . which will open a interface letting you set all the variable you have access to.
When using CMake you should avoid building inside the source. What I recommend is that make a source directory and a build directory

   | -build
   | -sources

Then you can go to build and type: ccmake path/to/where/is/CMakeLists.txt which will open the curse interface of cmake and set the build directory to build. If you don not want to use ccmake you can still do cmake -DVAR=val ccmake path/to/where/is/CMakeLists.txt and latter one edit the CMakeCache.txt.
The build directory is where you called cmake or ccmake if not explicitely set. Your executable should be under bin in the directory.

1 Like

Okay !!! I solved my problem for the simple helloworld !!!
But I want to use OTB for a photo projection :

And with the previous command I have an error.
When I run “make” after “cmake”, I’ve this one:

fatal error: otbSpatialReference.h: File or directory not exist
#include “otbSpatialReference.h”

how can I solve ?

Thank you !!

Hmmm… What version of OTB are you using?

the last one ! I’ve done this steps:
chmod +x

But I can’t execute that example ! I don’t know why. I’m not so good with linux, but i must use it ! I’m sorry for my ignorance !!!

Ok, the issue here is that you are using the 6.6.1 and the develop version of the cookbook. Try this with the latest binary packages:

1 Like

Okay !!! You are my saviour ! With yours tips works all perfectly !!