在c++中编译opencv

发布于 2025-01-01 07:45:01 字数 1671 浏览 1 评论 0原文

我有一个仅导入的文件:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

任性一次 2025-01-08 07:45:01

您需要正确包含标头 -I(大写 i)和库 -l(小写 L)。

在最新的 OpenCV 版本上,您应该执行以下操作:

#include <cv.h>
#include <highgui.h>

然后尝试使用以下命令进行编译:

g++ m.cpp -o app `pkg-config --cflags --libs 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:

#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.

丑丑阿 2025-01-08 07:45:01

如果您的开发环境没有 pkg-config 并且因此 karlphilip 接受的答案不切实际,或者,您需要知道链接应用程序所需的最小库集,然后假设

#include <cv.h>
#include <highgui.h>

int main()
{
    return 0;
}

您可以从以下列表中从顶部按顺序添加库参数,直到找到您需要的最小参数集。需要:

  -lopencv_core
  -lopencv_imgproc
  -lopencv_highgui
  -lopencv_ml
  -lopencv_video
  -lopencv_features2d
  -lopencv_calib3d
  -lopencv_objdetect
  -lopencv_contrib
  -lopencv_legacy
  -lopencv_flann

例如,本文顶部列出的 C 源代码仅

gcc hello.c -o hello \
    -I /usr/include/opencv \
    -L /usr/lib \
    -lopencv_core \
    -lopencv_imgproc

在我的旧 x86_64 Ubuntu 12.04 机器上干净地编译和链接。

假设 C++ 代码如下

#include "core/core.hpp"
#include "highgui/highgui.hpp"

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    return 0;
}

,您将编译并链接

g++ hello.cpp -o hello \
    -I /usr/include/opencv2 \
    -L /usr/lib \
    -lopencv_core \
    -lopencv_imgproc

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
长安忆 2025-01-08 07:45:01

我建议你使用CMake用G++来编译OpenCV,我认为这种方式更合适。

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})

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})
国产ˉ祖宗 2025-01-08 07:45:01

我认为接受的答案有点旧了。

至少对于今天最新发布的 OpenCV 4.X,您可以使用:

#include <opencv2/opencv.hpp>

并通过以下方式编译您的程序:

g++ your_program_file.cpp -o your_program `pkg-config --cflags --libs opencv4`

注意 opencv4 而不仅仅是 opencv

例如,使用上面的命令编译以下 your_program_file.cpp 文件:

#include <opencv2/opencv.hpp>

int main() {

    std::cout << "The current OpenCV version is " << CV_VERSION << "\n";

    return 0;

}

并运行:

./your_program 

输出:

The current OpenCV version is 4.4.0

这确实与我当前的 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.

极致的悲 2025-01-08 07:45:01
  1. 下载 OpenCV 文件夹中的源文件并
    install-opencv.sh 脚本。
  2. 通过运行脚本文件,您
    自动安装opencv所需的文件。运行以下命令
    代码:

    chmod +x install-opencv.sh
    ./安装-opencv.sh
    

如果您安装不同版本的库,请更新安装脚本中版本的第一行。
有关更多信息,请使用教程。使用下一行进行编译:

g++ `pkg-config --cflags opencv` example.cpp `pkg-config --libs opencv`
  1. Download source files in OpenCV folder and
    install-opencv.sh script.
  2. 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`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文