C++ ubuntu 12.04中的openCV程序编译错误“libopencv_core.so.2.4:无法打开共享对象文件:没有这样的文件或目录”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12335848/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:10:48  来源:igfitidea点击:

openCV program compile error "libopencv_core.so.2.4: cannot open shared object file: No such file or directory" in ubuntu 12.04

c++ubuntuopencvg++

提问by linkrules

I compiled and installed openCV 2.4.2 in ubuntu 12.04. Under /usr/local/includeI can see the directories /usr/local/opencvand /usr/local/opencv2.

我在 ubuntu 12.04 中编译并安装了 openCV 2.4.2。在/usr/local/include我可以看到目录/usr/local/opencv/usr/local/opencv2.

Here is the code I wrote:

这是我写的代码:

#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc,char **argv)
{
   Mat image;
   image = imread(argv[1],1);

   if(argc != 2 || !image.data)
   {
       cout << "No image data\n";
       return -1;
   }

   namedWindow("Display Image",CV_WINDOW_AUTOSIZE);
   imshow("Display Image",image);
   waitKey(0);
   return 0;
}

I compiled it using this command line:

我使用这个命令行编译它:

g++ DisplayImage.cpp -o DisplayImage `pkg-config opencv --cflags --libs` 

There were no compile time errors, however when I try to run the resulting binary with /DisplayImage code.pngI get the following error message:

没有编译时错误,但是当我尝试运行生成的二进制文件时,/DisplayImage code.png我收到以下错误消息:

./DisplayImage: error while loading shared libraries: libopencv_core.so.2.4: cannot open shared object file: No such file or directory

回答by Cookyt

You haven't put the shared library in a location where the loader can find it. look inside the /usr/local/opencvand /usr/local/opencv2folders and see if either of them contains any shared libraries (files beginning in liband usually ending in .so). when you find them, create a file called /etc/ld.so.conf.d/opencv.confand write to it the paths to the folders where the libraries are stored, one per line.

您没有将共享库放在加载程序可以找到的位置。查看/usr/local/opencv/usr/local/opencv2文件夹,看看它们中是否有任何一个包含任何共享库(以 开头lib并通常以 结尾的文件.so)。找到它们后,创建一个名为/etc/ld.so.conf.d/opencv.conf的文件,并将存储库的文件夹的路径写入其中,每行一个。

for example, if the libraries were stored under /usr/local/opencv/libopencv_core.so.2.4then I would write this to my opencv.conffile:

例如,如果库存储在下面,/usr/local/opencv/libopencv_core.so.2.4那么我会将其写入我的opencv.conf文件:

/usr/local/opencv/

Then run

然后运行

sudo ldconfig -v

If you can't find the libraries, try running

如果找不到库,请尝试运行

sudo updatedb && locate libopencv_core.so.2.4

in a shell. You don't need to run updatedbif you've rebooted since compiling OpenCV.

在一个壳里。updatedb如果您在编译 OpenCV 后重新启动,则不需要运行。

References:

参考:

About shared libraries on Linux: http://www.eyrie.org/~eagle/notes/rpath.html

关于 Linux 上的共享库:http: //www.eyrie.org/~eagle/notes/rpath.html

About adding the OpenCV shared libraries: http://opencv.willowgarage.com/wiki/InstallGuide_Linux

关于添加 OpenCV 共享库:http: //opencv.willowgarage.com/wiki/InstallGuide_Linux

回答by Umair R

To make it more clear(and to put it together) I had to do Two things mentioned above.

为了使它更清楚(并将其放在一起),我必须做上面提到的两件事。

1- Create a file /etc/ld.so.conf.d/opencv.confand write to it the paths of folder where your opencv libraries are stored.(Answer by Cookyt)

1-创建一个文件/etc/ld.so.conf.d/opencv.conf并将存储您的opencv库的文件夹的路径写入其中。(由Cookyt回答)

2- Include the path of your opencv's .sofiles in LD_LIBRARY_PATH ()

2-.so在 LD_LIBRARY_PATH () 中包含 opencv文件的路径

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/opencv/lib

回答by Andol Li

Umair R's answer is mostly the right move to solve the problem, as this error used to be caused by the missing links between opencv libs and the programme. so there is the need to specify the ld_libraty_path configuration. ps. the usual library path is suppose to be:

Umair R 的回答主要是解决问题的正确举措,因为这个错误曾经是由 opencv libs 和程序之间缺少链接引起的。所以需要指定 ld_libraty_path 配置。附:通常的库路径假设为:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib

I have tried this and it worked well.

我试过这个,效果很好。

回答by codeislife

Find the folder containing the shared library libopencv_core.so.2.4 using the following command line.

使用以下命令行查找包含共享库 libopencv_core.so.2.4 的文件夹。

sudo find / -name "libopencv_core.so.2.4*"

Then I got the result:

然后我得到了结果:

 /usr/local/lib/libopencv_core.so.2.4.

Create a file called /etc/ld.so.conf.d/opencv.confand write to it the path to the folder where the binary is stored.For example, I wrote /usr/local/lib/to my opencv.conffile. Run the command line as follows.

创建一个名为 /etc/ld.so.conf.d/opencv.conf的文件,并将存储二进制文件的文件夹的路径写入其中。例如,我写入/usr/local/lib/了我的opencv.conf文件。运行命令行如下。

sudo ldconfig -v

Try to run the command again.

尝试再次运行该命令。

回答by misakawa

Add this link:

添加此链接:

/usr/local/lib/*.so.*

The total is:

总数是:

g++ -o main.out main.cpp -I /usr/local/include -I /usr/local/include/opencv -I /usr/local/include/opencv2 -L /usr/local/lib /usr/local/lib/*.so /usr/local/lib/*.so.*