compiling opencv 2.4 on a 64 bit mac in Xcode - opencv

I have an opencv project that I've been developing under ubuntu 12.04, on a parellels VM on a mac which has an x86_64 architecture. There have been many screen switching performance issues that I believe are due to the VM, where linux video modes flip around for a couple seconds while camera access is made by the opencv application. I decided to moved the project into Xcode on the mac side of the computer to continue the opencv development. However, I'm not that familiar with xcode and am having trouble getting the project to build correctly there.
I have xcode installed.
I downloaded and decompressed the latest version of opencv on the mac, and ran: ~/src/opencv/build/cmake-gui -G Xcode .. per the instructions from willowgarage and various other locations.
This appeared to work fine (however I'm wondering now if I'm missing an architecture setting in here, although it is 64-bit intel in Xcode).
I then setup an xcode project with the source files from the linux project and changed the include directories to use /opt/local/include/... rather than the /usr/local/include/...
I switched xcode to use the LLVM GCC compiler in the build settings for the project
then set the Apple LLVM Dialog for C++ to Language Dialect to GNU++11 (which seems possibly inconsistant with the line above)
I'm not using a makefile in xcode, (that I'm aware of - it has its own project file...)
I was also running into a linker issue that looked like they may be resolved with the addition of this linker flag:
-lopencv_video
based on a similar posting here: other thread
however in that case the person was using a Makefile in their project.
I've tried adding this linker flag under "Other Linker Flags" in xcode build settings but still get build errors.
I think I may have two issues here, one with the architecture settings when building the opencv libraries with Cmake, and one with the linker flag settings in my project.
Currently the build error list looks like this:
Undefined symbols for architecture x86_64:
"cv::_InputArray::_InputArray(cv::Mat const&)", referenced from:
_main in main.o
"cv::_OutputArray::_OutputArray(cv::Mat&)", referenced from:
_main in main.o
"cv::Mat::deallocate()", referenced from:
cv::Mat::release() in main.o
"cv::Mat::copySize(cv::Mat const&)", referenced from:
cv::Mat::Mat(cv::Mat const&)in main.o
cv::Mat::operator=(cv::Mat const&)in main.o
"cv::Mat::Mat(_IplImage const*, bool)", referenced from:
_main in main.o
"cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)", referenced from:
_main in main.o
---SNIP---
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Can anyone provide some guidance on what to try next?
Thanks,
Walt

steps to compile and run c++ opencv 2.4.4 on mac os x lion 10.7.5 with cmake 2.8.10 and xcode 4.6.1
EDIT: successfully tested with opencv 2.4.5 on mac os x mountain lion 10.8.3 and Xcode 4.5
Having the right tools
download opencv-unix from http://sourceforge.net/projects/opencvlibrary/files/ and untar it wherever
download cmake .dmg from http://www.cmake.org/cmake/resources/software.html and install it
i am assuming you have xcode 4.6 on os x lion which includes the ios sdk 6.1
go to xcode preferences to download and install the Command Line Tools so you have g++ etc.
Use cmake to compile opencv
go to the extracted opencv folder
create a build directory
mkdir build
cd build
cmake -D WITH_TBB=OFF -D BUILD_NEW_PYTHON_SUPPORT=OFF -D BUILD_FAT_JAVA_LIB=OFF -D BUILD_TBB=OFF -D BUILD_EXAMPLES=ON -D CMAKE_CXX_COMPILER=g++ CMAKE_CC_COMPILER=gcc -D CMAKE_OSX_ARCHITECTURES=x86_64 -D BUILD_opencv_java=OFF -G "Unix Makefiles" ..
make -j8
sudo make install
from the build folder, go to bin/ and run one of the tests
./opencv-test-photo
Create your own c++ opencv xcode project
fire up xcode and create a new xcode project
select Command Line Tool for the type of project under os x
open your project's build settings
under Architectures, set Architecture to 64-bit intel. also set Valid Architectures to x86_64
under Build Options, set Compiler for C/C++ to Default Compiler
under Search Paths, set Header Search Paths to /usr/local/include
also under Search Paths, set Library Search Paths to /usr/local/lib
under Apple LLVM compiler 4.2 - Language set C++ Standard Library to libstd++ (With OpenCV 2.4.6, Xcode 5, LLVM 5.0, and 10.8.5, set both language dialect and std library to "Compiler Default" instead)
Add the compiled opencv libraries to your project
go the the Build Phases tab next to Build Settings tab you were in
inside Link Binary With Libraries, click on the + sign and choose Add Other
hit the front slash / on your keyboard and enter /usr/local/lib
hit enter and select the libraries you want to use in your project
make sure you always select libopencv_core.2.4.4.dylib
hit enter and you will see the selected dylibs under your project
write some code
first lets organize the files, right click on your project blueprint icon and select New Group
name the new group opencv or whatever
drag the dylibs and drop them in that group
open main.cpp
copy code from any of the sample tests that came with opencv and paste it here
make sure all the required dylibs are added, for example, if you copied the opencv_test_stitching.cpp code into main.cpp, you will need to add the following libraries in the previous steps
libopencv_core.2.4.4.dylib
libopencv_highgui.2.4.4.dylib
libopencv_stitching.2.4.4.dylib
Cheers.

