簡體   English   中英

如何在沒有 Makefile 的情況下編譯包含 OpenCV 庫的 C++ 代碼?

[英]How to compile C++ code that contains OpenCV library without Makefile?

我剛開始用 C++ 學習 OpenCV。 我在 Linux 機器上從源代碼構建了 OpenCV。 我使用 Visual Studio Code 文本編輯器。 我想編譯包含沒有 Makefile 的 OpenCV 庫的 C++ 代碼。 我將/usr/local/opencv4/**添加到includePath變量。 但它沒有用。 我收到 opencv2/opencv.hpp 錯誤。 是否可以在沒有 Makefile 的情況下構建包含 OpenCV 的 C++ 代碼? 我該怎么做?

我的代碼

#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
  
int main(int argc, char** argv)
{
    Mat image = imread("Enter the Address"
                       "of Input Image",
                       IMREAD_GRAYSCALE);
  
    if (image.empty()) {
        cout << "Image File "
             << "Not Found" << endl;
  
        cin.get();
        return -1;
    }
  
    imshow("Window Name", image);
  
    waitKey(0);
    return 0;
}

您必須告訴編譯器頭文件和庫文件的位置。 正如 kotatsuyaki 所提到的,你可以使用 pkg-config 為你的。

gcc 和 pkg-config 的示例:

g++ *.cpp -o program_name `pkg-config --cflags --libs opencv4`

使用 gcc 且不使用 pkg-config 的示例:

g++ *.cpp -o program_name -I/PATH/TO/OPENCV/INCLUDE/FOLDER -L/PATH/TO/OPENCV/LIB/FOLDER -lopencv_something -lopencv_somethingelse ... etc

在帶有 cl 的 Windows 中,您可以在沒有 pkg-config 的情況下執行與 g++ 相同的操作:

cl main.cpp /I"PATH TO OPENCV ICNLUDE FOLDER" /link /LIBPATH:"PATH TO OPENCV LIB FOLDER" opencv_library1.lib opencv_libarary2.lib ... etc. 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM