Cross compiling the Hello World cpp application was an easy one (see my last blog). Now I wanted to write a simple program for a USB webcam using the OpenCV library. I had already written and run the code on OS X Yosemite, so I didn't expect any problems - or so I thought. Many frustrating hours later, it seems that OpenCV is compiling... But let's start from the beginning.
I had built and installed OpenCV on the iMac before, but using this build was not going to work on the Debian target. OpenCV had to be cross compiled as well before my little program could be compiled.
OpenCV 2.4.10 was downloaded and copied to ~/bbb/source/opencv-2.4.10. Another directory ~/bbb/build/opencv-2.4.10 was created to hold the build. The CMake GUI was started and the two folders were set as source and build path.
Options for cross-compiling were specified manually:
Iterating between clicking configure, looking for errors and updating build variables led to better and better results. This is what I had to do:
PYTHON_INCLUDE_DIR:PATH=/usr/include/python2.7
PYTHON_LIBRARY_DEBUG:FILEPATH=/usr/lib/python2.7
PYTHON_LIBRARY_RELEASE:FILEPATH=/usr/lib/python2.7
PYTHON_LIBRARY:FILEPATH=/usr/lib/python2.7
PYTHON_EXECUTABLE:FILEPATH=/usr/bin/python2.7
WITH_JASPER:BOOL=0
WITH_JPEG:BOOL=0
WITH_PNG:BOOL=0
WITH_TIFF:BOOL=0
(...one variable is still missing, see further down...)
Everything looked good, I started Terminal, changed to the build folder, and run make.
The build started, but failed all too soon with
Linking C static library libz.a
Error running link command: No such file or directory
make[2]: *** [libz.a] Error 2
make[1]: *** [CMakeFiles/zlibstatic.dir/all] Error 2
make: *** [all] Error 2
make VERBOSE=1
and give much more information:
Linking C static library libz.a
/Applications/CMake.app/Contents/bin/cmake -P CMakeFiles/zlibstatic.dir/cmake_clean_target.cmake
/Applications/CMake.app/Contents/bin/cmake -E cmake_link_script CMakeFiles/zlibstatic.dir/link.txt --verbose=1
CMAKE_AR-NOTFOUND cq libz.a CMakeFiles/zlibstatic.dir/adler32.o CMakeFiles/zlibstatic.dir/compress.o CMakeFiles/zlibstatic.dir/crc32.o CMakeFiles/zlibstatic.dir/deflate.o CMakeFiles/zlibstatic.dir/gzclose.o CMakeFiles/zlibstatic.dir/gzlib.o CMakeFiles/zlibstatic.dir/gzread.o CMakeFiles/zlibstatic.dir/gzwrite.o CMakeFiles/zlibstatic.dir/inflate.o CMakeFiles/zlibstatic.dir/infback.o CMakeFiles/zlibstatic.dir/inftrees.o CMakeFiles/zlibstatic.dir/inffast.o CMakeFiles/zlibstatic.dir/trees.o CMakeFiles/zlibstatic.dir/uncompr.o CMakeFiles/zlibstatic.dir/zutil.o
Error running link command: No such file or directory
make[2]: *** [libz.a] Error 2
make[1]: *** [CMakeFiles/zlibstatic.dir/all] Error 2
make: *** [all] Error 2
Aha! CMAKE_AR-NOTFOUND was the culprit. But only what means that? AR is an archiver, and obviously CMake can't find it. Easy: add another CMake variable pointing to the archiver in our toolchain:
CMAKE_AR:FILEPATH=/usr/local/willtm/bin/arm-willtm-linux-gnueabi-ar
This actually solved my problem, if I only had known that in order to make the variable work, I had to delete everything except CMakeCache.txt from the build directory and generate again with CMake. Cost me another few hours...
Finally OpenCV has been successfully cross compiled. Now let's go back to Eclipse and see if we can compile code that's using it.
Afterthought: To have all cross compiled libraries at one location, I made a new folder ~/bbb_target/ with the directory structure of the BeagleBone root (i.e. ./usr, ./usr/local etc.). Setting the CMake variable
CMAKE_INSTALL_PREFIX:PATH=/Users/<me>/bbb_target/usr/local/lib
and generating everything again will result in make install copying the libraries to bbb_target, where they can be referenced by Eclipse (so I hope).
To sum it up, here again all manual changes to the CMake variables:
CMAKE_INSTALL_PREFIX:PATH=/Users/<me>/bbb_target/usr/local/lib
CMAKE_AR:FILEPATH=/usr/local/willtm/bin/arm-willtm-linux-gnueabi-ar
PYTHON_INCLUDE_DIR:PATH=/usr/include/python2.7
PYTHON_LIBRARY_DEBUG:FILEPATH=/usr/lib/python2.7
PYTHON_LIBRARY_RELEASE:FILEPATH=/usr/lib/python2.7
PYTHON_LIBRARY:FILEPATH=/usr/lib/python2.7
PYTHON_EXECUTABLE:FILEPATH=/usr/bin/python2.7
WITH_JASPER:BOOL=0
WITH_JPEG:BOOL=0
WITH_PNG:BOOL=0
WITH_TIFF:BOOL=0
PYTHON_LIBRARY_RELEASE:FILEPATH=/usr/lib/python2.7
PYTHON_LIBRARY:FILEPATH=/usr/lib/python2.7
PYTHON_EXECUTABLE:FILEPATH=/usr/bin/python2.7
WITH_JASPER:BOOL=0
WITH_JPEG:BOOL=0
WITH_PNG:BOOL=0
WITH_TIFF:BOOL=0