Related

Xcode - Missing Architecture (library linking)

For the recent couple of days I have had this compiler error in Xcode every time I've built my project:
ld: warning: ignoring file /.../(Framework Name).framework/(Framework), missing required architecture x86_64 in file /.../(Framework Name).framework/(Framework) (2 slices)
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_CLSNAME", referenced from:
objc-class-ref in ViewController.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I also can't run this code on the iPhone 5 simulator (i386).
The iPhone device I have is outdated and can't run iOS 8. I tried setting the build settings to iOS 7 but I get an error regarding the lack of support for dynamic libraries before iOS 8.
Things to note:
I am using 2 projects, in 2 different workspaces. (one for the library and one for the demo app. Tests of the same code with the unit tests in the library project are successful.)
I used to be able to link the demo project with the library project until I had to do some changes in the library code. After I exported the newer version this error started occurring.
In order to get the library file/folder (.framework) I used the Archive option.
Running lipo -info outputs Architectures in the fat file: lib.a armv7 arm64
I have C++ code in the library project (unlikely to be the issue)
In the library project, I am using another external library I installed with Cocoa Pods (also used to work before changes in the library code)
Solutions I tried:
Changing the Architectures and the Valid Architectures list in Build Settings.
Setting "Build Active Architecture Only" to "No" in Build Settings.
Creating a new project (both for the demo and the library)
Making the library a static library (new project, got the same warning, no error, couldn't #import the files I needed)
I only started working with iOS and Xcode a couple of months ago and I am not super-experienced with everything there yet, so please try to explain everything with as much detail as you can.
Thanks in advance to anyone trying to help :)
Did you add the framework it says is missing? Also, try to delete the Derived Data folder and then clean the project. If that does not work, then turn off Modules in build settings.

How to Use LibXtract with iOS project?

I am trying to extract some features from audio files, MFCC to be accurate. Then I found a C library LibXtract, that would do the job for me.
I am trying to use LibXtract in iOS project, I have followed this tutorial
http://inote.apptrek.net/2011/10/howto-compile-native-c-codes-to-a-library-for-ios-development-in-xcode-take-mosquitto-for-an-example/
but I got some problems in process, first I got this warning
ld: warning: ignoring file ......./libLibXtract.a, missing required architecture i386 in file ..../libLibXtract.a
and those errors
Undefined symbols for architecture i386:
"_xtract_asdf", referenced from:
-[VoiceRecAppDelegate applicationWillResignActive:] in VoiceRecAppDelegate.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So I would like to ask for your help in this problem, I would like to extract the MFCC from scratch with only obj-C code if possible.
Regards,
Omar
It sounds like you are trying to build your application with the Simulator as a target, but the LibXtract static library was not built for the i386 architecture.
You can confirm this with:
lipo -info /path/to/libxtract.a
The result from lipo should list the i386 architecture.
If it doesn't, then check that iphonesimulator is included in the Supported Platforms build setting in your LibXtract Xcode project and that i386 is included in the list of architectures.
It is also possible to compile autotools-based libraries for iOS/Simulator from the command line. Some instructions for this can be found here. Note: these are a little out-of-date so some of the options may need tweaking, although the principle remains the same.
However, it is far simpler to compile LibXtract into your application by dragging the LibXtract sources into your application's Xcode project!
I generally tend to have a "Libraries" group in my projects, under which I place third party sources (like LibXtract), as follows:
When dragging the files into your project ensure that you check the checkbox for your application in "Add to targets".
The LibXtract sources will then be compiled into your application and you can remove libxtract.a from your linker settings (or frameworks).

Building a c based static library for iOS

