How to install ZXing in Xcode 4

After an upgrading to Xcode 4, I have been having trouble compiling my own ZXing iOS project. That’s why I decided to explain you how to install easily ZXing with Xcode 4.

First of all (for those who don’t know), ZXing is an open-source library to read the 1D/2D barcodes. This library is available on many platforms such as the iOS, Android, Blackberry, ect. You can find it here: http://code.google.com/p/zxing/.

Before to start, be sure that you have the latest version of ZXing on your computer. If you don’t, you must download it via a SVN client here: http://zxing.googlecode.com/svn/trunk/.

To use ZXing into your project in Xcode 4 follow these steps:

  1. Firstly go to the “zxing/iphone/ZXingWidget/” and drag and drop the ZXingWidget.xcodeproj file onto your Xcode “Project navigator” sidebar. If a dialog appears uncheck the “Copy items” and verify that the “Reference Type” is “Relative to Project” before clicking “Add”.

  2. Now we are going to add ZXingWidget as a dependency of your project to allow Xcode to compile it whenever you compile the main project:
    1. First select your project file in the “Project navigator”.
    2. Then select the corresponding target.
    3. After choose the “Build Phases” tab and expand the “Target Dependencies” section.
    4. Click the “+” (add) button to display a dialog.
    5. To finish add the “ZXingWidget” target as shown above.

  3. Now we are going to link the ZXingWidget static library (libZXingWidget.a) to the project:
    1. Firstly choose the “Build Phases” tab and expand the “Link Binary With Libraries” section.
    2. Then click the “+” (add) button to display a dialog.
    3. To finish add the “libZXingWidget.a” which is located in the “Workspace” category as shown above.
    4. By the way add the following iOS frameworks too:
      • AddressBook
      • AddressBookUI
      • AudioToolbox
      • AVFoundation
      • CoreMedia
      • CoreVideo
      • libiconv.dylib

  4. Then you must configure the header search path of your project to allow Xcode to find the ZXingWidget headers. To do that:
    1. In the “Project navigator” select the main project (not the target).
    2. Go to the “Build Settings” tab and search the “Header Search Paths“.
    3. Double-click on it and add:
      • The full path of the “zxing/iphone/ZXingWidget/Classes” directory. Check the “recursive path“.
      • The full path of the “zxing/cpp/core/src/” directory. Uncheck the “recursive path“.

Now you just have to import the “ZXingWidgetController.h” and the “QRCodeReader.h” to your project and use them.
Attention: Make sure that the files in which you are using the ZXing headers have the .mm extension because they use c++ library files.

Voilà! Now all should be ok. I hope it’ll help you!

1 Star2 Stars3 Stars4 Stars5 Stars (36 votes, average: 4.56 out of 5)
Loading...
Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on RedditPin on Pinterest

87 comments

