Installing OpenCV 3 on Mac OS X as a framework - c++

I wanted to make an app on OS X El Capitan using OpenCV. I decided to use the latest version, version 3.0.0 released on June 4th, 2015. I had installed version 2.4.x using brew, but since version 3 now has a build script for osx that builds a framework, I wanted to use that method.
After downloading version 3 from the OpenCV.org, I opened a terminal window in the opencv-3.0.0 directory and executed the build_framework.py script as follows:
platforms/osx/build_framework.py osx
(The osx argument tells the script to make a directory named osx to output the framework there)
Everything built without a hitch so I then added the framework built in the osx directory to my project.
Much to my surprise my project would not build without errors. First of all were the two following problems. Here is a screenshot of the first:
If the first problem did not show up, the #ifndef __cplusplus # error for each header file in the opencv2.framework would get triggered.
It turns out that problem was that the C++ headers need to be called before the Objective-C headers, so I added the following to a PCH file to the project:
You can actually add this code to the header file that need the opencv.hpp header file instead of make a PCH file for the project. You should also be sure to say #include and not #import, but they should both work.
Once I figured out those problems I was still stuck with 39 undefined symbols for architecture x86_64.
At first I thought the framework was not including the x86_64 versions, but a quick check revealed that it included both i386 and x86_64 versions of the object files and that my Project settings were all correct.
Next I looked up the names of some of the undefined symbols and found that they were part of a project named OpenCL. OpenCL is supposed to accelerate some of the functions and was included in opencv3. At first I thought that the module was not getting built, but after reading through the build_framework.py build script I found that it was getting its settings from the CMakeLists.txt file. Reading this showed that following OpenCL flags:
WITH_OPENCL
WITH_OPENCLAMDFFT
WITH_OPENCLAMDBLAS
were being set
A little more reading lead me to believe that Mac OS X El Capitan is not compatible with OpenCL, as it appears there needs to be kernel support. So I changed the CMakeLists.txt file to not build OpenCL by adding "AND NOT APPLE"
WITH_OPENCL
WITH_OPENCLAMDFFT
WITH_OPENCLAMDBLAS
After the changes to the CMakeLists.txt file and building the framework again, my program was able to link with opencv2.framework (not sure why they still call it opencv2 instead of opencv3) and run.
It took me more time than I would like to admit, so I thought I would share my experience here in the hopes that it save someone else all the frustration getting OpenCV 3 working with their Mac OS X app. Cheers!

After nearly 2 years, I encountered the same problem. However, I found a more decent solution.
Contrary to your thought, macOS actually is compatible with OpenCL, although it is not linked into the OpenCV2.framework. I tried to link my binary with liblapack.tbd, libcblas.tbd and OpenCL.framework before I link opencv2.framework and it works like a charm.
So just go to the Project Settings -> Build Phases -> Link Binary With Libraries and add the following:
Link Binary With Libraries section

Related

Xcode framework no longer found after second build

