How to include OpenCV include into XCode 4 c++ project? - c++

I have seen posts on this before, but none of the answers seemed to work for me. I just installed OpenCV on my Mac and got it to work fine with g++ from the command line. I installed it using Mac Ports:
$ sudo port install opencv configure.compiler=llvm-gcc-4.2
To compile with g++, I use the command:
g++ myfile.cpp -o myprogram -I/opt/local/include -L/opt/local/lib -lopencv_core.2.4.2 -lopencv_calib3d.2.4.2
And my OpenCV include header in the main.cpp file is:
#include "opencv2/opencv.hpp"
And all works well.
Now, when I tried to include the library in Xcode, it simply says 'opencv2/opencv.hpp' file not found. To include the library in Xcode, I followed some detailed instructions. First, to the project target, I added /opt/local/lib to the library search paths, and /opt/local/include, /opt/local/include/opencv and /opt/local/include/opencv2 to the header search paths under the Build Settings tab. Then I clicked on Build Phases, and to Link Binary With Libraries I added all of the OpenCV .dylib files in /opt/local/lib (all 11 of them). This didn't work. I've tried many other things, including adding to user search paths and setting up "Other Linker Flags" for the target. Nothing worked. It can't find the file. If I use the exact path (#include "/opt/local/include/opencv2/opencv.hpp") it can't find all the other header files.
The version of OpenCV is 2.4.2. I am using Xcode 4.3.3 on OS X Lion 10.7.4. This is a C++ project. Any ideas?

This problem can be solved by going to the project properties and click on the target.
In the build settings search "header search path".
And there you add "/opt/local/include".
By doing this you're telling Xcode to look for the header files in that directory also.
Now you can use:
#include <opencv2/opencv.hpp>
If you don't want to do this every time you start a new project you can add the .h and .hpp files to the defaulti include directory Xcode uses. You can find this directory in Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/usr/include .
You will have to add the binaries to your project as well. For that you go to "build phases" and then add the binaries in the "Link binaries with libraries" section.
I hope this helps you out.

Related

Xcode "ld: library not found" for C++ makefile project

OS: Mac 10.14.6 -- Xcode ver: 11.3.1
I have an external C++ project (that uses an existing makefile to build) that I imported into Xcode, and when I try to build it I get the following error message: ld: library not found for -ljpeg.
Strangely, when I run the same build command ($make all-recursive) from the command line, it builds successfully. I've double-checked and the relevant .dylib files DO exist in /usr/local/lib. From reading around the web it seemed like Xcode build-settings needed library search paths to include the directory where the .dylib files are, so I added LIBRARY_SEARCH_PATHS under target "Build Settings", but that did not solve the problem.
Is there any reason that comes to mind why Xcode's build command would fail but mine wouldn't?
Link should also receive a flag of the form -L<path> which has the path to the jpeg library's parent folder, before the -ljpeg flag.
So add -L/usr/local/lib to the other linker flags build setting and try again.
Easy way to find the correct flags is running pkg-config:
pkg-config --libs libjpeg
-L/usr/local/Cellar/jpeg/9d/lib -ljpeg
If that fails, you can:
Copy the dylib from its original location.
Paste it in a folder #executable_path/../Resources/lib. Create those folders if needed.
Then rebuild.
Most robust way is to pass the full path in -l option, and not break it in two components.

How can Codeblocks find my header files even without adding any search paths

I'm learning to use OpenCV (and C++) in Codeblocks. What confuses me, however, is that when I start to include header files from OpenCV in my main.cpp file, Codeblocks automatically suggests to me the files as shown in the image below.
I have not included any search paths to project build options, so how is this possible that Codeblocks can find the files? Is there some other variable working here that I'm unaware of?
Note that I'm a beginner with both Codeblocks and OpenCV and that I only have a little experience with C++.
Thank you
Of course when you install an IDE like code::blocks by default, it knows about standard path for library on your OS.
On my OS -> Ubuntu that is /usr/include
It only searches on a standard path, except you add one. If you install your library by command-line, it goes to the standard place, if you installed manually, then it depends on your option you added to installation. I can not see you screen-shot but it has access to /usr/include by default.
For more detail on Linux and OpenCV
And here is a screen-shot of codeblock on Ubuntu that I added some 3rd-party library
NOTE:
if you install any libraries by command-line, just use it.
But if you have installed them manually, you need to add 2 things to codeblock.
1. First is your path for header file
2. Second is your path for linker
And you see it in screen-shot that say: Search Directory
First is for header and second is for linker

Setting up OpenGL in CodeBlocks

I am having trouble getting the right setings in order to use OpenGl in CodeBlocks.
I have used the instructions from this tutorial: GLUT but for my project to run I need the following flags: -lGL -lGLU -lglut which I can set in the Other linker options tab from Build options. When I do this, the compiler says cannot find -lGL -lGLU -lglut. What do I have to install in order for these libraries to work? GL.h GLU.h glut.h? and if yes how can I link them to the project? By adding them in the Link libraries tab? And also from the project tree which appears in Build options does the name of the project have to be selected when I install these libraries, or Debug or Release?
In Build options, if I select the name of the project, at Link libraries I have the following: glut32, opengl32, glu32, winmm, gdi32 but I don't remember giving a path for them. Are they correct or do I have to change them as well?
I would like to mention that the created project is a GLUT project and that I am using Windows 7.
The issue is that you are telling Code::Blocks to look for opengl32.lib, glu32.lib, etc. but not where to look for them. Hence the error during linkage.
Under Project Build Options -> Search Directories -> Linker you need to add the directories containing your OpenGL libraries. Example:
Note that the directory containing your OpenGL libraries will probably be different from mine, since according to the link in your question they should be wherever you put MinGW.
You will also need to make sure you add to the Search Directories the location of the OpenGL header files. Example:
This is the folder that contains the gl subdirectory.
After downloading the GLUT bin zip file (considering you already installed codeblocks earlier), you extract all the files in it and copy those three files separately. The glut32.lib goes to c:\program files\mingw\lib and copy glut32.dll to c:\windows\system and copy glut.h (header file) to c:\program files\mingw\include\GL
Then open codeblocks and go for new project>GLUT. Then set up the GLUT location to Mingw(in program files) and then finish. It worked for me just fine.

