Last active
June 14, 2023 11:59
-
-
Save treastrain/11bb810fda45f1b7d7bbbc041eddbd6a to your computer and use it in GitHub Desktop.
✅ A C++ project using OpenCV that can be compiled with Clang but not with GCC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.cpp | |
// | |
// Created by treastrain / Tanaka Ryoga on 2021/01/25. | |
// | |
#include "main.hpp" | |
int main(int, char**) { | |
std::cout << "Hello, OpenCV World!" << std::endl; | |
std::cout << "OpenCV version: " << CV_VERSION << std::endl; | |
cv::Mat sourceImage = cv::imread("./image.jpg"); | |
if (!sourceImage.data) { | |
std::cerr << "Data cannot be read." << std::endl; | |
exit(-1); | |
} | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.hpp | |
// | |
// Created by treastrain / Tanaka Ryoga on 2021/01/25. | |
// | |
#include <iostream> | |
#include <opencv2/opencv.hpp> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
OpenCV-Playground: | |
echo OpenCV-Playground | |
buildclang: | |
clang++ -Wall -std=c++17 -O0 \ | |
-I../Libraries/opencv/include/opencv4 -L../Libraries/opencv/lib \ | |
-lopencv_core -lopencv_videoio -lopencv_imgproc -lopencv_imgcodecs \ | |
-o OpenCV-Playground \ | |
main.cpp | |
buildgcc: | |
g++ -Wall -std=c++17 -O0 \ | |
-I../Libraries/opencv/include/opencv4 -L../Libraries/opencv/lib \ | |
-lopencv_core -lopencv_videoio -lopencv_imgproc -lopencv_imgcodecs \ | |
-o OpenCV-Playground \ | |
main.cpp |
Author
treastrain
commented
Jan 24, 2021
•
- Ubuntu 20.04.01 LTS
- OpenCV 4.5.1
- Clang 10.0.0
- g++ (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
$ make buildclang
clang++ -Wall -std=c++17 -O0 \
-I../Libraries/opencv/include/opencv4 -L../Libraries/opencv/lib \
-lopencv_core -lopencv_videoio -lopencv_imgproc -lopencv_imgcodecs \
-o OpenCV-Playground \
main.cpp
$ make buildgcc
g++ -Wall -std=c++17 -O0 \
-I../Libraries/opencv/include/opencv4 -L../Libraries/opencv/lib \
-lopencv_core -lopencv_videoio -lopencv_imgproc -lopencv_imgcodecs \
-o OpenCV-Playground \
main.cpp
/usr/bin/ld: /tmp/cc2mRTwF.o: in function `main':
main.cpp:(.text+0xc0): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: main.cpp:(.text+0x118): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: main.cpp:(.text+0x16c): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit status
make: *** [Makefile:12: buildgcc] エラー 1
Clang でも同じエラーを発生させるためには、
-lopencv_core -lopencv_videoio (…省略)
を消す
https://twitter.com/treastrain/status/1353379531227205632
ref: https://stackoverflow.com/questions/62098479/opencv-undefined-reference-to-cvimreadcvstring-const-int
参考: https://stackoverflow.com/questions/62098479/opencv-undefined-reference-to-cvimreadcvstring-const-int
pkg-config needed to be after the source files.
pkg-config はソースファイルの後ろに指定する必要があります (訳: @Qs-F )
So put main.cpp
before -I../Libraries/opencv/include/op...
.
The whole script will be like
なので main.cpp
を -I../Libraries/opencv/include/op...
. の前に書いてください。
全体像は次のようになります
g++ -Wall -std=c++17 -O0 \
main.cpp \
-I../Libraries/opencv/include/opencv4 -L../Libraries/opencv/lib \
-lopencv_core -lopencv_videoio -lopencv_imgproc -lopencv_imgcodecs \
-o OpenCV-Playground \
@Qs-F You're my sunshine!
🦀🦀🦀🦀🦀🦀
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment