链接器/加载错误与raspicam库链接时,“未定义参考......”

问题描述 投票:1回答:1

我试图编译CMake的一个简单的项目,同时采用raspicam库。项目完美编制;然而,我在结束LD错误。 Raspicam从这里下载:http://www.uco.es/investiga/grupos/ava/node/40。有任何想法吗?

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})

构建命令:

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

错误:

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
c++ cmake linker ld
1个回答
1
投票

看来你试图从源代码编译raspicam。看着似乎raspicam提供的CMake找到包,并鼓励用户包括像这样的库documentation you linked

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

我会鼓励你做到这一点,但它需要在系统上安装raspicam。也就是说,你首先需要按照上面的文件中列出的安装步骤:

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

这将安装包在CMake的可以找到它,让您的CMakeList.txt使用的find_package(raspicam REQUIRED)正常工作的位置。

如果你没有到库安装到你的开发机,而是想从源沿着你的项目,你可以考虑建立它的能力:

  1. 使用cmake的add_subdirectory命令
  2. 使用cmake的ExternalProject_Add命令。

选项1将允许您更新CMakeList.txt文件,如下所示:

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)

请注意,您需要将您的目标名称raspicam改变一些独特的,作为目标名称由你包括add_subdirectory包。

方案2是比较复杂的,但得到的是你不会在你的版本控制系统来跟踪raspicam源代码的额外好处。

© www.soinside.com 2019 - 2024. All rights reserved.