C++ Beaglebone上的OpenCV编译错误

C++ Beaglebone上的OpenCV编译错误,c++,opencv,compilation,C++,Opencv,Compilation,我使用这里提到的步骤安装了Ubuntu 14.04和Opencv 我正在尝试编译保存在名为test2.cpp的文本文件中的以下代码。test2.cpp和lena.jpg已复制到beaglebone主文件夹: // Test to convert a color image to gray // Build on Linux with: // g++ test2.cpp -o test2 -lopencv_core -lopencv_imgproc -lopencv_highgui #inclu

我使用这里提到的步骤安装了Ubuntu 14.04和Opencv 我正在尝试编译保存在名为test2.cpp的文本文件中的以下代码。test2.cpp和lena.jpg已复制到beaglebone主文件夹:

// Test to convert a color image to gray
// Build on Linux with:
// g++ test2.cpp -o test2 -lopencv_core -lopencv_imgproc -lopencv_highgui

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    // Load the image file and check for success
    cv::Mat input = cv::imread("lena.jpg", 1);
    if(!input.data) {
        std::cout << "Unable to open the image file" << std::endl;
        return -1;
    }

    // Convert the input file to gray
    cv::Mat gray_image;
    cvtColor(input, gray_image, cv::COLOR_BGR2GRAY);

    // Save the result
    cv::imwrite("lena_gray.jpg", gray_image);

    return 0;
}
使用g++test2.cpp-o test2-lopencv_core-lopencv_imgproc-lopencv_highgui 我还尝试告诉编译器库在哪里使用-L/usr/local/lib。在那里找到了libopencv文件。但每次都会出现以下错误:

ubuntu@arm:~$ g++ test2.cpp -o test2 -lopencv_core -lopencv_imgproc -lopencv_highgui -L usr/local/lib
/tmp/cckXjOPd.o: In function `main':
test2.cpp:(.text+0x26): undefined reference to `cv::imread(cv::String const&, int)'
test2.cpp:(.text+0xf0): undefined reference to `cv::imwrite(cv::String const&, cv::_InputArray const&, std::vector<int, std::allocator<int=""> > const&)'
collect2: error: ld returned 1 exit status

有人能帮我吗?任何帮助都将不胜感激。

我发现使用以下工具进行编译总是方便且安全的:

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

如果尚未提供,请安装pkg config。

Typo:库路径缺少前导/。您需要-L/usr/local/lib。在正确设置此类内容的情况下编写Makefile可能更容易。我尝试添加前导/但出现了相同的错误。我尝试了您提供的编译代码。它没有显示任何错误,所以已经安装了pkg config?但我没有看到任何输出。一个名为lena_gray.jpg的文件应该保存在主文件夹中,对吗?但我没有看到任何这样的文件。另外,请注意,我前面提到的代码使用jpg。但是我在代码中使用了jpeg,因为我的文件是jpeg。这会造成问题吗?谢谢。我应该在编译后运行./test2命令。工作起来很有魅力!如果希望看到编译的详细输出,请在标志中添加-v。是的,文件名必须匹配。很高兴它帮助解决了您的问题: