我正在尝试使用raspicam库使用 cmake 编译一个简单的项目。项目完美编译;但是,最后我有 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
答案 0 :(得分:1)
您似乎正在尝试从源代码构建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)
正常运行。
如果您没有能力将库安装到开发机器上,而是想与项目一起从源代码构建它,则可以考虑:
使用CMake的add_subdirectory命令
使用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源代码。