Time limit is exhausted. Please reload CAPTCHA.

  1. Kofi · April 17, 2011

    Hey Yannick, great tutorial. I have a quick question. I followed all the directions, however I get two errors saying:

    ZXingWidgetController.h: No such file or directory
    QRCodeReader.h: No such file or directory

    I’m wondering if it is a problem with the file path of my “zxing-1.6” folder? my header search path? Both?

    I’m not sure where to place my “zxing-1.6” folder that contains:
    -the “iphone” folder (which contains /ZXingWidget/Classes/ZXingWidgetController.h)
    -the “cpp” folder (which contains /core/src/zxing/qrcode/QRCodeReader.h).

    The file path that I put my “zxing-1.6” folder in is /Documents/MyFirstApp (In your case, it would be equivalent to /Documents/testzxing).

    Thanks.

    • Yannick Loriot · April 17, 2011

      Hey,

      Sorry for the late.
      To answer your question you can put your ZXing library where you want. You just have to enter the correct full paths into the “Header Search Paths”.
      For example in your case you should put “/Documents/testzxing/zxing-1.6/iphone/ZXingWidget/Classes” and “/Documents/testzxing/zxing-1.6/cpp/core/src/”.
      I hope it’ll help you.

  2. neil.young · April 17, 2011

    I was following your tutorial and tried some other things, but I still can’t make it run with XCode 4. I’m having 28 issues, all have to do with missing c++ includes (#include and “namespace xxx”)

    Do you have an idea? XCode 4.0.2

  3. neil.young · April 17, 2011

    Forget about the problem. It was the “.m” vs. “.mm”. issue

  4. Marius Lücking · April 17, 2011

    Hi,
    I have the correct paths in the “Header Search Paths” (“/Documents/Library/zxing/iphone/ZXingWidget/Classes” and /Documents/Library/zxing/cpp/core/src). But when I do “#import “ZXingWidgetController.h”” I always get the error “ZXingWidgetController.h: No such file or directory”.
    I hope you can help me.
    Thanks

    • Yannick Loriot · April 17, 2011

      Hi,

      Is your path relative or absolute? Have you try the absolute path like that /Users/${yourusername}/Documents/Library/zxing/iphone/ZXingWidget/Classes?
      It must comes from this.

      I hope it’ll help you.

  5. auto ru · April 17, 2011

    Nice topic – respect !

  6. tatw · April 17, 2011

    Any further comments, Stuck on same.. No such file or directory 🙁

    • Yannick Loriot · April 17, 2011

      Hi,
      I remade the whole procedure and it works perfectly for me. Generally this error occurs if the header search path is not correctly sets. Please recheck the 4th point of the tuto and if it doesn’t work give me more details please, I’ll try to see why it doesn’t work.

  7. Spokane-Dude · April 17, 2011

    Hi Yannick… great help, but I have one error when trying to do a build on XC 4 of ScanTest. The “meat” of the errors are:

    -dynamic not specified the following flags are invalid: -ObjC -M -exported_symbols_list exportList
    can’t locate file for: -lZXingWidget
    file: -lZXingWidget is not an object file (not allowed in a library)
    …libtool failed with exit code 1

    I followed your instructions, but can’t get rid of the 1 error… can you help me? (I would greatly appreciate it!)

    • Yannick Loriot · April 17, 2011

      Hi,
      thank you for your com.
      I made some research but I haven’t found anything about this problem. Have you downloaded the lastest version on the SVN (not the zip available in the downloads section)? (it works perfectly here)

      • Spokane-Dude · April 17, 2011

        Ignore my last post… in response to your question, no, I’m not sure where I got it, so I will download from SVN and give it a go…

      • Spokane-Dude · April 17, 2011

        I don’t see a download from SVN… all I see is ZXing-1.6.zip, which has a date of Sep 2010. Is that the one?

      • Spokane-Dude · April 17, 2011

        I finally got it to work… a combination of having the wrong source and reading your directions very, very carefully!

        Thank you so much… I appreciate your help!

        • Yannick Loriot · April 17, 2011

          Well,
          I’m going to point out that we must download the latest version on the SVN to avoid this kind of issue.
          Thank you for this feedback!

    • Spokane-Dude · April 17, 2011

      I made the changes above from the beginning, now the build is “stuck” (it does nothing)… no errors, nothing!

      What could be wrong?

  8. Kevin · April 17, 2011

    Hello, i have made all the changes you described. After i will run my project there comes 56 errors like…whats wrong???

    Greets from Germany Kevin

    • Yannick Loriot · April 17, 2011

      Hi,

      I can’t reproduce this error with my project. Have you downloaded the latest version of ZXing?
      Otherwise it seems to come from the project architecture. Make sure that your Base SDK is set correctly (3.0 or over) and the architecture is by default to “armv6 armv7”.
      Keep me in touch.

      Greets,
      Yannick

  9. Ming Yang · April 17, 2011

    good job! nice tutorial

  10. n · April 17, 2011

    You’re a genius, thanks so much!

  11. Miguel Angel · April 17, 2011

    Hi.
    I have a problem.
    I create a Test like ScanTest.
    My code is:

    -(IBAction) doSomeThing:(id)sender{
    NSLog(@”Vamos pa ya”);
    ZXingWidgetController *widController = [[ZXingWidgetController alloc] initWithDelegate:self showCancel:YES OneDMode:NO];
    QRCodeReader* qrcodeReader = [[QRCodeReader alloc] init];
    NSSet *readers = [[NSSet alloc ] initWithObjects:qrcodeReader,nil];
    [qrcodeReader release];
    widController.readers = readers;
    [readers release];
    //NSBundle *mainBundle = [NSBundle mainBundle];
    //widController.soundToPlay =
    //[NSURL fileURLWithPath:[mainBundle pathForResource:@”beep-beep” ofType:@”aiff”] isDirectory:NO];
    [self presentModalViewController:widController animated:YES];
    [widController release];
    }

    compile and run in iphone simulator without error, but i try run in my iphone no installer.
    xCode doesnt show any error, only stop deploy.

    Help me please
    Thankyou

    • Yannick Loriot · April 17, 2011

      Hi,

      there may have several sources for this problem.
      The first thing to do is to check if the “Build Configuration” in the “Run” scheme (Product > Edit Scheme…) is set to the Debug mode.
      If all it’s ok just try to clean your projet and to reboot Xcode and your device. Otherwise be sure that no application is already install with the same bundle id on your device.

      I don’t know if it’ll help you but I have no idea where it comes from without more details.

  12. Paciencia · April 17, 2011

    Hi, thanks for the help.
    However I still get the 28 errors on the project even when I change the file to .mm (I’m using xcode 4)
    Any help?

    Thanks

    • Yannick Loriot · April 17, 2011

      Can you tell me what kind of errors you have got please?

      • Paciencia · April 17, 2011

        /Users/macbook/Downloads/QRImplementation/cpp/core/src/zxing/common/Counted.h:26:20: error: iostream: No such file or directory

        /Users/macbook/Downloads/QRImplementation/cpp/core/src/zxing/Result.h:31: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘zxing’

        /Users/macbook/Downloads/QRImplementation/ZXingWidget/Classes/FormatReader.h:35: error: expected ‘)’ before ‘zxing’

        This are errors that I get in a lot of header files such as Reader.h, BitArray.h, Binarizer.h, Array.h, etc

        Thansk

        • Yannick Loriot · April 17, 2011

          These errors are typical that the compiler interprets the file as Objective-C, not Objective-C++. Where did you put the headers “QRCodeReader.h” and “ZXingWidgetController.h”?

          Verify whether the file including ZXing libraries are compiled as Objective-C++.

          http://yannickloriot.com/wp-content/uploads/2011/06/Xcode-Objective-C++

          • Paciencia · April 17, 2011

            I’ve tried to put it on both AppDelegate and then on the ViewController.
            Also changed them both to be Objective-C++ source but still it gives me the same errors

          • Sam · April 17, 2011

            Thank you Yannick. Compiling as Objective-C++ Source exactly fixed this problem. I had the same error as Paciencia, 56 build errors after it found the .h files, I would never have figured this solution out.

            Tutorial is amazing, everything went perfectly smoothly except for that one extra step as flagging the .m file as C++.

        • Ganbledor · April 17, 2011

          Hi Paciencia,
          I’ve got the same issue.
          Have you found any solution?

          THX

  13. John · April 17, 2011

    I am having the same problem as Paciencia and have verified it is being interpreted as objective-c++. Any other reason the compiler would treat as objective-c?

  14. John · April 17, 2011

    In an update to paciencias problem, the solution is to remove the ‘zxing’ from the path:

    The full path of the “zxing/cpp/core/src/” directory. Uncheck the “recursive path“.

    should be

    The full path of the “cpp/core/src/” directory. Uncheck the “recursive path“.

  15. John · April 17, 2011

    To clarify my previous comment, the “zxing” isn’t really an error more of a misunderstanding – this represents the root level of the directory.

    • Paciencia · April 17, 2011

      it brought me down to 12 errors, still not building successfully

      • François · April 17, 2011

        Did you find a solution ?

        I’m having the same problem, i had 28 errors, then John’s solution brought me to 12 errors, but i can’t find the solution…

        Thanks

  16. Bobby · April 17, 2011

    I found these instructions to be great! But for some reason I had some extra steps, I tried to document those here: http://dailypineapple.tumblr.com/post/7371097234/using-zxing-with-phonegap, I also posted a copy of my test project if it helps anyone.

    • Marcin · April 17, 2011

      hey, i have the file no found on ZXingWidgetController.h: No such file or directory
      QRCodeReader.h: No such file or directory
      the paths are set properly, as described in previous comments when i comment out both imports everything compiles nicely-i try to put zxing into a view [view file is mm main is mm just in case] zxing is in app root
      please help i’m in despair with this
      P.S.

      in build phases in settings of my target in link binary with libraries libZXingWidget.a is in RED

  17. Amit Chabra · April 17, 2011

    Thanks Dude
    You writeup was a great help to me. I owe you a beer. 🙂
    Amit ….

  18. leonel · April 17, 2011

    Hi I having this issue .. do you know what can be?

    File:
    bitMatrix.cpp
    Line:
    31 – unsigned int logDigits(unsigned digits) {}

    Error:
    No previous prototype for ‘unsigned int zxing::logDigits(unsigned int)’

    Thank u for time.

  19. David Williams · April 17, 2011

    What I found out was that I had to rename the current version of ZXING such as zxing-1.7 to just plain zxing because the “- and or .” of the directory was confusing the Mac OS Shell as to where to find the relative directories. So I ended up with zxing (mv zing-1.7 to zxing) but still the same contents as the latest release, and then using RELATIVE paths in the Header Search Paths which are:
    ../ZxingWidget/Classes and then ../../cpp/core/src as mentioned in the post above. So that
    was the big deal in linking in truly finding the headers. The other stuff above is spot on.

    • David Williams · April 17, 2011

      That’s for xcode 4.0.1, and I just redid this as of July 27th, 2011. Just did it, it works fine to
      compile and run my ZXING Multireader.

  20. iosupdaterblogger · April 17, 2011

    You will be making many superior tips now there. Dependant on my prior practical knowledge, I’d quite possibly accept you.

  21. Boguslava · April 17, 2011

    hello, i have exact same issue as Marcin mentioned above, please help

  22. Marcin · April 17, 2011

    the no file found was resolved for me by putting in header paths
    $(SRCROOT)/zxing/iphone/ZXingWidget/Classes/ZXingWidgetController.h
    and respectively similar thing for the other file
    hope it will help others too

  23. minh · April 17, 2011

    I was wondering if anyone has any tips and tricks for Zxing on Xcode and iPhone. Something as simple as turning on the flash when Zxing runs for better lighting. Or a modification that makes Zxing better? maybe delay Zxing so that the user came frame the code on their screen because sometimes I barely put the code on and Zxing already takes a snapshot.

  24. Jeremy · April 17, 2011

    Yannick, et…all:

    I’ve followed the install instructions carefully. ScanTest compiles now fine in my xcode 4.2 environment. But at runtime I’m getting the following error. Please advise what my mistake may be:

    dyld: Library not loaded: /usr/lib/libc++abi.dylib
    Referenced from: /Users/Jeremy/Library/Application Support/iPhone Simulator/4.0.2/Applications/77E844B3-4D93-4391-982D-38B546D5BB73/ScanTest.app/ScanTest
    Reason: image not found

    Thanks in advance!

  25. João Ícaro · April 17, 2011

    The solution I found was set the file type of the class that I import the “QRCodeReader” and “ZXingWidgetController” files to “Objective-C++ source”.

    That is it.

  26. Dom · April 17, 2011

    Hi,

    I have this error on build:

    Undefined symbols for architecture armv6:
    “_OBJC_CLASS_$_ZXingWidgetController”, referenced from:
    objc-class-ref in BarcodeScanner.o
    “_OBJC_CLASS_$_QRCodeReader”, referenced from:
    objc-class-ref in BarcodeScanner.o
    ld: symbol(s) not found for architecture armv6
    collect2: ld returned 1 exit status

    Any idea?

    • Myron · April 17, 2011

      Hi, Dom, I faced the same issue today.

      The problem was that I had two “Link Binary With Libraries” sections in the “Build Phases” tab (see step 3 of the tutorial). I added my libraries in the second one and got exactly the same error. Then I removed them and added to the first section and it worked well.

  27. Drew · April 17, 2011

    Hey, I’m getting my project to build fine but when I’m running it on my device i get the following error – ERROR: Plugin ‘BarcodeScanner’ not found, or is not a PGPlugin. Check your plugin mapping in PhoneGap.plist.

    What is the right key/value for the PhoneGap.plist? I’ve tried just about every combination i can think of but am still stuck. Any ideas?

    • proudM · April 17, 2011

      What fixed my plugin prob was:

      BarcodeScanner / BarcodeScanner for the key value pair even though the key for the other was a domain format like “com.phonegap.camera” and “PGCamera”. Hope this helps. I guess the plugin is out of sync with the phonegap codebase?

  28. sweta kishore · April 17, 2011

    Hi,
    i am continuously getting the following error:-

    Unsupported compiler ‘GCC 4.2’ selected for architecture ‘i386’

    Also,i have not included .h and .mm file of the zbar sdk into the plugin folder and ,js file in www folder of my project.do i need to do this??
    Plz help

    • Sasha · April 17, 2011

      Sweta,

      You need to make a few changes to ur build settings in both your Project/Project Target as well as ZXingWidget/ZXingWidget Target

      Change the compiler to : Apple LLVM (Default Compiler)
      Change your architectures to : armv6 armv7 (do this if you want to support the older iOS models too)

      No idea about the PhoneGap parts sorry 🙁

      Hope this helps (somewhat)
      Sasha

  29. Chris · April 17, 2011

    Hi,

    is it possible to use ean / 1D barcodes with this plugin?

  30. Sasha Azad · April 17, 2011

    Hi,

    I was having the same
    “ZXingWidgetController.h” file not found &&
    “QRCodeReader.h” file not found

    I struggled with it for ages then looked up bunch of sites and tutorials and googled to no awail…. FINALLY I HIT UPON THE SOLUTION

    Click on your Project > Project Name under Editor (not target) > Build Settings > Header Search Paths
    If any of the 2 absolute paths that Loriot described above have a ” ” (ie space character) in them, then the header files will not be recognized and the path will be broken into 2 separate paths

    In my case my ZXing folder was in a folder called “Mobile Development”
    So my absolute path was : “/Users/[Name]/Desktop/Mobile Development/ZXing/cpp/core/src/”
    This got broken into : “/Users/[Name]/Desktop/Mobile” && “Development/ZXing/cpp/core/src/”
    Hence XCode couldnt find my .h files

    Hope this helps : REMOVE ALL SPACES IN THE PATH, if you need to replace them wid _ (ie underscores)

    Cheers
    Sasha