我创建了一个C API,它公开了C ++代码的功能。这是opencv_test.h
的代码:
#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif
EXTERNC void OpenVideo(char *filename);
#undef EXTERNC
opencv_test.cpp
的代码:
#include <iostream>
#include <unistd.h>
#include "opencv_test.h"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
void OpenVideo(char *filename){
VideoCapture cap(filename);
Mat Uframe;
int frameCount = 0;
if(!cap.isOpened()){
cout<<"Cannot open video file!";
return;
}
namedWindow("Video");
while(++frameCount){
bool success = cap.read(Uframe);
if(!success){
cout<<frameCount<<" frames read";
break;
}
imshow("Video", Uframe);
waitKey(1);
}
}
最后,对于opencvTest.go
:
package main
/*
#cgo LDFLAGS: -L/usr/local/lib -lopencv_core -lopencv_video -lopencv_videoio -lopencv_highgui
#include <stdlib.h>
#include "opencv_test.h"
*/
import "C"
func main(){
theFilename := "DRŽAVNI POSAO [HQ] - Ep.323 Dan žena (07.03.2014.).avi"
C.OpenVideo(C.CString(theFilename))
}
我得到的错误如下:
命令行参数/tmp/go-build823465636/b001/_x002.o:在 函数“ _cgo_dcf5d601f507_Cfunc_OpenVideo”: / tmp / go-build / cgo-gcc-prolog:48:未定义对“ OpenVideo”的引用 collect2:错误:ld返回1退出状态
现在,我不确定是否必须运行任何类型的特殊命令或其他命令,我已经读过,只要导入“ C” 伪软件包,我的.go
文件就可以用cgo
构建。因此,如果我包含了自定义的.h
文件,并且该项目是使用cgo
构建的,我怎么会得到未定义的对函数的引用错误?
编辑:
这是运行go build -x opencvTest.go
命令时的全部输出:
WORK = / tmp / go-build568047770
mkdir -p $ WORK / b001 /
cd / home / stefan / Desktop / GoLang
CGO_LDFLAGS ='“-L / usr / local / lib”“ -lopencv_core”“ -lopencv_video” “ -lopencv_videoio”“ -lopencv_highgui”“ -L / usr / local / lib” “ -lopencv_core”“ -lopencv_video”“ -lopencv_videoio” “ -lopencv_highgui”'/ usr / local / go / pkg / tool / linux_amd64 / cgo -objdir $ WORK / b001 / -importpath命令行参数--I / usr / local / include -I $ WORK / b001 / -g -O2 ./opencvTest.go
cd $ WORK
gcc -fno-caret-diagnostics -c -x c-||是
gcc -Qunused-arguments -c -x c-||是
gcc -fdebug-prefix-map = a = b -c -x c-||是
gcc -gno-record-gcc-switches -c -x c-||是
cd $ WORK / b001
TERM ='dumb'gcc -I / home / stefan / Desktop / GoLang -fPIC -m64 -pthread -fmessage-length = 0 -fdebug-prefix-map = $ WORK / b001 = / tmp / go-build -gno-record-gcc-switchs
-I / usr / local / include -I ./ -g -O2 -o ./_x001.o -c _cgo_export.c TERM ='dumb'gcc -I / home / stefan / Desktop / GoLang -fPIC- m64 -pthread -fmessage-length = 0 -fdebug-prefix-map = $ WORK / b001 = / tmp / go-build -gno-record-gcc-switchs
-I / usr / local / include -I ./ -g -O2 -o ./_x002.o -c opencvTest.cgo2.c TERM ='dumb'gcc -I / home / stefan / Desktop / GoLang- fPIC -m64 -pthread -fmessage-length = 0 -fdebug-prefix-map = $ WORK / b001 = / tmp / go-build -gno-record-gcc-switchs
-I / usr / local / include -I ./ -g -O2 -o ./_cgo_main.o -c _cgo_main.c cd / home / stefan / Desktop / GoLang TERM ='dumb'gcc -I。 -fPIC -m64 -pthread -fmessage-length = 0 -fdebug-prefix-map = $ WORK / b001 = / tmp / go-build -gno-record-gcc-switches -o $ WORK / b001 / cgo .o $ WORK / b001 / _cgo_main.o $ WORK / b001 / _x001.o $ WORK / b001 / _x002.o -L / usr / local / lib -lopencv_core -lopencv_video -lopencv_videoio -lopencv_highgui -L / usr / local / lib -lopencv_core -lopencv_video -lopencv_videoio -lopencv_highgui
命令行参数/tmp/go-build568047770/b001/_x002.o:在 功能
_cgo_dcf5d601f507_Cfunc_OpenVideo': /tmp/go-build/cgo-gcc-prolog:48: undefined reference to
OpenVideo' collect2:错误:ld返回1退出状态