I need a network library for my c library so I can use it on both iOS and android. I've been searching around and tried to build libcurl as a static library but wasn't successful.
I built it with the following settings
$ export CC="clang"
$ export CFLAGS="-Wall -g -std=c11 -pipe -Os -gdwarf-2"
$ ./configure --enable-static
make install
It runs fine when I test the library with my test.c library. However, when I run it on my iOS project, I got a mismatch on CurlchkszEQ(long, CURL_SIZEOF_LONG)
I think the problem was that CURL_SIZEOF_LONG is set to be 8 while long in iOS is 4.
Does anyone know what went wrong with my config setting?
You want to cross-compile libcurl for iOS but the problem here is that you use clang which by default produces 64-bit x86_64 object files.
In other words you need to work with the iOS toolchain (xcrun -sdk iphoneos clang), provide the right architecture flag(s)[1] (e.g -arch armv7) and use the right sysroot path.
I recommend you to refer to:
curl-ios-build-scripts: a collection of handy scripts used to build (lib)curl for iOS 5+ and OSX 10.7+,
or, http://seiryu.home.comcast.net/~seiryu/libcurl-ios.html which provides a ready-to-use a precompiled binary.
[1]: you may want to produce a fat library that targets ARMv7, ARMv7s and i386 (simulator) architectures.

Linker errors after upgrading Xcode to 4.5.2 and OpenCV to 2.4.3

