7.6   Creating an Executable Under Linux

Your own C++ programs that use HALCON operators must include the file HalconCpp.h, which contains all user-relevant definitions of the HALCON system and the declarations necessary for the C++ interface. Do this by adding the following command near the top of your C++ file:

  #include "HalconCpp.h"

To specify the include path for the compiler on the command line, use the following syntax:

  -I$HALCONROOT/include -I$HALCONROOT/include/halconcpp

To create an application, you have to link two libraries to your program: The library libhalconcpp.so contains the various components of the HALCON/C++ interface. The library libhalcon.so is the HALCON library. To specify the library path and the libraries for the linker, use the following syntax:

  -L$HALCONROOT/lib/$HALCONARCH -lhalconcpp -lhalcon -lXext -lX11 -lpthread

On some systems, you also have to link the libraries libdl.so and librt.so (by using the additional options -ldl and -lrt, respectively).

HALCON XL applications: If you want to use HALCON XL, link the libraries libhalconcppxl.so and libhalconxl.so instead.

For suitable settings, take a look at the example makefiles. If you call gmake without further arguments, the example application matching will be created. To create the other example applications (e.g., error_handling), call

  gmake error_handling

You can use the example makefiles not only to compile and link the example programs but also your own programs (if placed in the subdirectory source). For example, to compile and link a source file called myprogram.cpp call

  gmake myprogram

You can link the program to the HALCON XL libraries by adding XL=1 to the make command, for example

  gmake myprogram XL=1

To link and run applications under Linux, ensure that the system variable LD_LIBRARY_PATH contains the HALCON library path $HALCONROOT/lib/$HALCONARCH.

Compiling and Linking an Example Program Under Linux

The following example shows a very basic C++ program called main.cpp, which reads an image and then prints its width and height:

  #include "HalconCpp.h"

  #include <iostream>

  int main(int argc, char **argv)
  {
    HalconCpp::HObject hobj;
    HalconCpp::HTuple width, height;
    HalconCpp::ReadImage(&hobj, "printer_chip/printer_chip_01");
    HalconCpp::GetImageSize(hobj, &width, &height);

    std::cout << "Image - width: " << width.I() << ", height: " << height.I() << '\n';
    return 0;
  }

To compile and link the program under Linux, run:

  g++ -o example main.cpp  -I$HALCONROOT/include -I$HALCONROOT/include/halconcpp  \
    -L$HALCONROOT/lib/$HALCONARCH -lhalconcpp -lhalcon -lXext -lX11 -lpthread

In this simple case with console output only, you can also omit the options -lXext -lX11 -lpthread.

For more information, compare the makefile that is provided with the examples under $HALCONEXAMPLES/cpp/console/makefiles/makefile.