Help us understand the problem. What is going on with this article?

pkg-configを使ってg++でコンパイルできるようになるまで

前回の続き

前回のページはこちら

こちらでは、opencv4.0をubuntu18.04(LTS)にインストールするまでをやりました

今回やること

pkg-configを使ってターミナル上で
g++ sample.cpp `pkg-config --cflags --libs opencv4` -o sampleB
でコンパイルできるようにする。

内容

前回の記事で、インストール終わりました。
一番のキーは /usr/local/lib/pkgconfig/opencv4.pc
ファイルで
内容は

opencv4.pc
Package Information for pkg-config

prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir_old=${prefix}/include/opencv4/opencv
includedir_new=${prefix}/include/opencv4

Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.1.0
Libs: -L${exec_prefix}/lib -lopencv_dnn -lopencv_gapi -lopencv_highgui -lopencv_ml -lopencv_objdetect -lopencv_photo -lopencv_stitching -lopencv_video -lopencv_calib3d -lopencv_features2d -lopencv_flann -lopencv_videoio -lopencv_imgcodecs -lopencv_imgproc -lopencv_core
Libs.private: -ldl -lm -lpthread -lrt
Cflags: -I${includedir_old} -I${includedir_new}

これでターミナルで
g++ sample.cpp `pkg-config --cflags --libs opencv4` -o sampleB
と叩いても、

Package opencv4 was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv4.pc'
to the PKG_CONFIG_PATH environment variable

と出てしまうので
.bashrcファイルへopecv4.pcファイルへのパスをPKG_CONFIG_PATHへ代入。(以下今回での追加分のみ記載)

.bashrc
export PKG_CONFIG_PATH==/usr/local/lib/pkgconfig

これで
g++ sample.cpp `pkg-config --cflags --libs opencv4` -o sampleB
でコンパイルできるようになる。

以下、ちょっと説明

pkg-configを使わずに直接コンパイルする場合、

g++ sample.cpp -I/usr/local/include/opencv4 -lopencv_core -lopencv_imgcodecs -lopencv_highgui -o sample

と叩いてもコンパイルはできる。
しかし、毎回インクルードするファイルをパスごと打つのはとても面倒なのでpkg-configを使った。
pkg-configの中身

Libs:~
の中身で
-lopencv_core -lopencv_imgcodecs -lopencv_highgui
を置き換え、
Cflags:~
の中身で
-I/usr/local/include/opencv4
を置き換えている。

参考URL

https://docs.opencv.org/master/d7/d9f/tutorial_linux_install.html
https://prateekvjoshi.com/2013/10/18/package-opencv-not-found-lets-find-it/
https://ja.stackoverflow.com/questions/53767/macos-c%E3%81%A7opencv%E3%81%8C%E5%AE%9F%E8%A1%8C%E3%81%A7%E3%81%8D%E3%81%AA%E3%81%84
https://qiita.com/hsmtknj/items/0f56e3317cd0f2ae056a#%E5%82%99%E8%80%83

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away