You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Geany编译器中摄像头初始化报错:undefined reference to raspicam

解决Raspicam编译时未定义引用错误

编译摄像头初始化代码时出现raspicam相关函数未定义引用,导致编译失败。编译命令及报错信息如下:

错误的编译命令

g++ $(pkg-config opencv4 --cflags --libs) -o g++ $(pkg-config raspicam --cflags --libs) -o cameratest cameratest.cpp

报错信息

/usr/bin/ld: /tmp/cc3JiNr2.o: in function `main':
cameratest.cpp:(.text+0x14): undefined reference to `raspicam::RaspiCam_Cv::RaspiCam_Cv()'
/usr/bin/ld: cameratest.cpp:(.text+0x3c): undefined reference to `raspicam::RaspiCam_Cv::open()'
/usr/bin/ld: cameratest.cpp:(.text+0x9c): undefined reference to `raspicam::RaspiCam_Cv::getId[abi:cxx11]() const'
/usr/bin/ld: cameratest.cpp:(.text+0xd8): undefined reference to `raspicam::RaspiCam_Cv::~RaspiCam_Cv()'
/usr/bin/ld: cameratest.cpp:(.text+0x108): undefined reference to `raspicam::RaspiCam_Cv::~RaspiCam_Cv()'
collect2: error: ld returned 1 exit status
Compilation failed.

问题原因

  1. 编译命令重复使用-o参数,错误设置输出文件名为g++,导致后续参数逻辑混乱
  2. GCC/G++要求源文件在前,链接库参数在后,错误的顺序会让链接器无法匹配raspicam的函数实现

正确的编译命令

g++ cameratest.cpp $(pkg-config opencv4 --cflags --libs) $(pkg-config raspicam --cflags --libs) -o cameratest

额外排查步骤

如果执行正确命令后仍报错,可检查raspicam库是否安装到位:

pkg-config --modversion raspicam

若返回具体版本号则库安装正常,否则需要重新安装raspicam库。

内容的提问来源于stack exchange,提问作者Talha Faraz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 19:20:37