I'm trying to use OpenCV train_HOG utility.
When I try to compile it with
g++ -ggdb `pkg-config --cflags --libs opencv` train_HOG.cpp -o train_HOG
I get a ton of different errors.
train_HOG.cpp:11:21: error: ‘ml’ is not a namespace-name
using namespace cv::ml;
^
train_HOG.cpp:11:23: error: expected namespace-name before ‘;’ token
using namespace cv::ml;
And a few pages more.
Can anyone give me instructions how to use it properly or point me to a working solution?
Related
I have been programming using Gtkmm for a while now, using C++11 features without problems. Today, I added a line of code using the C++14 feature std::make_unique and got a compiler error. At first, I thought I had an issue with my build configuration but after some testing, I narrowed it down to Gtkmm. Here is code that builds fine on my system:
Build command:
g++ -std=c++14 main.cpp
Code:
#include <memory>
int main()
{
std::unique_ptr<int> intPtr;
intPtr = std::make_unique<int>(3);
return 0;
}
If I switch to this build command:
g++ -std=c++14 main.cpp `pkg-config gtkmm-3.0 --cflags --libs`
The code no longer builds. I get the following errors:
main.cpp: In function ‘int main()’:
main.cpp:7:14: error: ‘make_unique’ is not a member of ‘std’
intPtr = std::make_unique<int>(3);
^
main.cpp:7:31: error: expected primary-expression before ‘int’
intPtr = std::make_unique<int>(3);
What's the problem with Gtkmm? For your information, I am using g++ v 5.4.0 and gtkmm 3.0.
EDIT: It seems this is not C++14 related. I tried building with other C++14 features, like [[DEPRECATED]] and it worked fine. Maybe only the standard library... I also tried switching to g++ 7 and got the same errors.
You are probably running into a problem with the C++ standard that is specified by the --cflags argument in pkg-config gtkmm-3.0 --cflags --lib. If -std=c++11 or something older is the result of providing the --cflags option, then it will override any earlier specifications. You can probably fix the problem just by placing your desired specification at the end:
g++ main.cpp `pkg-config gtkmm-3.0 --cflags --libs` -std=c++14
I tried to run the following example sketch for the SimlpeBlobTracker:
https://github.com/spmallick/learnopencv/blob/master/BlobDetector/blob.cpp
However, when I try to run the program,
./track
it breaks and returns:
OpenCV Error: The function/feature is not implemented () in
detectAndCompute, file
/home/sixtimesseven/Desktop/opencv/opencv/modules/features2d/src/feature2d.cpp,
line 144
terminate called after throwing an instance of 'cv::Exception'
what(): /home/sixtimesseven/Desktop/opencv/opencv/modules/features2d/src/feature2d.cpp:144:
error: (-213) in function detectAndCompute
I am compiling with a makefile:
CFLAGS = `pkg-config --cflags opencv`
LIBS = `pkg-config --libs opencv`
track : track.cpp
g++ $(CFLAGS) -o track track.cpp $(LIBS)
And I am running Ubuntu 16 and OpenCV 2.4.
I am not an expert on this topic, but I did come across this, which suggests that it might be something related to how you're declaring a certain object.
I'm trying to setup a trivial OpenCV example following instructions from here. But when I try to run my example using
$ g++ -ggdb `pkg-config --cflags opencv` -o `basename opencvtest.cpp .cpp` opencvtest.cpp `pkg-config --libs opencv`
$ ./opencvtest`
I get the following error
g++ error missing argument to ‘-l’
I would appreciate any insight on how I could solve this.
I am trying to compile my code using libboost library, by putting #include <boost/python.hpp> in my C++ code. Could somebody please help me the right command to run this, especially to include and link the library. I'm pretty basic in this.
The command used(but not working):
g++ try.cpp -L /usr/lib/libboost_python.so -o try
EDIT:
The tested code:
#include <boost/python.hpp>
#include <iostream>
int main()
{
std::cout << "Yes, it works :-)" << std::endl;
return 0;
}
Error messages:
from try.cpp:1:
/usr/include/boost/python/enum.hpp:31: error: expected ‘;’ before ‘*’ token
/usr/include/boost/python/enum.hpp:32: error: expected ‘;’ before ‘(’ token
/usr/include/boost/python/enum.hpp:33: error: ‘PyObject’ has not been declared
/usr/include/boost/python/enum.hpp:52: error: expected constructor, destructor, or type conversion before ‘*’ token
/usr/include/boost/python/enum.hpp:67: error: ‘void* boost::python::enum_<T>::convertible_from_python’ is not a static member of ‘struct boost::python::enum_<T>’
/usr/include/boost/python/enum.hpp:67: error: template definition of non-template ‘ void* boost::python::enum_<T>::convertible_from_python’
/usr/include/boost/python/enum.hpp:67: error: ‘PyObject’ was not declared in this scope
/usr/include/boost/python/enum.hpp:67: error: ‘obj’ was not declared in this scope
/usr/include/boost/python/enum.hpp:80: error: variable or field ‘construct’ declared void
/usr/include/boost/python/enum.hpp:80: error: ‘PyObject’ was not declared in this scope
/usr/include/boost/python/enum.hpp:80: error: ‘obj’ was not declared in this scope
/usr/include/boost/python/enum.hpp:80: error: expected primary-expression before ‘*’ token
/usr/include/boost/python/enum.hpp:80: error: ‘data’ was not declared in this scope
Another thing is that when I compile g++ -Wall thread_one.cpp -o thread_one -lboost_thread, that works in order to use boost_thread library.
Try this:
g++ try.cpp -o try -lboost_python
It'd be good style to also add -W -Wall -Wextra -pedantic to your compiler invocation (so that your next SO question can be more specific :-)). Also, -O2 or -O3 for optimization is probably a very good idea, especially with Boost. Finally, splitting the building up into separate stages makes recompiling faster when you have multiple files:
g++ -c -o try.o try.cpp -W -Wall -Wextra -pedantic -O2
g++ -o try try.o -s -lboost_python
Finally, it is working. The command used is as below:
g++ -I/usr/include/python2.6 try.cpp -o try -lboost_python -lpython2.6
i have a file with only import:
#include <iostream>
#include <stdio.h>
#include "cxcore.hpp"
#include "highgui.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
}
and i try to compile with g++ -I/usr/include/opencv -lopencv -lm m.cpp
but get whit error:
In file included from /usr/include/opencv/cxcore.hpp:46,
from m.cpp:5:
/usr/include/opencv/cxmisc.h:214: error: expected constructor, destructor, or type conversion before ‘void’
/usr/include/opencv/cxmisc.h:220: error: expected constructor, destructor, or type conversion before ‘int’
/usr/include/opencv/cxmisc.h:226: error: ‘CV_INLINE’ does not name a type
/usr/include/opencv/cxmisc.h:516: error: ‘CV_DEPTH_MAX’ was not declared in this scope
/usr/include/opencv/cxmisc.h:522: error: ‘CV_DEPTH_MAX’ was not declared in this scope
/usr/include/opencv/cxmisc.h:522: error: ‘CV_CN_MAX’ was not declared in this scope
In file included from m.cpp:5:
/usr/include/opencv/cxcore.hpp:70: error: template declaration of ‘cv::CV_EXPORTS cv::Size_’
/usr/include/opencv/cxcore.hpp:71: error: template declaration of ‘cv::CV_EXPORTS cv::Point_’
/usr/include/opencv/cxcore.hpp:72: error: template declaration of ‘cv::CV_EXPORTS cv::Rect_’
/usr/include/opencv/cxcore.hpp:77: error: expected initializer before ‘fromUtf16’
/usr/include/opencv/cxcore.hpp:78: error: expected initializer before ‘toUtf16’
/usr/include/opencv/cxcore.hpp:80: error: expected initializer before ‘format’
/usr/include/opencv/cxcore.hpp:82: error: expected initializer before ‘:’ token
m.cpp:38: error: expected ‘}’ at end of input
this is my copencv lib content:
alberto#zefiro:~$ ls /usr/include/opencv/
cvaux.h cvcompat.h cv.hpp cvtypes.h cvvidsurv.hpp cxcore.h cxerror.h cxmat.hpp cxoperations.hpp highgui.h ml.h
cvaux.hpp cv.h cvinternal.h cvver.h cvwimage.h cxcore.hpp cxflann.h cxmisc.h cxtypes.h highgui.hpp
i'm on ubuntu 10.10
You need to properly include the headers -I (capital i) and libraries -l (lowercase L).
On the newest OpenCV versions you should do:
#include <cv.h>
#include <highgui.h>
And then try to compile it with:
g++ m.cpp -o app `pkg-config --cflags --libs opencv`
Note: if you execute only pkg-config --cflags --libs opencv in the command line you will see the paths and libraries you need to include in the g++ command line.
if your development environment does not have pkg-config and because of this the accepted answer by karlphilip is not practical, or, you need to know the minimal set of libraries required to link your application, then assuming code such as
#include <cv.h>
#include <highgui.h>
int main()
{
return 0;
}
you can add library arguments from the following list sequentially from the top until you find the minimal set of arguments that you need:
-lopencv_core
-lopencv_imgproc
-lopencv_highgui
-lopencv_ml
-lopencv_video
-lopencv_features2d
-lopencv_calib3d
-lopencv_objdetect
-lopencv_contrib
-lopencv_legacy
-lopencv_flann
For example, the C source code listed at the top of this post compiles and links cleanly with only
gcc hello.c -o hello \
-I /usr/include/opencv \
-L /usr/lib \
-lopencv_core \
-lopencv_imgproc
on my old x86_64 Ubuntu 12.04 box.
Assuming C++ code such as
#include "core/core.hpp"
#include "highgui/highgui.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
return 0;
}
then you would compile and link with
g++ hello.cpp -o hello \
-I /usr/include/opencv2 \
-L /usr/lib \
-lopencv_core \
-lopencv_imgproc
I suggest you use CMake to compile OpenCV with G++, this way is more suitable, I think.
cmake_minimum_required(VERSION 3.1)
project(YOUR_PROJECT_NAME)
set(CMAKE_GXX_FLAGS "-Wall -Wextra -Wconversion -pedantic -std=gnu11")
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(YOUR_EXCUTABLE YOUR_CODE_SOURCE_FILES)
target_link_libraries(YOUR_EXCUTABLE ${OpenCV_LIBS})
Download source files in OpenCV folder and
install-opencv.sh script.
By running script file you
automatically install needed files for opencv. Run the following
code:
chmod +x install-opencv.sh
./install-opencv.sh
In case if you install different version of the library please update the first line of version inside the installation script.
For more information use this tutorial. Compile it with the next line:
g++ `pkg-config --cflags opencv` example.cpp `pkg-config --libs opencv`
I think that the accepted answer is a bit old.
At least for OpenCV 4.X, the most recently release today, you can use:
#include <opencv2/opencv.hpp>
and compile your program by something like:
g++ your_program_file.cpp -o your_program `pkg-config --cflags --libs opencv4`
Note opencv4 and not only opencv.
For example, using the command above to compile the following your_program_file.cpp file:
#include <opencv2/opencv.hpp>
int main() {
std::cout << "The current OpenCV version is " << CV_VERSION << "\n";
return 0;
}
And running:
./your_program
Outputs:
The current OpenCV version is 4.4.0
Which indeed matches with my current OpenCV settings.