1

I have created a C API which exposes the functionality of C++ code. Here is the code for opencv_test.h:

#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif

EXTERNC void OpenVideo(char *filename);

#undef EXTERNC

Code for 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);

    }

}

And finally, for 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))
}

And the error I am getting is the following:


command-line-arguments /tmp/go-build823465636/b001/_x002.o: In function "_cgo_dcf5d601f507_Cfunc_OpenVideo": /tmp/go-build/cgo-gcc-prolog:48: undefined reference to `OpenVideo' collect2: error: ld returned 1 exit status


Now I am not sure if I have to run any kind of special commands or something, I have read that just by importing "C" pseudo-package, my .go file will be build with cgo. So, if I have included my custom-made .h file, and the project is built using cgo, how come I get undefined reference to a function error?

EDIT:

Here is the whole output when running go build -x opencvTest.go command:

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 command-line-arguments -- -I/usr/local/include -I $WORK/b001/ -g -O2 ./opencvTest.go

cd $WORK

gcc -fno-caret-diagnostics -c -x c - || true

gcc -Qunused-arguments -c -x c - || true

gcc -fdebug-prefix-map=a=b -c -x c - || true

gcc -gno-record-gcc-switches -c -x c - || true

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-switches

-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-switches

-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-switches

-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

command-line-arguments /tmp/go-build568047770/b001/_x002.o: In function _cgo_dcf5d601f507_Cfunc_OpenVideo': /tmp/go-build/cgo-gcc-prolog:48: undefined reference toOpenVideo' collect2: error: ld returned 1 exit status

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Stefan Radonjic
  • 1,449
  • 4
  • 19
  • 38
  • 2
    I'm not seeing anything there about `opencv_test.cpp` being compiled, or the result being linked to your golang program. I'm not sure how this is supposed to work on the golang side, but that's what appears to be missing. – John Bollinger Mar 09 '19 at 22:23
  • You are right. Any idea why that is happening? I am new to GoLang. – Stefan Radonjic Mar 09 '19 at 22:26

0 Answers0