Ask Your Question
0

Help with "Undefined-reference" when compiling with CMake for OpenCV & C++ program

asked 2020-03-04 12:32:49 -0600

raisa_ gravatar image

updated 2020-03-04 12:41:58 -0600

I'm following a book tutorial to add slider and mouse events to an interface with OpenCV and C++. I'm working in terminal Ubuntu and compile my project with CMake. Here, I have 2 programs, sample1.cpp and sample2.cpp. Now, sample1 worked just fine I can run it properly. When I added sample2 to my CMakeLists and tried to compile it, I encountered this :

[ 25%] Building CXX object CMakeFiles/sample1.dir/sample1.cpp.o
[ 50%] Linking CXX executable sample1
[ 50%] Built target sample1
Scanning dependencies of target sample2
[ 75%] Building CXX object CMakeFiles/sample2.dir/sample2.cpp.o
[100%] Linking CXX executable sample2
CMakeFiles/sample2.dir/sample2.cpp.o: In function `main':
sample2.cpp:(.text+0x15b): undefined reference to `onChange(int, void*)'
sample2.cpp:(.text+0x1ec): undefined reference to `onMouse(int, int, int, int, void*)'
sample2.cpp:(.text+0x229): undefined reference to `onChange(int, void*)'
collect2: error: ld returned 1 exit status
CMakeFiles/sample2.dir/build.make:146: recipe for target 'sample2' failed
make[2]: *** [sample2] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/sample2.dir/all' failed
make[1]: *** [CMakeFiles/sample2.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

Looks like linker error but I think I have added all the necessary libs. Here's my code to sample2 :

#include <iostream>
#include "opencv2/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc.hpp"
using namespace cv;
using namespace std;

//create variable to save the position of value in track
int blurAmount = 15;

//Trackball call back function
void onChange(int pos, void* userInput);

//Mouse callback
void onMouse(int event, int x, int y, int, void* userInput);

int main(int argc, const char** argv){
         //Read images
         Mat img1 = imread("image_one.jpg");

         //Create windows
         namedWindow("Image1");

         //Create a trackbar --
         createTrackbar("Image1", "Image1", &blurAmount, 30, onChange, &img1);
         setMouseCallback("Image1", onMouse, &img1);

         //Call to onChange to init
         onChange(blurAmount, &img1);

         waitKey(0);
         destroyWindow("Image1");
         return 0;
 }

I believe my CMakeLists isn't the problem here, since it works fine with sample1. But here's my CMakeLists.txt in case it's necessary.

cmake_minimum_required(VERSION 3.0)
PROJECT(Chapter3)

FIND_PACKAGE(OpenCV REQUIRED)
INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})
LINK_DIRECTORIES(${OpenCV_LIB_DIR})

ADD_EXECUTABLE(sample1 sample1.cpp)
TARGET_LINK_LIBRARIES(sample1 ${OpenCV_LIBS})

ADD_EXECUTABLE(sample2 sample2.cpp)
TARGET_LINK_LIBRARIES(sample2 ${OpenCV_LIBS})

And advice what could be causing those errors ??

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-03-04 14:49:56 -0600

mvuori gravatar image

Well, in your own code you give prototypes for those functions, but not code for the actual functions... They are your functions, where you define the actions, not OpenCV's.

edit flag offensive delete link more

Comments

1

Ohh, dang. They're just functions declaration. The actual functions are on the next 2 pages on the book. I'm just started learning C++ and still not used with C/C++ where we need to declare all the functions first. Thank you.

raisa_ gravatar imageraisa_ ( 2020-03-04 15:15:39 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-03-04 12:32:49 -0600

Seen: 4,102 times

Last updated: Mar 04 '20