1

There are a couple similar problems to this but they all seem to have answers that don't work.

I've installed opencv4 onto an image of Ubuntu and I know the opencv files are install correctly

$ pkg-config --cflags opencv
-I/usr/include/opencv

and have a proper .cp file

/usr/local/lib/pkgconfig$ ls
gpr.pc   grpc++.pc         grpc++_unsecure.pc  protobuf-lite.pc
grpc.pc  grpc_unsecure.pc  opencv4.pc          protobuf.pc

But even though it's all installed correctly it's giving me this error.

~/neuralink/neuralink-image-service-prompt/proto$ make g++ -std=c++11 `pkg-config --cflags opencv protobuf grpc` -L/usr/local/lib `pkg-config --libs opencv protobuf grpc++` -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed -ldl  foo.cpp   -o foo
/tmp/ccnfoOEl.o: In function `main':
foo.cpp:(.text+0x48): undefined reference to `cv::imread(cv::String const&, int)'
/tmp/ccnfoOEl.o: In function `cv::String::String(char const*)':
foo.cpp:(.text._ZN2cv6StringC2EPKc[_ZN2cv6StringC5EPKc]+0x4d): undefined reference to `cv::String::allocate(unsigned long)'
/tmp/ccnfoOEl.o: In function `cv::String::~String()':
foo.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccnfoOEl.o: In function `cv::String::operator=(cv::String const&)':
foo.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
/tmp/ccnfoOEl.o: In function `cv::Mat::~Mat()':
foo.cpp:(.text._ZN2cv3MatD2Ev[_ZN2cv3MatD5Ev]+0x39): undefined reference to `cv::fastFree(void*)'
/tmp/ccnfoOEl.o: In function `cv::Mat::release()':
foo.cpp:(.text._ZN2cv3Mat7releaseEv[_ZN2cv3Mat7releaseEv]+0x4b): undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
<builtin>: recipe for target 'foo' failed
make: *** [foo] Error 1

Some people have already tried answering similar questions (such as Undefined reference to cv::imread(cv::String const&, int) and linking opencv libraries with g++) however these don't help, as the user didn't add opencv to their makefile and I have.

Here is my make makefile

LDFLAGS = -L/usr/local/lib `pkg-config --libs opencv protobuf grpc++`\
           -Wl,--no-as-needed -lgrpc++_reflection -Wl,--as-needed\
           -ldl

CXX = g++
CPPFLAGS += `pkg-config --cflags opencv protobuf grpc`
CXXFLAGS += -std=c++11

GRPC_CPP_PLUGIN = grpc_cpp_plugin
GRPC_CPP_PLUGIN_PATH ?= `which $(GRPC_CPP_PLUGIN)`

all: foo

client: image.pb.o image.grpc.pb.o client.o
    $(CXX) $^ $(LDFLAGS) -o $@

server: image.pb.o image.grpc.pb.o server.o
    $(CXX) $^ $(LDFLAGS) -o $@

%.grpc.pb.cc: %.proto
    protoc --grpc_out=. --plugin=protoc-gen-grpc=$(GRPC_CPP_PLUGIN_PATH) $<

%.pb.cc: %.proto
    protoc --cpp_out=. $<

clean:
    rm -f *.o *.pb.cc *.pb.h client server

And I wrote a simple program that still throws the error

#include <opencv2/opencv.hpp>
#include <stdio.h>

int main() {

    cv::Mat frame = cv::imread("sylvania.png", cv::IMREAD_UNCHANGED);

    printf("Image size is %i x %i\n", frame.rows, frame.cols);

}

Everything seems to compile fine but I keep getting this linker issue. I don't seem to get the issue when I compile it on another computer (my mac) for what it's worth. Also I have the gRPC and Protoc files because I need them for the project, I just didn't use those file as an example since they're quite long.

haxonek
  • 174
  • 1
  • 2
  • 17
  • 1
    you have to link all the individual libraries that you are using, like opencv_core, opencv_highgui etc.. Or, if present, link opencv_world as an all-in-one library – Micka Aug 07 '19 at 04:50

1 Answers1

2

I agree with Micka, here is a Makefile of one of my projects:

CPPFLAGS=-g -Wall -I. -DDEBUG
LDFLAGS=-g
LDLIBS=-lopencv_core -lopencv_calib3d -lopencv_highgui -lopencv_imgproc -lopencv_stitching -lopencv_video

main: main.o AffineKalmanLP.o
    g++ $(LDFLAGS) -o main main.o AffineKalmanLP.o $(LDLIBS)

main.o: main.cpp main.h
    g++ $(CPPFLAGS) -c main.cpp

AffineKalmanLP.o: AffineKalmanLP.cpp AffineKalmanLP.h
    g++ $(CPPFLAGS) -c AffineKalmanLP.cpp

clean:
    rm main main.o AffineKalmanLP.o

You need to add it to LDLIBS.

If I remove that line, I get undefined reference for various things as well.

Okan Barut
  • 279
  • 1
  • 7