Compiling OpenCV code on a 64-bit mac - c++

I recently inherited some OpenCV code. I installed openCV on my mac, built in in XCode, and then compiled and successfully ran my first openCV "hello world"-ish program.
Now I'm trying to run the code I was given, but I get errors that lead me to believe it's an issue with the original code being run on a 32-bit Windows system and mine being on a 64-bit Mac.
When I run the Makefile by entering "make"
CC = g++
CFLAGS =
LDFLAGS = -I/usr/local/include/opencv -lm -lopencv_core -lopencv_highgui -lopencv_video
ALL = vision
all: $(ALL)
vision: vision.o
$(CC) $(LDFLAGS) -o $# $^
vision.o: vision.cpp
$(CC) $(LDFLAGS) -c $<
.PHONY: clean
clean:
rm -rf *.o core* $(ALL)
I get the following output…
g++ -I/usr/local/include/opencv -lm -lopencv_core -lopencv_highgui -lopencv_video -o vision vision.o
Undefined symbols for architecture x86_64:
"cv::equalizeHist(cv::Mat const&, cv::Mat&)", referenced from:
_main in vision.o
"cv::threshold(cv::Mat const&, cv::Mat&, double, double, int)", referenced from:
_main in vision.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [vision] Error 1
I'm confused; does this mean my install of OpenCV is wrong, the code (those methods specifically) needs to be changed, or something else entirely?
Note: When I comment out the problem methods from the vision.cpp code, everything compiles just fine.

Add opencv_imgprocto your LDFLAGS:
LDFLAGS = -I/usr/local/include/opencv -lm -lopencv_core -lopencv_highgui -lopencv_video -lopencv_imgproc

Here is a working example:
CXX = g++
SOURCES = aaa.cpp bbb.cpp
OBJS = $(SOURCES:.cpp=.o)
CXXFLAGS = -I. -I/opt/local/include \
-std=c++11 -stdlib=libc++ \
-g3 -Wall -O0
# -std=c++0x -arch x86_64 -stdlib=libc++ \
LDFLAGS = -L/opt/local/lib -L/usr/lib $(pkg-config --libs --cflags opencv) -lm -ljpeg
LDFLAGS = -L/opt/local/lib -L/usr/lib -I/opt/local/include/opencv -I/opt/local/include -L/opt/local/lib -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab -lm -ljpeg
.o:
$(CXX) $(CXXFLAGS) -o $# -c $^
all: $(OBJS)
$(CXX) $(CXXFLAGS) $(LDFLAGS) -o out $(OBJS)
clean:
rm -rf *.o

You can also have the computer guess you the libraries automatically:
CFLAGS = `pkg-config --cflags opencv`
LDFLAGS = `pkg-config --libs opencv` -lm

Related

Makefile to compile a class with main opencv

