Search code examples
c++cmakezbar

C++ ZBar cmake error 'File not found' when including


I have a problem when I include ZBar in my C++ script. I already tried adding it via a CMakelists.txt:

cmake_minimum_required(VERSION 2.8.12)
project( Barcode-cpp )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} ${ZBARCV_SOURCE_DIR} )
set(CMAKE_MODULE_PATH ${ZBARCV_SOURCE_DIRS})
add_compile_options(-std=c++11)
add_library( src
        src/VideoVeed.h
        src/VideoVeed.cpp
        src/Crop.h
        src/Crop.cpp
        src/Barcodes.h
        src/Barcodes.cpp
)
add_executable( program
            program/main.cpp
)
target_link_libraries( program src ${OpenCV_LIBS} ${ZBAR_LIBRARIES} zbar )

I'm on mac. I looked and my zbar.h file is located in /usr/local/include/ where it's supposed to be.

I include it like this: #include <zbar.h>

I hope someone is able to help me. Thanks in advance!

EDIT:

Full make error log:

/Users/mathijs/Documents/Barcode-cpp/src/Barcodes.h:7:10: fatal error: 'zbar.h' file not found
#include <zbar.h>
         ^~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/src.dir/src/VideoVeed.cpp.o] Error 1
make[1]: *** [CMakeFiles/src.dir/all] Error 2
make: *** [all] Error 2

Solution

  • I just checked; the Brew package for ZBar includes a packageconfig file (zbar.pc)

    That means you can use modern CMake tooling instead of cargo culting:

    cmake_minimum_required(VERSION 3.8)
    project( Barcode-cpp )
    find_package( OpenCV REQUIRED )
    include_directories(${OpenCV_INCLUDE_DIRS})
    set(CMAKE_CXX_STANDARD 11)
    
    add_library( src
            src/VideoVeed.h
            src/VideoVeed.cpp
            src/Crop.h
            src/Crop.cpp
            src/Barcodes.h
            src/Barcodes.cpp
    )
    add_executable( program
                program/main.cpp
    )
    target_link_libraries(program src ${OpenCV_LIBS})
    
    find_package(PkgConfig REQUIRED)
    pkg_check_modules(ZBar REQUIRED IMPORTED_TARGET zbar)
    
    target_link_libraries(program PkgConfig::ZBar)
    

    The pkg_check_modules will read the zbar.pc file and generate an IMPORTED target named PkgConfig::ZBar that will automatically set both include paths and linker paths for program.