简体   繁体   中英

Linker/loader errors "undefined reference to ..." when linking with raspicam library

I am trying to compile a simple project with cmake , while using raspicam library. Project compiled flawlessly; however, I have ld errors at the end. Raspicam downloaded from here: http://www.uco.es/investiga/grupos/ava/node/40 . Any ideas?

main.cpp:

#include <raspicam_cv.h>

int main ( int argc,char **argv ) {
    raspicam::RaspiCam_Cv capture;

    if (!capture.open()) {
        return 1;
    }
    return 0;
}

CMakeLists.txt:

cmake_minimum_required(VERSION 3.10)
project(raspicam)

set(CMAKE_CXX_STANDARD 17)
include_directories(raspicam-0.1.6/src)
link_directories(raspicam-0.1.6/src)
add_executable(raspicam main.cpp)
set(RASPICAM_LIB ${CMAKE_SHARED_LIBRARY_PREFIX}raspicam_cv${CMAKE_SHARED_LIBRARY_SUFFIX})
target_link_libraries(raspicam ${RASPICAM_LIB})

Build command:

cd raspicam-0.1.6/ && cmake -DOpenCV_DIR=/home/user/Projects/opencv/opencv-build . && make && cd .. && cmake . && make

Errors:

CMakeFiles/raspicam.dir/main.cpp.o: In function `main':
/home/user/CLionProjects/raspicam/main.cpp:5: undefined reference to `raspicam::RaspiCam_Cv::RaspiCam_Cv()'
/home/user/CLionProjects/raspicam/main.cpp:7: undefined reference to `raspicam::RaspiCam_Cv::open()'
/home/user/CLionProjects/raspicam/main.cpp:5: undefined reference to `raspicam::RaspiCam_Cv::~RaspiCam_Cv()'
/home/user/CLionProjects/raspicam/main.cpp:5: undefined reference to `raspicam::RaspiCam_Cv::~RaspiCam_Cv()'
collect2: error: ld returned 1 exit status
CMakeFiles/raspicam.dir/build.make:94: recipe for target 'raspicam' failed
make[2]: *** [raspicam] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/raspicam.dir/all' failed
make[1]: *** [CMakeFiles/raspicam.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

It appears you're attempting to build raspicam from source. Looking at the documentation you linked it appears that raspicam provides a CMake find package and encourages users to include the library like so:

project (my_raspi_project)
find_package(raspicam REQUIRED)
add_executable (my_raspi_project main.cpp)  
target_link_libraries (my_raspi_project ${raspicam_LIBS})

I would encourage you to do this, but it would require installing raspicam on your system. That is, you would first need to follow the install steps listed in the above documentation:

tar xvzf raspicamxx.tgz
cd raspicamxx
mkdir build
cd build
cmake ..
make
sudo make install
sudo ldconfig

This installs the package in a location where CMake can find it and allow the find_package(raspicam REQUIRED) used in your CMakeList.txt to work correctly.

If you do not have the ability to install the library to your development machine, and instead want to build it from source alongside your project you could consider:

  1. Using CMake's add_subdirectory command

  2. Using CMake'sExternalProject_Add command.

Option #1 would allow you to update your CMakeList.txt file to look like the following:

cmake_minimum_required(VERSION 3.10)
project(raspicam)

set(CMAKE_CXX_STANDARD 17)
add_subdirectory(<path to raspicam CMakeList.txt>)
add_executable(my_raspicam_program main.cpp)
target_link_libraries(my_raspicam_program raspicam)

Note that you will need to change your target name raspicam to something unique, as that target name is used by the package you are including with add_subdirectory.

Option #2 is more complicated but gets the added benefit that you will not be tracking the raspicam source code in your version-control system.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM