Why cant I compile this OpenCV 'hello world' using g++? - c++

I have installed OpenCV via homebrew, for my Mac OSX.
The problem I am having is that I am unable to use the imread openCV command at all. When I do, I get an error. Here is a breakdown of what I have done.
My simple code is:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat imageMe = imread("frog.jpg", 0);
return 0;
}
To compile, I type this into the command prompt:
g++ opencvEntry.cpp -o opencvEntry `pkg-config --cflags --libs opencv` && ./opencvEntry
Upon doing that, the error message I get is:
Undefined symbols for architecture x86_64:
"cv::imread(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)", referenced from:
_main in ccvT5f6R.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
So... I am not sure where/what is going on here. If I simply uncomment the imread line the file compiles just fine. However when uncommented, it does not recognize the command.
What is going on here?

I have the same issue, after searching, this is a solution which works for me, hope it can help you as well.
Compiling OpenCV code on a 64-bit mac
need LDFLAG of "-lopencv_imgproc".

Related

Using armadillo in atom with script

I there a way to compile a c++ file using script in atom that uses armadillo? I couldn't find anything related to it.
I already installed armadillo and tried compiling some basic code using script in atom:
#include <iostream>
#include <armadillo>
int main(int argc, const char **argv) {
arma::arma_rng::set_seed_random();
arma::Mat<double> A = arma::randu(4,4);
std::cout << "A:\n" << A << "\n";
return 0;
}
This is the error I got:
Undefined symbols for architecture x86_64:
"thread-local wrapper routine for arma::arma_rng_cxx11_instance", referenced from:
arma::arma_rng::set_seed(unsigned long long) in test1-83e853.o
arma::arma_rng::randu<double>::fill(double*, unsigned long long) in test1-83e853.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
If I read the source code for this atom-script thing correctly, it just hardcodes -Wall -include stdio.h as compilation options. You need to add -larmadillo to link it to the Armadillo library.
In short: you cannot use atom-script in combination with custom libraries, unless you edit the extension to hardcode the libraries in your compilation commandline.

libuvc program does not compile

I'm trying to use libuvc in one of my C/C++ projects. I succesfully compiled and installed both libusb and libuvc from source and when I attempt to compile the following code using gcc:
#include "libuvc/libuvc.h"
int main (int argc, const char **argv) {
uvc_init(NULL, NULL);
}
I get the following error:
Undefined symbols for architecture x86_64:
"_uvc_init", referenced from:
_main in main-de2855.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'm on MacOS High Sierra 10.13.1 x64.
I used the command:
gcc main.c -luvc -o main
and it worked! Adding the -luvc flag solved my problem, but I don't understand why I need to use it. I've never needed flags before when working with other C/C++ dependencies.

ld: symbol(s) not found for architecture

First of all I want to say I realise this has been asked a dozen times, and I've read countless solutions without any luck so therefore I'm asking again.
I'm using OS X Mavericks 10.9.5, writing in Sublime 3 and compiling using terminal with g++
I'm trying to compile this simple file (test.cpp)
#include <iostream>
#include <SDL2/SDL.h>
int main () {
if(SDL_Init(SDL_INIT_VIDEO)) {
std::cout << "I made it this far" << std::endl;
}
return 0;
}
Compiler line :
g++ test.cpp
This returns the error :
Undefined symbols for architecture x86_64:
"_SDL_Init", referenced from:
_main in test-ebbae4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
So, I've tried adding a lot of different flags, -m32 only changes the result to throw Undefined symbols for architecture i386: and ld: symbol(s) not found for architecture i386
I've tried different -arch variations, but I can't seem to get it to work. also played around with -l -I flags but I'm not sure I know what they do/how they could help..
Does anyone have a clue what to do next?
EDIT : Some additional information. I've tried using the .framework for SDL2, that didn't change anything, currently I'm using SDL2 installed through compilation of the source. Headers located in usr/local/SDL2
g++ test.cpp
You should specify the SDL library, too:
g++ test.cpp -lsdl2
You might need to include a path if its not well known to the compiler driver:
g++ test.cpp -L/path/to/the/library -lsdl2

OpenCv Compiling and Linking error

