0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

opencv-vscode-mac-c++ 環境構築 困ったこと

Posted at

環境構築手順

1. opencvをインストール

brew install opencv

バージョン確認

brew list --versions | grep opencv
// => 4.5.3   私の場合

2. C++のプログラムを用意

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

int main() {
    cv::Mat img = cv::imread("sample.jpeg");

    cv::namedWindow("test", cv::WINDOW_AUTOSIZE);
    cv::imshow("test", img);
    cv::waitKey(0);
    cv::destroyWindow("test");
    return 0;
}

ディレクトリ構造

├── main.cpp
└── sample.jpeg

適当な画像を用意し上記のディレクトリ構造にした。

3. 実行

g++ main.cpp -o main.out `pkg-config --cflags opencv` `pkg-config --libs opencv` && ./main.out
エラー
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
main.cpp:1:10: fatal error: 'opencv2/opencv.hpp' file not found
# include <opencv2/opencv.hpp>
         ^~~~~~~~~~~~~~~~~~~~
1 error generated.

あれ、、、インストールしたはずのopencvがないと言われている。。。。

解決方法

1. pkgconfigにコピーする

sudo cp /usr/local/Cellar/opencv/インストールしたバージョン/lib/pkgconfig/opencv4.pc /usr/local/lib/pkgconfig/opencv.pc
私の場合
sudo cp /usr/local/Cellar/opencv/4.5.3/lib/pkgconfig/opencv4.pc /usr/local/lib/pkgconfig/opencv.pc

2. 実行

echo `pkg-config --cflags opencv`
g++ -std=c++11 main.cpp -o main.out `pkg-config --cflags opencv` `pkg-config --libs opencv` && ./main.out

参考サイト

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?