Using MySQL in XCODE for C++ project

Currently, I have a project in C++ where i have to get a mysql database and ask for tablevalues. I understand that now I have to import the mysql library.
So I do so by going to this site:
MySQL Mac OSX 10.7 (64bit)
Now that I have successfully downloaded the files and imported the include folder and libraries that the folder contains, I get this error in the mysql.driver.h file I imported with the rest of the mysql files in the folder I just recently downloaded ...
#include <boost/scoped_ptr.hpp> // 'boost/scoped_ptr.hpp' file not found
So this is telling me that this templated header file was not found...
Do I need to download the boost library now? I did and imported BOOST into my project but it just gave me more errors...
Anyone ran into this same issue before or can help? :) I'd greatly appreciate it!
EDIT
I also read up on a forum in the mySQL webpage and saw that I would need to add linker flags and header/library search paths? I did this but nothing helped :(
Header Search Paths - /usr/local/mysql/include
Library Search Paths - /usr/local/mysql/lib
Other Linker Flags -lz -lm -lmysqlclient
I am a newbie and had a hard time on getting the C++ connector for mysql to work under Xcode. These are the steps necessary for it to work:
Download the MySQL C connector dmg package and install it. This package installs in /usr/local/mysql-connector.... Rename that folder to mysql. (in order to view the /usr folder in finder click on the GO menu and then on GO TO FOLDER once there type in /usr.)
Download the MySQL C++ connector binary. Uncompress it by double clicking the file. Once the file uncompresses, create a folder named mysqlCPP in /usr/local/ (you will now have a folder named mysql and mysqlCPP). Now copy the folders /include and /lib from the uncompressed C++ connector folder to the mysqlCPP folder you just created.
Download the BOOST library from boost.org. Uncompress the downloaded file and copy the boost directory to /usr/local/. (now you should have /mysql /mysqlCPP /boost directories here. There may be additional system folders DONT DELETE THEM.
Now on XCODE open your project and in Build Settings do the following:
Look for OTHER LINKER FLAGS, double click and add the following: -lz -lm -lmysqlcppconn-static (this builds a static reference and a bigger executable, if you wish to link dynamically you need type -lz -lm -lmysqlcppconn but you MUST COPY THE dynamic library into the EXECUTION directory of the file. Add to all options under OTHER LINKER FLAGS.
Look for HEADER SEARCH PATHS, double click and add the following /usr/local/mysqlCPP/include, /usr/local/mysql/include and /usr/local/ all should be added non-recursive. Don't forget to type in for all inputs Debug, Release, Any Architecture, etc.
Look for LIBRARY SEARCH PATHS, double click and add the following: /usr/local/mysql/lib and /usr/local/mysqlCPP/lib. Don't forget to type in for all inputs Debug, Release, Any Architecture, etc. Now you have both installed Connector-C and Connector-C++.
Look for C++ Standard Library and choose the libstdc++ (GNU C++ standard library)
Now you should be set and ready to compile C++ programs. If you wish to use the C connector instead then you should change the OTHER LINKER FLAGS to: -lm -lz -lmysqlclient. Just try with any of the samples on mysql.com and both connectors should work.
The documentation clearly states boost is required for building the MySQL connector from source
As of MySQL Connector/C++ 1.1.0, the Boost C++ libraries 1.34.0 or
newer must be installed. Boost is required only to build the
connector,
Once you have boost installed, just point to its installation location
Once Boost is installed, tell the build system where the Boost files
are by defining the BOOST_ROOT:STRING option. This can be done when
you invoke CMake. For example:
shell> cmake . -DBOOST_ROOT:STRING=/usr/local/boost_1_40_0

Customize compiler command in C++ IDEs

Is it possible to customize or specify the command for compiling in codeblocks or any of the other IDEs like NetBeans and Eclipse C++?
I'm asking this because I tried all those IDEs and no one can find the libraries even though it's there listed in the code assistance list (NetBeans 7.2). However, when I compile it in shell in Ubuntu 12.04 LTS, the libraries are located and the program can be compiled.
I use one of the commands:
gcc -lGL -lglut filetoCompile.cpp -o compiled.sh
g++ -lGL -lglut filetoCompile.cpp -o compiled.sh
to compile. I'm trying to use glut for open GL stuff and this is the library it can't find. I assume it may be the same for other third party libraries I may add in the future that's why I really need to get this fixed that's why my plan is to have the IDE use that command instead because it might work.
What's also odd is that my /usr/local/lib directory only contains 2 folders: python 2.7 and python 3.2. I'm not sure if this has an effect but is this normal? Shouldn't the libraries somehow be found here and the headers are in /usr/local/include?
The header files are located in /usr/include. Is this the correct location?
Generally speaking, yes this is possible.
In Eclipse CDT, you can set the directory where the header files are in this window:
Right click on project name; Properties; C/C++ General; Paths and symbols; Includes;
In window you must add all directories that contain header files for the functions/classes you want auto completion for. For example, my configuration is:
/usr/include/c++/4.4.5
/usr/local/include
/usr/lib/gcc/i486-linux-gnu/4.4.5/include
to find which folder contain an header file you can use the find or locate command, and set the IDE properly.