在c++中编译opencv
我有一个仅导入的文件:
#include <iostream>
#include <stdio.h>
#include "cxcore.hpp"
#include "highgui.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
}
我尝试使用 g++ -I/usr/include/opencv -lopencv -lm m.cpp 进行编译,
但出现错误:
在 /usr/include/opencv/cxcore.hpp:46 包含的文件中, 来自 m.cpp:5: /usr/include/opencv/cxmisc.h:214: 错误:预期的构造函数、析构函数或“void”之前的类型转换 /usr/include/opencv/cxmisc.h:220: 错误:预期的构造函数、析构函数或“int”之前的类型转换 /usr/include/opencv/cxmisc.h:226:错误:“CV_INLINE”未命名类型 /usr/include/opencv/cxmisc.h:516:错误:未在此范围内声明“CV_DEPTH_MAX” /usr/include/opencv/cxmisc.h:522:错误:未在此范围内声明“CV_DEPTH_MAX” /usr/include/opencv/cxmisc.h:522:错误:未在此范围内声明“CV_CN_MAX” 在 m.cpp:5 包含的文件中: /usr/include/opencv/cxcore.hpp:70: 错误: 'cv::CV_EXPORTS cv::Size_' 的模板声明 /usr/include/opencv/cxcore.hpp:71: 错误: 'cv::CV_EXPORTS cv::Point_' 的模板声明 /usr/include/opencv/cxcore.hpp:72: 错误: 'cv::CV_EXPORTS cv::Rect_' 的模板声明 /usr/include/opencv/cxcore.hpp:77: 错误: 'fromUtf16' 之前的预期初始化程序 /usr/include/opencv/cxcore.hpp:78: 错误: 'toUtf16' 之前的预期初始化程序 /usr/include/opencv/cxcore.hpp:80:错误:“格式”之前的预期初始化程序 /usr/include/opencv/cxcore.hpp:82: 错误: ':' 标记之前的预期初始化程序 m.cpp:38: 错误:输入末尾应有“}”
这是我的 copencv lib 内容:
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
我使用的是 ubuntu 10.10
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要正确包含标头
-I
(大写 i)和库-l
(小写 L)。在最新的 OpenCV 版本上,您应该执行以下操作:
然后尝试使用以下命令进行编译:
注意:如果您仅在命令行中执行
pkg-config --cflags --libs opencv
您将看到路径和您需要包含在 g++ 命令行中的库。You need to properly include the headers
-I
(capital i) and libraries-l
(lowercase L).On the newest OpenCV versions you should do:
And then try to compile it with:
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.如果您的开发环境没有
pkg-config
并且因此 karlphilip 接受的答案不切实际,或者,您需要知道链接应用程序所需的最小库集,然后假设您可以从以下列表中从顶部按顺序添加库参数,直到找到您需要的最小参数集。需要:
例如,本文顶部列出的 C 源代码仅
在我的旧 x86_64 Ubuntu 12.04 机器上干净地编译和链接。
假设 C++ 代码如下
,您将编译并链接
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 asyou can add library arguments from the following list sequentially from the top until you find the minimal set of arguments that you need:
For example, the C source code listed at the top of this post compiles and links cleanly with only
on my old x86_64 Ubuntu 12.04 box.
Assuming C++ code such as
then you would compile and link with
我建议你使用CMake用
G++
来编译OpenCV
,我认为这种方式更合适。I suggest you use CMake to compile
OpenCV
withG++
, this way is more suitable, I think.我认为接受的答案有点旧了。
至少对于今天最新发布的 OpenCV 4.X,您可以使用:
并通过以下方式编译您的程序:
注意
opencv4
而不仅仅是opencv
。例如,使用上面的命令编译以下
your_program_file.cpp
文件:并运行:
输出:
这确实与我当前的 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:
and compile your program by something like:
Note
opencv4
and not onlyopencv
.For example, using the command above to compile the following
your_program_file.cpp
file:And running:
Outputs:
Which indeed matches with my current OpenCV settings.
install-opencv.sh 脚本。
通过运行脚本文件,您
自动安装opencv所需的文件。运行以下命令
代码:
如果您安装不同版本的库,请更新安装脚本中版本的第一行。
有关更多信息,请使用此教程。使用下一行进行编译:
install-opencv.sh script.
By running script file you
automatically install needed files for opencv. Run the following
code:
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: