【发布时间】:2019-10-03 09:42:06
【问题描述】:
我正在尝试获取一个使用 Basler 相机捕获图像的 C++ 程序。我拿到 来自制造商的代码,它应该“非常易于使用”,但是,链接它有 成为噩梦。我的 C++ 时代已经过去(最近只使用 Matlab),所以我可能会犯一些愚蠢的错误,但请赐教:
代码如下所示:
// Include files to use the PYLON API.
#include <pylon/PylonIncludes.h>
#ifdef PYLON_WIN_BUILD
# include <pylon/PylonGUI.h>
#endif
// Namespace for using pylon objects.
using namespace Pylon;
using namespace GenApi;
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/video/video.hpp>
//#include <opencv2/videoio.hpp>
using namespace cv;
// Namespace for using cout.
using namespace std;
#include <stdio.h>
#include "kbhit.h"
#include <sys/stat.h> // for checking the file size
#include <sstream>
// Number of images to be grabbed.
//static const uint32_t c_countOfImagesToGrab = 100;
int main(int argc, char* argv[])
{
// The exit code of the sample application.
int exitCode = 0;
// Automagically call PylonInitialize and PylonTerminate to ensure the pylon runtime system
// is initialized during the lifetime of this object.
Pylon::PylonAutoInitTerm autoInitTerm;
VideoWriter cvVideoCreator;
struct stat statbuf;
string filenameBase = "/opt/PylonTestAVI";
uint filecounter = 1;
string filename = "";
... (and so on)
我正在尝试使用以下代码对其进行编译:
g++ GrabV3.cpp -I/opt/pylon5 -I/opt/pylon5/include -I/usr/local/include -I/usr/include/ -L/opt/pylon5/lib64
这里/opt/pylon5是Basler自己的库,/usr/local/include是opencv4文件夹的链接。
我收到一页长的错误消息列表 - 开头看起来像这样
GrabV3.cpp:(.text+0x2a5): 未定义引用 cv::VideoWriter::VideoWriter()'
GrabV3.cpp:(.text+0x32a): undefined reference toPylon::CTlFactory::GetInstance()'
GrabV3.cpp:(.text+0x346): 未定义引用Pylon::CDeviceInfo::CDeviceInfo()'
GrabV3.cpp:(.text+0x370): undefined reference toPylon::CInstantCamera::CInstantCamera(Pylon::IPylonDevice, Pylon::ECleanup)'
GrabV3.cpp:(.text+0x38e): 未定义引用Pylon::CInstantCamera::Open()'
GrabV3.cpp:(.text+0x39d): undefined reference toPylon::CInstantCamera::GetNodeMap()'
GrabV3.cpp:(.text+0x3cb): 未定义引用GenICam_3_1_Basler_pylon::gcstring::gcstring(char const*)'
GrabV3.cpp:(.text+0x41b): undefined reference toGenICam_3_1_Basler_pylon::gcstring::~gcstring()'
GrabV3.cpp:(.text+0x465): 未定义引用GenICam_3_1_Basler_pylon::gcstring::gcstring(char const*)'
GrabV3.cpp:(.text+0x488): undefined reference toGenICam_3_1_Basler_pylon::gcstring::~gcstring()'
GrabV3.cpp:(.text+0x4af): 未定义引用GenICam_3_1_Basler_pylon::gcstring::gcstring(char const*)'
GrabV3.cpp:(.text+0x4ff): undefined reference toGenICam_3_1_Basler_pylon::gcstring::~gcstring()'
GrabV3.cpp:(.text+0x526): 未定义引用GenICam_3_1_Basler_pylon::gcstring::gcstring(char const*)'
GrabV3.cpp:(.text+0x576): undefined reference toGenICam_3_1_Basler_pylon::gcstring::~gcstring()'
GrabV3.cpp:(.text+0x745): undefined reference to `cv::VideoWriter::open(cv::String const&, int, double, cv::Size_, bool)'*
显然没有任何效果,从 cv:VideoWriter() 函数开始,它是标准 OpenCV 的一部分(我使用本教程安装:https://cv-tricks.com/installation/opencv-4-1-ubuntu18-04/)。
所以我在这里迷路了 - 已经花了大约一天的时间试图让它工作。有人可以帮忙吗?
【问题讨论】:
-
请尝试
g++而不是gcc。 (缺少对std::basic_string的引用让我认为std c++ lib。没有链接。g++默认情况下应该这样做。) -
再次查看您的命令,我坚信有/缺少 lib。这会导致左侧未定义的引用错误(链接错误)。详细说明cmd。 args.:
-I(大写 i)用于包含路径(用于编译器)。-L用于库路径(用于链接器)。-l(small ell) 用于图书馆。我在您的编译命令中没有看到任何-l参数。我相信这就是你所缺少的。 -
正如@Scheff 提到的,您需要链接到正确的库。正如我所看到的,您只链接到这个 pylon 库,但您也使用了 opencv,并且您还获得了一个未定义的参考(我相信单独安装不足以让链接器找到正确的库)。
-
所以,这个命令会告诉你
pkg-config知道的关于pkg-config --list-all的所有包,你应该能够找到 OpenCV 和 Pylon 在那里,比如 opencv4。然后您可以使用pkg-config --libs --cflags opencv4获取相应的库,并将其放入您的编译命令中$(...)。 Pylon 也是如此。 -
好的,更详细地查看您的代码...您代码中的包含文件本身对于 OpenCV4 是错误的。它们都变了,现在整个 OpenCV 只有一个包含文件,而不是
core.hpp、highgui.hpp和video.hpp我认为是#include <opencv2/opencv.hpp>