Since I upgraded to Xcode 11 every new project I start that includes a framework can be no longer be compiled after the second build of the project.
For example including SDL2 using this command:
#include <SDL2/SDL.h>
will result in this error:
'SDL2/SDL.h' file not found
after the project has been compiled a second time.
Projects that have been created in Xcode 10 work normal in Xcode 11.
I tried changing the embedding options of the frameworks but no setting works. This error occurs in all Xcode 11 versions (0 - 2.1).
I am using macOS Version 10.14.6.
Why does this happen or how can this be fixed?
I hade the same problem with XCode 11.1. What I found out was that I needed to add the Header Search Path in Build Settings/Search Paths to:
/Library/Frameworks/SDL2.framework/Versions/A/Headers
And then it worked. It looks like the path is changed.
I finally found a solution. I added the frameworks with the menu in this screenshot to my project:
In Xcode 9 and 10 this did the same as adding the framework using this menu:
But now in Xcode 11 this isn't the same anymore. Adding the framework using the first menu will copy the framework from it's original path into the build folder of the project but for some reason Xcode does not copy the Headers folder in the framework. I think this is a bug but it seems like they haven't fixed it up to Xcode version 11.2.1 at least.
So now the solution to this problem is adding the framework using the Build Phases menu in the second picture.
SDL2 isn't a framework. Rather it is either a static or dynamic lib. Unless of course you are using some other custom made framework for SDL2.
There are two scenarios you would typically encounter:
SDL2 installed in the default location such as /usr/local
SDL2 installed in a custom place (which is how I do it)
I am guessing you are doing the former. If this is the case, it is possible that with Xcode 11 the default system header search path has somehow changed to not include /usr/local (or where you have installed it).
To fix this, you should first locate your SDL2 library. In particular where the include files are.
Then in Build Settings, go to Search Paths:System Header Search Paths and enter that path in.
Keep in mind the #include is using SDL2 as part of the path, so you do not want to put the SDL2 part in the path. Here is an example of my directory structure. So my path entry for this is $(PROJECT_DIR)/3rdParty/SDL2/include. You can see in my project directory, I have a 3rdParty directory which I use to hold the 3rd Party libs I integrate into the project.
You're using angled brackets for inclusion, which utilizes the system path.
Note you also may have to do update how you link against SDL2 (ie. it may not be able to find it).

How to compile Quantlib via Xcode?

I am trying to install QuantLib on my Mac running OSX 10.11.6. Installed Boost 1.59 via MacPorts and then followed these instructions.
I used these additional environment variables
./configure --with-boost-include=/opt/local/include/ \
--with-boost-lib=/opt/local/lib/ --prefix=/opt/local/ \
CXXFLAGS='-O2 -stdlib=libstdc++ -mmacosx-version-min=10.6' \
LDFLAGS='-stdlib=libstdc++ -mmacosx-version-min=10.6'
and then make && sudo make install.
However when I run the Bermuda Swaption test it gave me the same error described here.
Little premise: I don't know anything about C++. I need QuantLib to work on Python. So I read carefully the answer by SmallChess and tried to solve it by myself. As I read in his answer
You can't just compile BermudanSwaption.cpp and hope everything would be fine. You have to compile the entire QuantLib library and link with the generated library files. Please google "compiling and linking C++" for more information.
By far, the easiest way to make it happen on Mac is to do it with Xcode. You will need to create a new Xcode project, and import the entire Quantlib project files into it. Next, you will need to create a main() function. Xcode does the compiling and linking for your automatically.
This is what I exactly did:
created a new project in Xcode (version 8.2.1)(file/new project/Command Line Tool/"HelloWorld"/Documents/create)
selected Targets, Build Phases and Link Binary With Library. Added libQuantLib.0.dylib
set libstdc++(GNUC++ standard library) as C++ Standard Library in Build Settings
Modified Header Search Paths to include: /opt/local/include/, and Library Search Paths to include: /opt/local/lib
C++ Language Dialect is set on Compiler Default.
Dragged the ql folder onto the left window of the Xcode
Now, I managed to copy a simple code which includes the library and even if there are many warnings, it runs. Still when I run on the Terminal the command for the Bermuda Swaption test I get the same error. What am I doing wrong?
Additional info (may or may not be useful): if I change the C++ Standard Library setting on Xcode to libc++, I get on Xcode the same error I get when i try the Bermuda Swaption test (ld: symbol(s) not found for architecture x86_64).
Any help would be very much appreciated
Regards
EDIT: you can find a picture of the code at https://i.stack.imgur.com/1zhjO.png

Following a Lynda.com c++ tutorial and having problems setting up paths on Eclipse (OSX 10.9.3)