Dear Friend please forgive me if the question i am about to ask sound stupid or very basic. I am trying to use the opencv for three days now and i am finding it very hard to compile the code in both Qt and XCODE, yesterday i was luck enough after gong through many tutorial was able to get one of the most basic codes working. The problem however is in some books i find example of codes written in this way:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
int main( int argc, char** argv ) {
IplImage* img = cvLoadImage( "/Users/mughery/Desktop/1_s.jpg" );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
}
The above code works very fine, it has now problem what so ever, however when i test the code bellow i get a plenty of error. i believe they are both C++ code and they are supposedly doing the same thing.
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
int main()
{
//read an image
cv::Mat image = cv::imread("/Users/mughery/Desktop/1_s.jpg");
//create image window named "My Image"
cv::namedWindow("My Image");
cv::imshow("My image", image);
cv::waitKey(5000);
return 1;
}
When i run the second code it says library not found, the books are using the same library for both so what’s wrong can some one help please. The error i recive from the second code is
Ld /Users/mughery/Library/Developer/Xcode/DerivedData/Open-dvjwxosfuaihuabwnlxxuydfmyou/Build/Products/Debug/Open normal x86_64
cd /Users/mughery/Documents/ImageProgramtest/Open
export MACOSX_DEPLOYMENT_TARGET=10.9
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang++ -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -L/Users/mughery/Library/Developer/Xcode/DerivedData/Open-dvjwxosfuaihuabwnlxxuydfmyou/Build/Products/Debug -L/usr/local/lib -F/Users/mughery/Library/Developer/Xcode/DerivedData/Open-dvjwxosfuaihuabwnlxxuydfmyou/Build/Products/Debug -filelist /Users/mughery/Library/Developer/Xcode/DerivedData/Open-dvjwxosfuaihuabwnlxxuydfmyou/Build/Intermediates/Open.build/Debug/Open.build/Objects-normal/x86_64/Open.LinkFileList -mmacosx-version-min=10.9 -stdlib=libstdc++ -lopencv_highgui.2.4.9 -lopencv_core.2.4.9 -Xlinker -dependency_info -Xlinker /Users/mughery/Library/Developer/Xcode/DerivedData/Open-dvjwxosfuaihuabwnlxxuydfmyou/Build/Intermediates/Open.build/Debug/Open.build/Objects-normal/x86_64/Open_dependency_info.dat -o /Users/mughery/Library/Developer/Xcode/DerivedData/Open-dvjwxosfuaihuabwnlxxuydfmyou/Build/Products/Debug/Open
Undefined symbols for architecture x86_64:
"cv::namedWindow(std::string const&, int)", referenced from:
_main in main.o
"cv::imread(std::string const&, int)", referenced from:
_main in main.o
"cv::imshow(std::string const&, cv::_InputArray const&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Both conventions are valid to use OpenCV functions in C++ code. The first code snippet uses C functions while the second one uses C++ functions. I reproduced your bug and it happens when you have changed the C++ Standard Library from the default of libc++ (LLVM C++ standard library) to libstdc++ (GNU C++ standard library) under the Build Settings of your Project.
If you use libc++ then both conventions work fine. For some reason, libstdc++ does not recognize the newer C++ functions.
The first code is uses c library while the below code uses c++ library...
1- Make sure you are linking the proper opencv libraries for both
2 - From the error code, it seems to me that the error is coming from mixing 32/64 bit libraries, try to match them.

GDAL API in C/C++

I'm trying to compile the following program (using GDAL API in C++):
main.cpp:
#include "gdal_priv.h"
#include "cpl_conv.h"
int main(int argc, char *argv[])
{
GDALAllRegister();
GDALDataset* data = (GDALDataset*) GDALOpen( "heightmap.tiff", GA_ReadOnly );
}
the way I build it on a Mac :
clang main.cpp -I/Library/Frameworks/GDAL.framework/Versions/1.10/unix/include
and got this:
clang main.cpp -I/Library/Frameworks/GDAL.framework/Versions/1.10/unix/include/
Undefined symbols for architecture x86_64:
"_GDALOpenShared", referenced from:
_main in test_gdal-QIB6MK.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
What's wrong with this very basic case?
Try to build your project on a Mac using bash file:
#!/usr/bin/env bash
## Path to you installed GDAL
GDAL_PATH=/usr/local/Cellar/gdal2/2.2.0
INCLUDE_PATH=${GDAL_PATH}/include
LIBS_PATH=${GDAL_PATH}/lib
GDAL_LIB=-lgdal
g++ -I${INCLUDE_PATH} -L${LIBS_PATH} ${GDAL_LIB} main.cpp -o main