【发布时间】:2019-02-09 10:40:46
【问题描述】:
我想用 Makefile 编译 opencv4.0 但出现未定义的引用错误。
我曾经在 Windows 中使用 opencv,代码只是简单的代码,仅在 ubuntu18.10 中显示用于测试的图像。
但如果我在 shell 上输入下面的行,它会起作用。
g++ -o simple main.cpp $(pkg-config opencv4 --libs --cflags)
我的 Makefile 在下面
CC = g++
CFLAGS = -W -Wall
SRCS = main.cpp
TARGET = simple
OPENCV = $(pkg-config opencv4 --libs --cflags)
LIBS = $(OPENCV)
$(TARGET):$(SRCS)
$(CC) $(CFLAGS) -o $(TARGET) $(SRCS) $(LIBS)
clean:
rm -f $(OBJECTS) $(TARGET) core
下面是我的opencv4.pc。
# Package Information for pkg-config
prefix=/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include/opencv4
Name: OpenCV
Description: Open Source Computer Vision Library
Version: 4.0.0
Libs: -L${exec_prefix}/lib -lopencv_gapi -lopencv_stitching -lopencv_aruco -lopencv_bgsegm -lopencv_b
Libs.private: -ldl -lm -lpthread -lrt -L/usr/lib/x86_64-linux-gnu -lGL -lGLU
Cflags: -I${includedir}
并且错误如下。
g++ -W -Wall -o simple main.cpp
/usr/bin/ld: /tmp/cciHsvhP.o: in function `main':
main.cpp:(.text+0x70): undefined reference to `cv::imread(cv::String const&, int)'
/usr/bin/ld: main.cpp:(.text+0xc4): undefined reference to `cv::imshow(cv::String const&, cv::_InputArray const&)'
....
collect2: error: ld returned 1 exit status
make: *** [Makefile:11: simple] Error 1
【问题讨论】:
-
嗯?您的 Makefile 无法正常工作,因为它没有生成与您在 shell 中运行
g++时相同的命令 - OpenCV 开关全部丢失。 -
@MarkSetchell 这意味着什么openCV 开关全部丢失?
-
如果您查看您说 *"and error is below" 之后的行,您会看到执行的命令不包括您已经说过并且知道您需要的所有库。这也是弗洛里安在他的回答中告诉你的。