Carpetfizz
Carpetfizz

Reputation: 9149

Importing Zbar into C++ project on macOS

I'm trying to use Zbar which is a barcode reading library with bindings for different languages and environments. I'm trying to use Zbar with C++. I have written C++ code but I don't know much about importing libraries and setting up my IDE to do that. I'm using Xcode on macOS Sierra. Any guidance would be much appreciated.

EDIT: Thanks to the below answer, I was able to get ZBar installed successfully. However, when I try building the following code in Xcode:

#include <iostream>
#include <zbar.h>

int main(int argc, const char * argv[]) {        
    zbar::ImageScanner scanner;
    return 0;
}

I get the following error:

enter image description here

Upvotes: 1

Views: 1463

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207485

I would recommend you use homebrew to manage all your packages on a Mac since Apple doesn't supply a package manager for some reason. You can grab it from brew.sh.

Once you have that installed, you can simply install zbar with:

brew install zbar

It would be a good idea to install pkgconfig too :

brew install pkgconfig

Now you can compile at the command line with:

clang yourProgram.c $(pkg-config --cflags --libs zbar) -o yourProg

or with C++, or g++:

clang++ yourProgram.cpp $(pkg-config --cflags --libs zbar) -o yourProg

If you want to use Xcode, you need to set up:

  • the search path for headers (include files)
  • the search path for libraries
  • the libraries themselves.

As they are not that simple to find, click on 1 then 2 in the figure below to get to the correct area of Xcode:

enter image description here

Now set them up like this:

enter image description here

enter image description here

And everything should be all good to go - as our American friends say. This method takes advantage of the fact that homebrew always puts symbolic links in /usr/local/include and /usr/local/lib to the latest, greatest version of zbar that you have installed. So, if you update any homebrew packages, your code will use the latest, greatest versions. You can see the links I am talking about like this:

ls -l /usr/local/include | grep zbar 
lrwxr-xr-x    1 mark  admin    34 13 Mar 12:15 zbar -> ../Cellar/zbar/0.10_4/include/zbar
lrwxr-xr-x    1 mark  admin    36 13 Mar 12:15 zbar.h -> ../Cellar/zbar/0.10_4/include/zbar.h

ls -l /usr/local/lib | grep zbar 
lrwxr-xr-x    1 mark  admin        41 13 Mar 12:15 libzbar.0.dylib -> ../Cellar/zbar/0.10_4/lib/libzbar.0.dylib
lrwxr-xr-x    1 mark  admin        35 13 Mar 12:15 libzbar.a -> ../Cellar/zbar/0.10_4/lib/libzbar.a
lrwxr-xr-x    1 mark  admin        39 13 Mar 12:15 libzbar.dylib -> ../Cellar/zbar/0.10_4/lib/libzbar.dylib

P.S. You update homebrew with:

brew update && brew upgrade

If you want to use a specific version of zbar, you need to work a little harder.

You get the include path like this:

pkg-config --cflags zbar

Sample Output

-I/usr/local/Cellar/zbar/0.10_4/include 

And the library path like this:

pkg-config --libs zbar

Sample Output

-L/usr/local/Cellar/zbar/0.10_4/lib -lzbar

Then you put those values into Xcode along these lines:

enter image description here

Upvotes: 3

Related Questions