I am currently learning C++ using Lynda.com tutorials, I am a complete beginner and am following the tutorials on how to set up Eclipse on my Mac.
When I initially run the version-test.cpp program provided, it runs fine with the expected output (GCC version 4.2.1)
The tutorial requires my Mac to be running GCC version 4.7.0 or above and it explains how to upgrade GCC. The tutorial instructs me to save the updated GCC files within my home directory within the folder hpc-gcc and then follows on to explain the method to set my PATHs to find this folder.
Now when I run the version-test.cpp program it fails with the following error
dyld: Library not loaded: /usr/local/lib/libmpc.3.dylib
Referenced from: /Users/gary/hpc-gcc/bin/../libexec/gcc/x86_64-apple-darwin13.1.0/4.9.0/cc1plus
Reason: image not found
So from this message I can see that the system cannot find the required files within /usr/local/lib.
I am able to copy the files from hpc-gcc/bin to /usr/local/lib and have the program compile and run correctly but I am hoping to find out why Eclipse cannot read the required files directly from ~/hpc-gcc/bin
I have spent the last 2 hours searching forums and search engines for the answer but I am no further forward. I realise I can simply have the GCC files within /usr/local/lib and it will work but I want to understand where I am going wrong.
Solved.
You need to go into the properties of the Working folder (if you are following the lynda.com program you will understand the working folder) and under C/C++ Build -> Environment menu (Where we added in the LIBRARY_PATH, PATH, AND CPATH variables) and add in DYLD_LIBRARY_PATH with a value of ${HOME}/hpc-gcc/lib.
Remember the C/C++ Build "menus" are for build and debug, so the project is not able to find the library for the building process.
And you will be good to go.

How do I compile libnoise on Mac OS X Mountain Lio

I am new to the Mac OS X environment when it comes to compiling linux based libraries. Whenever I used a library i just downloaded the .framework file, added it to my /Library/Frameworks and included it in my XCODE project, and all was fine.
Now I am stuck with libnoise. I want to use it on my project and I have no idea how to generate the .framework file/directory.
Can you help me please?
If you have libnoise, most likely it contains some sort of a Makefile or a configure script.
By running the
./configure
make all
you will get the library file (libnoise.a) for your platform, the OSX10.8.
Framework is essentially a folder with specific layout and a .plist file. To generate such a folder automatically, you may create an expty Xcode project of the type Framework and add the libnoise.a you've just created as a linker's input.
There is a detailed instruction on how to create the Framework from static libraries (.a files): http://www.blackdogfoundry.com/blog/creating-a-library-to-be-shared-between-ios-and-mac-os-x/
You might be missing the header files in you framework, but then can be also added to the Xcode project from libnoise sources.
This SO answer may be of use also: Difference between framework and static library in xcode4, and how to call them
Apple's documentation is also good: https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPFrameworks/Tasks/CreatingFrameworks.html
I'm not entirely sure if this is what was meant by "with a different fork and cmake"
but I got libnoise to run in my mac using this git repo.
https://github.com/qknight/libnoise

Why is my program trying to use libluajit-5.1.so.2 instead of libluajit.so?

I have a project I'm writing that uses LuaJIT. I'm trying to run my project on a computer I have not run it on in a while. It used to run just fine but now when I try to run it it complains.
I have LuaJIT in my source tree, and it builds just fine. I'm using CMake to generate my make files, and as far as I can tell CMake finds the file libluajit.so, but when I run my program, I get the following error:
../build/game/game: error while loading shared libraries: libluajit-5.1.so.2: cannot open shared object file: No such file or directory
I don't know why it's looking for that version of the library instead of libluajit.so. This is Ubuntu linux for what it's worth. I can add more details if necessary, I can add more details if necessary, I'm not sure what info would be helpful to figure out happening.
Edit:
To build and link the program I have these lines in the file CMakeLists.txt (this is abbreviated a bit to just show the relevant bits)
find_package(LuaJIT REQUIRED)
set(Extern_LIBS luajit)
add_executable(proj ${proj_Sources})
target_link_libraries(proj ${Extern_LIBS})
After I run cmake on my source directory, I run make. Then to run it I just run ./proj
When you built it, the ".so" was actually a symlink to the library. Verisioned filenames and SONAMEs are used so that multiple versions of a library can coexist, preventing problems commonly found on... other operating systems whereby older software is incompatible with the newer library, and newer software is incompatible with the older library.