I am working on a project using OpenCV and cmake (both of which I'm new to) and keep getting an undefined reference to functions that are located in external folders.
This is for a Raspberry Pi using a command line interface. My CMakeLists.txt file looks like:
cmake_minimum_required(VERSION 3.1)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
project(projectName)
find_package(OpenCV REQUIRED)
add_executable(${PROJECT_NAME} project.cpp supportFile.c supportFile2.c)
target_link_libraries(projectName ${OpenCV_LIBS} wiringPi)
When I compile with cmake .
and make
it completes but with the following message:
CMakeFiles/.dir/projectName.cpp.o: In function `main':
projectName.cpp:(.text+0x4c): undefined reference to `initializeBoard()'
projectName.cpp:(.text+0x98): undefined reference to `setRS485ServoPosition(unsigned short, unsigned short, unsigned short)'
All of the undefined references are function calls in the supportFile.c, supportFile2.c, or the wiringPi library. I'm not sure how to fix this issue?
project.cpp file:
#include "opencv2/highgui.hpp"
#include "opencv2/opencv.hpp"
#include "supportFile.h"
#include "supportFile2.h"
int main(int argc, char* argv[]) {
initializeBoard(); brake = 0; speed = 70; usmAngle = 5; value = 4;
setRS485ServoPosition(USM,0,usmAngle);
setRS485ServoPosition(CAMERASERVO,0,value);
...
The initializeBoard() is found in the supportFile.h file and the setRS485ServoPosition() functions are in the supportFile2.h.
I figured out what my problem was. It was not with cmake but with the supportFile.h and supportFile2.h. I made the following changes to both files:
// .h file #include and #define section
#ifdef __cplusplus
extern "C" {
#endif
extern // to all .h functions section
#ifdef __cplusplus
}
#endif
After doing so, everything compiled and ran.