My project was working just fine until this morning. I was using xcode 4.3, and an older version of OpenCV (I'm not sure about the exact version). OSX was already 10.7.x, but not 10.7.5
Today, after upgrading OSX to 10.7.5, xcode to 4.5.2, and downloading OpenCV 2.4.3, I am getting the following linker errors when trying to build the project:
Undefined symbols for architecture armv7:
"_OBJC_CLASS_$_ALAssetsLibrary", referenced from:
objc-class-ref in opencv2(cap_ios_video_camera.o)
"cv::FeatureDetector::create(std::string const&)", referenced from:
-[ImageAnalyzer detectBlobs:] in ImageAnalyzer.o
"cv::FeatureDetector::detect(cv::Mat const&, std::vector >&, cv::Mat const&) const", referenced from:
-[ImageAnalyzer detectBlobs:] in ImageAnalyzer.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)
In order to use the new opencv framework I removed the reference to the old framework, and referenced the project to the official prebuilt opencv2.framework downloaded from here.
I also removed the reference to libz.dylib, and added a reference to libc++.dylib instead.
Last step was to update the prefix file to the new framework. The relevant part in the prefix file now looks like this:
#ifdef __cplusplus
#import <opencv2/opencv.hpp>
#endif
It all narrowed down to these 4 linker errors I can't seem to get rid of. I tried using libstdc++.dylib, but I am getting even more errors. I also tried building OpenCV myself as explained here, but I am still getting the same errors as the prebuilt framework.
What did I miss? Is there anything else I need to change in my project?
UPDATE:
As seen here, setting the "C++ Standard Library" to "libc++ (LLVM C++ standard libray with C++ 11 support" yielded only one error:
clang: error: invalid deployment target for -stdlib=libc++ (requires iOS 5.0 or later)
Changing the deployment target to iOS 5 finally got my project to run again.
Does this mean OpenCV 2.4.3 doesn't work on iOS versions older than 5?
steps to compile and run c++ opencv 2.4.4 on mac os x lion 10.7.5 with cmake 2.8.10 and xcode 4.6.1
Having the right tools
download opencv-unix from http://sourceforge.net/projects/opencvlibrary/files/ and untar it wherever
download cmake .dmg from http://www.cmake.org/cmake/resources/software.html and install it
i am assuming you have xcode 4.6 on os x lion which includes the ios sdk 6.1
go to xcode preferences to download and install the Command Line Tools so you have g++ etc.
Use cmake to compile opencv
go to the extracted opencv folder
create a build directory
mkdir build
cd build
cmake -D WITH_TBB=OFF -D BUILD_NEW_PYTHON_SUPPORT=OFF -D BUILD_FAT_JAVA_LIB=OFF -D BUILD_TBB=OFF -D BUILD_EXAMPLES=ON -D CMAKE_CXX_COMPILER=g++ CMAKE_CC_COMPILER=gcc -D CMAKE_OSX_ARCHITECTURES=x86_64 -D BUILD_opencv_java=OFF -G "Unix Makefiles" ..
make -j8
sudo make install
from the build folder, go to bin/ and run one of the tests
./opencv_test_stitching
Create your own c++ opencv xcode project
fire up xcode and create a new xcode project
select Command Line Tool for the type of project under os x
open your project's build settings
under Architectures, set Architecture to 64-bit intel. also set Valid Architectures to x86_64
under Build Options, set Compiler for C/C++ to Default Compiler
under Search Paths, set Header Search Paths to /usr/local/include
also under Search Paths, set Library Search Paths to /usr/local/lib
under Apple LLVM compiler 4.2 - Language set C++ Standard Library to libstd++ (For OpenCV 2.4.6, Xcode 5, LLVM 5.0, and 10.8.5, set both language dialect and std library to "Compiler Default" instead of "libstd++")
Add the compiled opencv libraries to your project
go the the Build Phases tab next to Build Settings tab you were in
inside Link Binary With Libraries, click on the + sign and choose Add Other
hit the front slash / on your keyboard and enter /usr/local/lib
hit enter and select the libraries you want to use in your project
make sure you always select libopencv_core.2.4.4.dylib
hit enter and you will see the selected dylibs under your project
write some code
first lets organize the files, right click on your project blueprint icon and select New Group
name the new group opencv or whatever
drag the dylibs and drop them in that group
open main.cpp
copy code from any of the sample tests that came with opencv and paste it here
make sure all the required dylibs are added, for example, if you copied the opencv_test_stitching.cpp code into main.cpp, you will need to add the following libraries in the previous steps
libopencv_core.2.4.4.dylib
libopencv_highgui.2.4.4.dylib
libopencv_stitching.2.4.4.dylib
Cheers.
It seems that your project is missing the framework AssetsLibrary.
Select the top node in the project navigator. (The project and targets page with the build settings appears.)
Select the target.
Select Summary.
Scroll down to Linked Frameworks and Libraries.
Click the Plus icon at the end of the table and select AssetsLibrary.framework.
Click Add.
Then try to build it again.
Since I can't seem to get an answer regarding the versions (neither here nor at the OpenCV Q&A site), I'm going to post this as an answer, as it at least solved the issue. This is described here.
In your project's Build Settings, go down to the section Apple LLVM compiler 4.1-Language.
There:
Set C++ Language Dialect to Compiler Default
Set C++ Standard Libray to libc++ (LLVM C++ standard libray with C++ 11 support
After doing the above, I stopped getting those linker errors, and only got one error instead, which stated that only iOS 5 and above is supported. Changing the Deployment Target to 5.0 in the project summery did the trick.
On a final note, I'm still not sure what it means, regarding OpenCV 2.4.3's compatibility with iOS versions older than 5.
clang: error: invalid deployment target for -stdlib=libc++ (requires iOS 5.0 or later) to remove this error.
GoTo BuildSettings. Set c++ standard library to compiler default. The
error will get removed surely.
Just to get this into the postings somewhere in case someone else runs into the same thing. If you follow all the great advice aboutsetting the proper c++ library to link against for building an iOS app BUT still get the link errors for undefined symbols make sure your code files are set to compile as c++! That is rename your .m to .mm and .h to .hpp. It's the little things...
Instead of using terminal commands given in the opencv installation guide in official website, use the following commands to build opencv from terminal. Worked for me.
cd OpenCV-2.3.1
mkdir build
cd build
cmake -G "Unix Makefiles" ..
make
sudo make install
Go Xcode/General/Linked Frameworks and Libraries
Press "+" button
type: AssetsLibrary
Select "AssetsLibrary.framework" and import it
Done
Good luck!

How to build V8 for armv7

i'm trying to use Google's v8 in my iPhone Application.
I built the libraries using "make arm" as stated on Google's website.
Unfortunately I get several linker errors telling me that the library is build "for archive".
The terminal tells me that the library's architecture is i386:
lipo -info libv8_base.a
input file libv8_base.a is not a fat file
Non-fat file: libv8_base.a is architecture: i386
So, how do I actually build a armv7 library?
BTW: I don't really know much about this library building stuff. Maybe I can only build for ARM on an actual ARM device? I don't get why I can't just download the prebuilt files anywhere, too.
You can find instructions to cross-compile for ARM on the v8 wiki.
When running on an x86 machine
make arm
builds a simulator, not an actual ARM binary.
You can also compile a release shell natively if you have ARM hardware:
scons arch=arm -j2
EDIT:
You may also want to learn about cross-compilation.
There is a simulator for armv7 built in the v8 source. If you check v8/src/arm/ you will find simulator files.
To build, make sure your CC, CXX and LINK are point to native gcc / g++ tools. And then do
Make arm.release -j8
This will make for arm and look into *.gypi files for more build options. Hope this is useful.

Resources