gpt4 book ai didi

c++ - 与 raspicam 库链接时链接器/加载器错误 "undefined reference to ..."

转载 作者:行者123 更新时间:2023-11-30 04:52:10 26 4
gpt4 key购买 nike

我正在尝试使用 cmake 编译一个简单的项目,同时使用 raspicam 库。项目编译完美;但是,最后我有 ld 错误。 Raspicam 从这里下载:http://www.uco.es/investiga/grupos/ava/node/40 .有什么想法吗?

ma​​in.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

最佳答案

看来您正在尝试从源代码构建 raspicam。看着 documentation you linked raspicam 似乎提供了一个 CMake 查找包,并鼓励用户像这样包含该库:

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 源代码。

关于c++ - 与 raspicam 库链接时链接器/加载器错误 "undefined reference to ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54551676/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com