I hope you're all fine.
I'm learning opencv (c++) currently and I'm trying to compile a "main.cpp" with a class called "Algorithms"(.h and .cpp) and for some reason I get an error like this one when trying :
g++ `pkg-config --cflags opencv4` `pkg-config --libs opencv4` src/Algorithms.cpp -o obj/Algorihtms.o -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs
/usr/bin/ld: /usr/lib/gcc/x86_64-linux-gnu/9/../../../x86_64-linux-gnu/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
collect2: error: ld returned 1 exit status
make: *** [makefile:15: obj/Algorithms.o] Error 1
Here is my makefile:
CFLAGS = `pkg-config --cflags opencv4`
LIBS = `pkg-config --libs opencv4`
B = bin
O = obj
S = src
FLAGS = -c -Wall
all: $(O) $(B) $(B)/main
$(B)/main: $(O)/Algorithms.o
g++ $(FLAGS) -ggdb $(S)/main.cpp -o $(O)/main.o
$(O)/Algorithms.o:
g++ $(CFLAGS) $(LIBS) $(S)/Algorithms.cpp -o $(O)/Algorihtms.o -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs
$(O):
mkdir $(O)
$(B):
mkdir $(B)
clean: $(O) $(B)
rm -rf $(O)
rm -rf $(B)
(Opencv is installed and I know that if I compile "main.cpp" alone it works, but I can't get "main.cpp" and the class "Algorithm" to compile simultaneously.)
Thanks a lot.
If you do not want to link to an executable, you need to make GCC aware. The default is to compile and link. Use option -c:
$(O)/Algorithms.o:
g++ -c $(CFLAGS) $(LIBS) $(S)/Algorithms.cpp -o $(O)/Algorihtms.o -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs
From the manpage:
When you invoke GCC, it normally does preprocessing, compilation, assembly and linking. The
"overall options" allow you to stop this process at an intermediate stage. For example, the -c
option says not to run the linker. Then the output consists of object files output by the
assembler.
However in your case, as mentioned in my comment, the question is why you need an intermediate object file at all.
Answering to my own question.
I don't know why I didn't figure that out immediately in my makefile.
Whatever, here is a working makefile that does the job :
CFLAGS = `pkg-config --cflags opencv4`
LIBS = `pkg-config --libs opencv4`
B = bin
O = obj
S = src
FLAGS = -c -Wall
all: $(O) $(B) $(B)/main
$(B)/main: $(O)/Algorithms.o
g++ $(CFLAGS) $(LIBS) $(S)/main.cpp -o $(B)/main $(O)/*.o -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_imgcodecs
$(O)/Algorithms.o:
g++ $(FLAGS) $(CFLAGS) $(LIBS) $(S)/Algorithms.cpp -o $(O)/Algorithms.o
$(O):
mkdir $(O)
$(B):
mkdir $(B)
clean: $(O) $(B)
rm -rf $(O)
rm -rf $(B)
I'm sure this makefile is not the most efficient and it can be improved in many ways.

QT application will not compile when OpenCV 4.2 is used

I'm trying to build an application using OpenCV and QT, working out of QT creator. I've used this combination in the past from Visual Studio without any problems, but I have started using a Linux workstation as my main dev box, so have switched to QTCreator as an IDE.
The application receives data over the network and displays some of it as an image; all the other stuff works ok and has been tested; it's just showing the image that causes the problem.
The build error occurs when I call imshow:
Mat image = Mat(1944, 2592, CV_16U, (void*)images[imgCount].data());
imshow("disp", image);
And the build output looks like this (I have shortened some of the file paths):
g++ -c -pipe -g -std=gnu++11 -Wall -Wextra -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_NETWORK_LIB -DQT_CORE_LIB -I../RasPiRawImageReceiverTest -I. -isystem /usr/local/include/opencv4 -isystem /usr/include/qt -isystem /usr/include/qt/QtNetwork -isystem /usr/include/qt/QtCore -I. -I/usr/lib/qt/mkspecs/linux-g++ -o main.o ../RasPiRawImageReceiverTest/main.cpp
g++ -c -pipe -g -std=gnu++11 -Wall -Wextra -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_NETWORK_LIB -DQT_CORE_LIB -I../RasPiRawImageReceiverTest -I. -isystem /usr/local/include/opencv4 -isystem /usr/include/qt -isystem /usr/include/qt/QtNetwork -isystem /usr/include/qt/QtCore -I. -I/usr/lib/qt/mkspecs/linux-g++ -o receiver.o ../RasPiRawImageReceiverTest/receiver.cpp
g++ -pipe -g -std=gnu++11 -Wall -Wextra -dM -E -o moc_predefs.h /usr/lib/qt/mkspecs/features/data/dummy.cpp
/usr/bin/moc -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_NETWORK_LIB -DQT_CORE_LIB --include moc_predefs.h -I/usr/lib/qt/mkspecs/linux-g++ -I/QtCreatorProjects/RasPiRawImageReceiverTest -I/usr/local/include/opencv4 -I/usr/include/qt -I/usr/include/qt/QtNetwork -I/usr/include/qt/QtCore -I. -I/usr/include/c++/9.2.1 -I/usr/include/c++/9.2.1/x86_64-pc-linux-gnu -I/usr/include/c++/9.2.1/backward -I/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.1/include -I/usr/local/include -I/usr/lib/gcc/x86_64-pc-linux-gnu/9.2.1/include-fixed -I/usr/include ../receiver.h -o moc_receiver.cpp
g++ -c -pipe -g -std=gnu++11 -Wall -Wextra -D_REENTRANT -fPIC -DQT_DEPRECATED_WARNINGS -DQT_QML_DEBUG -DQT_NETWORK_LIB -DQT_CORE_LIB -I../RasPiRawImageReceiverTest -I. -isystem /usr/local/include/opencv4 -isystem /usr/include/qt -isystem /usr/include/qt/QtNetwork -isystem /usr/include/qt/QtCore -I. -I/usr/lib/qt/mkspecs/linux-g++ -o moc_receiver.o moc_receiver.cpp
main.cpp: In function ‘int main(int, char**)’:
main.cpp:9:15: warning: unused variable ‘r’ [-Wunused-variable]
9 | Receiver* r = new Receiver();
| ^
g++ -o RasPiRawImageReceiverTest main.o receiver.o moc_receiver.o -L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_videoio -lopencv_imgcodecs -lopencv_flann /usr/lib/libQt5Network.so /usr/lib/libQt5Core.so -lpthread
/usr/bin/ld: receiver.o: in function `Receiver::dataReady()':
receiver.cpp:52: undefined reference to `imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
collect2: error: ld returned 1 exit status
make: *** [Makefile:228: RasPiRawImageReceiverTest] Error 1
12:03:02: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project RasPiRawImageReceiverTest (kit: Desktop)
When executing step "Make"
OpenCV has been compiled from the 4.2.0 stable source files downloadable from the OpenCV website and using the default CMake config. QT 5.14.1 is installed from the Manjaro repos.
From what I can tell this a C++ standard mismatch, but as far as I can tell both OpenCV and the application are being compiled with C++11. The QT .pro file has the CONFIG += c++11 line in. Other OpenCV functions work; it just seems to be this from what I have tested. I know there are other warnings in the build, but these are not a problem at the moment.
Edit: .pro looks like this:
QT -= gui
QT += network
CONFIG += c++11 console
CONFIG -= app_bundle
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += \
main.cpp \
receiver.cpp
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
HEADERS += \
receiver.h
INCLUDEPATH += \
/usr/local/include/opencv4
/usr/local/include
LIBS += \
-L/usr/local/lib -lopencv_core -lopencv_imgproc -lopencv_highgui -lopencv_ml -lopencv_video -lopencv_features2d -lopencv_calib3d -lopencv_objdetect -lopencv_videoio -lopencv_imgcodecs -lopencv_flann

cannot find -lopencv_contrib cannot find -lopencv_legacy

I have installed opencv it compiled 100%, i have run a command:
pkg-config --cflags --libs opencv
It's output is:-I/usr/include/opencv -I/usr/include/opencv4 -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -lopencv_core -lopencv_ml -lopencv_features2d -lopencv_objdetect -lopencv_flann -lopencv_video -lopencv_highgui
But when i try to compile a c++ code it gives cannot find error,
I don't know what to do.
Thanks..
~/cpp_test$
g++ main.cpp -o output `pkg-config --cflags --libs opencv`
/usr/bin/x86_64-linux-gnu-ld: cannot find -lopencv_contrib
/usr/bin/x86_64-linux-gnu-ld: cannot find -lopencv_legacy
collect2: error: ld returned 1 exit status
If you installed OpenCV 4, I'm pretty confident that you met the same problem as mine.
The solution is actually pretty simple, you just need to run
g++ main.cpp -o output `pkg-config --cflags --libs opencv4`
instead of
g++ main.cpp -o output `pkg-config --cflags --libs opencv`
after installation of Opencv, symbolic linking has to be done to make a link for the library to a known lib location.
Try the command on terminal sudo ldconfig, to dynamically link the library.
And then compile g++ main.cpp -o output $(pkg-config --cflags --libs opencv).

Makefile for linking OpenCV and Existing Library (Without using cmake)

Currently, i am been working on how to link an existing library with OpenCV using Makefile. I am still new to Makefile. I had googled on the Internet but mostly the answer is on CMake. Even there is an answer, the output of the result contain errors. Please have a look on my Makefile, am i doing anything wrong?
Makefile
###############################################################
#
# Purpose: Makefile for "head_tracking"
# Author.: robotis
# Version: 0.1
# License: GPL
#
###############################################################
TARGET = head_tracking
INCLUDE_DIRS = -I../../../include -I../../../../Framework/include
CXX = g++
CXXFLAGS += -O2 -DLINUX -Wall $(INCLUDE_DIRS)
#CXXFLAGS += -O2 -DDEBUG -DLINUX -Wall $(INCLUDE_DIRS)
LFLAGS += -lpthread -ljpeg -lrt
CPPFLAGS = $(shell pkg-config --cflags opencv2) #The one i added
LDLIBS = $(shell pkg-config --libs opencv2) #The one i addded
OBJECTS = main.o
all: $(TARGET)
clean:
rm -f *.a *.o $(TARGET) core *~ *.so *.lo
libclean:
make -C ../../../build clean
distclean: clean libclean
darwin.a:
make -C ../../../build
$(TARGET): darwin.a $(OBJECTS)
$(CXX) $(CFLAGS) $(OBJECTS) ../../../lib/darwin.a -o $(TARGET) $(LFLAGS)
chmod 755 $(TARGET)
# useful to make a backup "make tgz"
tgz: clean
mkdir -p backups
tar czvf ./backups/head_tracking_`date +"%Y_%m_%d_%H.%M.%S"`.tgz --exclude backups *
Error Image: Undefined Refrenced
Okay i finally solve my linking error. I can now compile openCV with my Robotis-Op library. This is the new Makefile.
###############################################################
#
# Purpose: Makefile for "head_tracking"
# Author.: robotis
# Version: 0.1
# License: GPL
#
###############################################################
TARGET = head_tracking
INCLUDE_DIRS = -I../../../include -I../../../../Framework/include -I/usr/local/include/opencv2
CXX = g++
CXXFLAGS += -O2 -DLINUX -Wall $(INCLUDE_DIRS) `pkg-config --cflags opencv`
#CXXFLAGS += -O2 -DDEBUG -DLINUX -Wall $(INCLUDE_DIRS)
LFLAGS += -lpthread -ljpeg -lrt
LDFLAGS = `pkg-config --libs opencv`
#CPPFLAGS = $(shell pkg-config --cflags opencv2)
#LDLIBS = $(shell pkg-config --libs opencv2)
OBJECTS = main.o
all: $(TARGET)
clean:
rm -f *.a *.o $(TARGET) core *~ *.so *.lo
libclean:
make -C ../../../build clean
distclean: clean libclean
darwin.a:
make -C ../../../build
$(TARGET): darwin.a $(OBJECTS)
$(CXX) $(CFLAGS) $(OBJECTS) ../../../lib/darwin.a -o $(TARGET) $(LFLAGS) $(LDFLAGS)
chmod 755 $(TARGET)
# useful to make a backup "make tgz"
tgz: clean
mkdir -p backups
tar czvf ./backups/head_tracking_`date +"%Y_%m_%d_%H.%M.%S"`.tgz --exclude back
ups *
You are having the linking errors,
You need to link with the following flags:
-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab
OpenCV Undefined symbols for architecture x86_64: error

Makefile does not work on mac os x Mavericks

I have a project, which uses opencv library. Today I have upgraded my Mac OS X Lion to Mavericks and now I am wondering why it does not compile anymore because of the following error:
c++ -O2 -g -Wall -fmessage-length=0 -c -o Hello.o Hello.cpp
Hello.cpp:2:10: fatal error: 'opencv/cv.h' file not found
#include "opencv/cv.h"
^
1 error generated.
make: *** [Hello.o] Error 1
Here's the code of Hello.cpp:
#include <iostream>
#include "opencv/cv.h"
using namespace std;
int main(){
return 0;
}
Here's my makefile:
CXXFLAGS = -O2 -g -Wall -fmessage-length=0
OBJS = Hello.o
LIBS = -lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video -lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_gpu
INCPATH = -I/usr/local/Cellar/opencv/2.4.6.1/include
LIBPATH = -L/usr/local/Cellar/opencv/2.4.6.1/lib
TARGET = Hello
$(TARGET): $(OBJS)
g++ $(INCPATH) $(LIBPATH) -o $(TARGET) $(OBJS) $(LIBS)
all: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)
I have to say that I can compile my project using terminal by typing:
g++ `pkg-config --cflags opencv` Hello.cpp -o Hello
and to be sure, this is what I get by running pkg-config --cflags opencv on terminal:
-I/usr/local/Cellar/opencv/2.4.6.1/include/opencv
I doubt this makefile ever worked, unless somehow the new version of MacOSX has moved those headers out of the standard location into a different location.
You are using the default built-in rules, as Paul R says, but you are not using the default make variables that go with those rules. I would write my makefile like this:
CXX = g++
CXXFLAGS = -O2 -g -Wall -fmessage-length=0
CPPFLAGS = -I/usr/local/Cellar/opencv/2.4.6.1/include
OBJS = Hello.o
LDFLAGS = -L/usr/local/Cellar/opencv/2.4.6.1/lib
LDLIBS = -lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video \
-lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect \
-lopencv_contrib -lopencv_legacy -lopencv_gpu
TARGET = Hello
.PHONY: all
all: $(TARGET)
$(TARGET): $(OBJS)
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $#
.PHONY: clean
clean:
rm -f $(OBJS) $(TARGET)
You makefile does not have an explicit rule for building Hello.o so it is using a default rule. There are various ways to handle this but I would just change it to:
CXXFLAGS = -O2 -g -Wall -fmessage-length=0
SRCS = Hello.cpp
LIBS = -lopencv_core -lopencv_imgproc -lopencv_calib3d -lopencv_video -lopencv_features2d -lopencv_ml -lopencv_highgui -lopencv_objdetect -lopencv_contrib -lopencv_legacy -lopencv_gpu
INCPATH = -I/usr/local/Cellar/opencv/2.4.6.1/include
LIBPATH = -L/usr/local/Cellar/opencv/2.4.6.1/lib
TARGET = Hello
$(TARGET): $(SRCS)
g++ $(CXXFLAGS) $(INCPATH) $(LIBPATH) -o $(TARGET) $(SRCS) $(LIBS)
all: $(TARGET)
clean:
rm -f $(TARGET)
Dear i got the same error, please have a look here:
C++ linking error after upgrading to Mac OS X 10.9 / Xcode 5.0.1
very quick solution: add -stdlib=libstdc++ to the linking command.