I have installed opencv with macports following the directions here: Compile OpenCV (2.3.1+) for OS X Lion / Mountain Lion with Xcode
I have also search and tried every other variation of this on stackexchange and google, but this seems to get me closest.
It seems to work for some things, but not for sample code that ships with 2.4.2. Note that I have added ALL opencv 2.4.2 dylibs Link Binary with Libraries.
For example, the following will compile and run:
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
int main ( int argc, char **argv )
{
cvNamedWindow( "My Window", 1 );
IplImage *img = cvCreateImage( cvSize( 640, 480 ), IPL_DEPTH_8U, 1 );
CvFont font;
double hScale = 1.0;
double vScale = 1.0;
int lineWidth = 1;
cvInitFont( &font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC,
hScale, vScale, 0, lineWidth );
cvPutText( img, "Hello World!", cvPoint( 200, 400 ), &font,
cvScalar( 255, 255, 0 ) );
cvShowImage( "My Window", img );
cvWaitKey();
return 0;
}
However, when I try to build any of the samples, such as the display_image.cpp, example, as follows, I get link errors.
-DOES NOT WORK-
#include <stdio.h>
#include <iostream>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/flann/miniflann.hpp"
using namespace cv; // all the new API is put into "cv" namespace. Export its content
using namespace std;
using namespace cv::flann;
static void help()
{
cout <<
"\nThis program shows how to use cv::Mat and IplImages converting back and forth.\n"
"It shows reading of images, converting to planes and merging back, color conversion\n"
"and also iterating through pixels.\n"
"Call:\n"
"./image [image-name Default: lena.jpg]\n" << endl;
}
int main(int argc, char *argv[])
{
help();
const char* imagename = argc > 1 ? argv[1] : "lena.jpg";
Mat img = imread(imagename); // the newer cvLoadImage alternative, MATLAB-style function
if(img.empty())
{
fprintf(stderr, "Can not load image %s\n", imagename);
return -1;
}
if( !img.data ) // check if the image has been loaded properly
return -1;
Mat img_yuv;
cvtColor(img, img_yuv, CV_BGR2YCrCb); // convert image to YUV color space. The output image will be created automatically
vector<Mat> planes; // Vector is template vector class, similar to STL's vector. It can store matrices too.
split(img_yuv, planes); // split the image into separate color planes
imshow("image with grain", img);
waitKey();
return 0;
}
I get the following errors:
Undefined symbols for architecture x86_64:
"cv::split(cv::Mat const&, std::__1::vector<cv::Mat, std::__1::allocator<cv::Mat> >&)", referenced from:
_main in main1.o
"cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
_main in main1.o
"cv::imshow(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, cv::_InputArray const&)", referenced from:
_main in main1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Any idea how to resolve this?
I had the same problem. A build setting default seems to be different in Xcode 4.5.
Under "Build Settings"--> Apple LLVM compiler 4.1 - Language >
C++ Standard Library:=
Change from libc++ (LLVM ...) to libstdc++ (GNU C++ ...).
It's very likely that OpenCV has not been compiled with C++11 settings, while the program is.
Set the build of your tool without C++11 switches (i.e. -std=c++11 -stdlib=libc++).
Try to manually add the directory where port puts all the dylibs (/opt/local/lib if I'm not getting wrong) in Build Settings->Library search path. This should fix the linking problem.
Related
I'm running into a linker error when I try to compile my code.
When I compile using g++ *.cpp -std=c++11 -o run I get the following error:
main.cpp:(.text+0x355): undefined reference to `Actions(mBoard&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::set<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)'
collect2: error: ld returned 1 exit status
I've tried compiling and linking all the files separately to no avail. Its not a member function so it's not a label issue. I've also tried to use g++ -std=c++11 main.cpp mAI.cpp -o run to make sure it is in fact compiling and linking both files but no luck.
What's really making me crazy is that it's not complaining about the Translate function that's declared and defined in the same way as the Actions function.
Any help is greatly appreciated.
mAI.hpp
#ifndef MAI_HPP
#define MAI_HPP
#include <iostream>
#include <set>
#include <tuple>
#include "mBoard.hpp"
using namespace std;
void Actions(mBoard const &state, string const playerColor, set<string> moveList);
tuple<int, int> Translate(string index);
#endif
mAI.cpp
#include "mAI.hpp"
std::set<char> blackPieces = {'r', 'n', 'b', 'q', 'k', 'p'};
std::set<char> whitePieces = {'R', 'N', 'B', 'Q', 'K', 'P'};
void Actions(mBoard const &state, string const playerColor, set<string> moveList)
{
//Do some stuff
}
tuple<int, int> Translate(string index)
{
//Do different stuff
}
main
#include "mAI.hpp"
#include "mBoard.hpp"
#include <set>
#include <tuple>
#include <string>
#include <iostream>
int main()
{
mBoard dasBoard;
set<string> moveList;
string color = "White";
cout<<"TEST - Translate: f6 to file, rank: ";
tuple<int, int> test;
test = Translate("f6");
cout << get<0>(test) << ", " << get<1>(test) << endl;
Actions(dasBoard, color, moveList);
return 0;
}
That sounds like a very old gcc compiler. That was a problem during abi change in earlier versions of gcc and installed libraries on the system.
using -D_GLIBCXX_USE_CXX11_ABI=0 and recompile all maybe help.
See also this documentation:
https://gcc.gnu.org/onlinedocs/libstdc%2B%2B/manual/using_dual_abi.html
BTW:
I can compile and link your code with gcc 7.3.1 ( fedora ) without any error. Some warnings of unused vars after cut and paste your code and only add an empty aBoard class. So I believe there is nothing wrong with your code.
From the documentation:
If you get linker errors about undefined references to symbols that involve types in the std::__cxx11 namespace or the tag [abi:cxx11] then it probably indicates that you are trying to link together object files that were compiled with different values for the _GLIBCXX_USE_CXX11_ABI macro. This commonly happens when linking to a third-party library that was compiled with an older version of GCC. If the third-party library cannot be rebuilt with the new ABI then you will need to recompile your code with the old ABI.
xcode 6.3 beta
I'm using libcrypto.a in my project.
My app can compile and run on my ipod touch5 (armv7).
But when I try to run my app on a iphone5 simulator, I'm getting the error:
"_closedir$UNIX2003", referenced from:
_OPENSSL_DIR_end in libcrypto.a(o_dir.o)
"_fputs$UNIX2003", referenced from:
_write_string in libcrypto.a(ui_openssl.o)
_read_string in libcrypto.a(ui_openssl.o)
"_opendir$INODE64$UNIX2003", referenced from:
_OPENSSL_DIR_read in libcrypto.a(o_dir.o)
"_readdir$INODE64", referenced from:
_OPENSSL_DIR_read in libcrypto.a(o_dir.o)
ld: symbol(s) not found for architecture i386
Then I checked what architectures the libcrypto.a I'm using support using the command:
lipo -info libcrypto.a
and get the result:
Architectures in the fat file: libcrypto.a are: i386 armv7 armv7s arm64
Any advice will be appreciated, thanks :)
create new m file anywhere.
and define all missing function here:
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <fnmatch.h>
FILE *fopen$UNIX2003( const char *filename, const char *mode )
{
return fopen(filename, mode);
}
int fputs$UNIX2003(const char *res1, FILE *res2){
return fputs(res1,res2);
}
int nanosleep$UNIX2003(int val){
return usleep(val);
}
char* strerror$UNIX2003(int errornum){
return strerror(errornum);
}
double strtod$UNIX2003(const char *nptr, char **endptr){
return strtod(nptr, endptr);
}
size_t fwrite$UNIX2003( const void *a, size_t b, size_t c, FILE *d )
{
return fwrite(a, b, c, d);
}
DIR * opendir$INODE64( char * dirName )
{
return opendir( dirName );
}
struct dirent * readdir$INODE64( DIR * dir )
{
return readdir( dir );
}
I'm trying to use CBLAS ATLAS. I'm a beginner. I have the following C code.
// tmp.cpp file
#include<stdio.h>
#include<stdlib.h>
#include<cblas.h>
int main(void){
float x[2] = {6,2};
float *y = (float *)malloc(2*sizeof(float));
const int n = 2;
const int incx = 1;
const int incy = 1;
float a = 5.4;
y[0] = 4.2; y[1] = 4.5;
cblas_saxpy(n,a,x,incx,y,incy);
free(y);
}
I installed the ATLAS library following the video http://youtu.be/DvLSr6zN0pU?t=6m5s and the next video "part3". However, I have no FORTRAN compiler, so I configure with
$../ATLAS/confiugre --nof77
The installation process took place as described in the video.
iMac:Desktop sotero$ ls /usr/local/atlas/include
atlas cblas.h clapack.h
iMac:Desktop sotero$ ls /usr/local/atlas/lib/
libatlas.a libcblas.a liblapack.a libptcblas.a
I tried compiling with this result
iMac:Desktop sotero$ c++ tmp.cpp
tmp.cpp:3:18: error: cblas.h: No such file or directory
tmp.cpp: In function ‘int main()’:
tmp.cpp:14: error: ‘cblas_saxpy’ was not declared in this scope
I read how to make the link to the website http://math-atlas.sourceforge.net/errata.html#LINK and I have the following to compile
iMac:Desktop sotero$ c++ tmp.cpp -L/usr/local/atlas/lib/ -lcblas -latlas -I/usr/local/atlas/include/
Undefined symbols:
"cblas_saxpy(int, float, float const*, int, float*, int)", referenced from:
_main in ccPop12J.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
I do not know how I should compile. I just need to level one BLAS. I've been looking to compile if you do link to the ATLAS library, but I'm lost.
How I can fix it?
I am using OpenCV 2.4.6 in Visual Studio 2012 and I have testing one of the sample programs , name matcher_simple.cpp -- which matches two sample images , image1 and image2.
#include <stdio.h>
#include "opencv2/core/core.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include "opencv2/opencv.hpp"
using namespace cv;
static void help()
{
printf("\nThis program demonstrates using features2d detector, descriptor extractor and simple matcher\n"
"Using the SURF desriptor:\n"
"\n"
"Usage:\n matcher_simple <image1> <image2>\n");
}
int main(int argc, char** argv)
{
if(argc != 3)
{
help();
return -1;
}
//cv::initModule_nonfree();
Mat img1 = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
Mat img2 = imread(argv[2], CV_LOAD_IMAGE_GRAYSCALE);
if(img1.empty() || img2.empty())
{
printf("Can't read one of the images\n");
return -1;
}
// detecting keypoints
SurfFeatureDetector detector(400);
vector<KeyPoint> keypoints1, keypoints2;
detector.detect(img1, keypoints1);
detector.detect(img2, keypoints2);
// computing descriptors
SurfDescriptorExtractor extractor;
Mat descriptors1, descriptors2;
extractor.compute(img1, keypoints1, descriptors1);
extractor.compute(img2, keypoints2, descriptors2);
// matching descriptors
BFMatcher matcher(NORM_L2);
vector<DMatch> matches;
matcher.match(descriptors1, descriptors2, matches);
// drawing the results
namedWindow("matches", 1);
Mat img_matches;
drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
imshow("matches", img_matches);
waitKey(0);
return 0;
}
On compiling I get this error :
1>------ Build started: Project: opencvreinstated, Configuration: Release x64 ------
1>matcher_simple.obj : error LNK2001: unresolved external symbol "public: __cdecl cv::SURF::SURF(void)" (??0SURF#cv##QEAA#XZ)
1>matcher_simple.obj : error LNK2001: unresolved external symbol "public: __cdecl cv::SURF::SURF(double,int,int,bool,bool)" (??0SURF#cv##QEAA#NHH_N0#Z)
1>C:\Users\motiur\documents\visual studio 2012\Projects\opencvreinstated\x64\Release\opencvreinstated.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I have testing this in release mode 64 bit , I also have successfully ran simple other opencv samples , for example streaming live video . I did not have these sort of issues there . Help is appreciated . Thanks.
You have to link in the nonfree module, since this is where the Surf features are implemented.
Go to project Properties -> Linker -> Input, and add smth like opencv_nonfree246d.dll to Additional Dependencies field.
For details, please, see http://docs.opencv.org/doc/tutorials/introduction/windows_visual_studio_Opencv/windows_visual_studio_Opencv.html#the-local-method
I use Xcode 4.5.2 and I wonna use Boost, but I got some problems.
In Build Setting, if I choose libc++ (LLVM C++ standard library with C++11 support), I will get the error messgae "Apple Mach-O Linker (ld) Error".
Just like this:
Undefined symbols for architecture x86_64:
"boost::filesystem::path_traits::dispatch(boost::filesystem::directory_entry const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, std::__1::codecvt<wchar_t, char, __mbstate_t> const&)"
referenced from:
boost::filesystem::path::path<boost::filesystem::directory_entry>(boost::filesystem::directory_entry const&, boost::enable_if<boost::filesystem::path_traits::is_pathable<boost::decay<boost::filesystem::directory_entry>::type>, void>::type*) in test1 - inverted index.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 include these two headers:
#include <boost/filesystem.hpp>
#include <boost/operators.hpp>
I also add these in Build Phases:
libboost_filesystem-mt.dylib
libboost_filesystem-mt.a
libboost_system-mt.dylib
libboost_system-mt.a
Code is here:
#include <boost/filesystem.hpp>
#include <boost/operators.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <set>
using namespace boost::filesystem;
std::map<std::string, std::set<int>> invertedIndex;
std::map<std::string, int> number;
bool check_char(const char in)
{
if((in>='A' && in<='Z') || (in>='a' && in<='z'))
return true;
else
return false;
}
int main() {
int con = 0;
std::string word;
path root("/Users/tomhu/Desktop/pro/data/");
std::string rootDirectory = root.native();
recursive_directory_iterator iter(root);
recursive_directory_iterator end;
for (; iter != end; ++iter)
{
if(is_regular_file(*iter))
{
std::string filename;
std::string directory(rootDirectory);
filename = iter->path().filename().native();
directory.append(filename);
std::ifstream fileIn(directory.c_str());
number[filename] = con;
if(!fileIn)
{
std::cerr << "File doesn't exist!" << std::endl;
exit(1);
}
while(fileIn>>word)
{
long po = 0;
if(!check_char(*(word.end()-1)))
word.pop_back();
transform(word.begin(),word.end(),word.begin(),::tolower);
if(word=="i")
word="I";
invertedIndex[word].insert(con);
}
}
}
std::cout << con << std::endl;
return 0;
}
!!!!!!!!
If I choose libstdc++ (GUN C++ standard library) in "Build Setting", I won't get any error message about Boost, but I can't use anything in C++11 Standard.
How to solve these problems?
See Why can't clang with libc++ in c++0x mode link this boost::program_options example? - Howard gave an excellent answer what's going wrong there.
And here is how you can recompile Boost for clang+libc++: How to compile/link Boost with clang++/libc++?