Import Non Standard C++ Lib Using Import "C"

Guys I am trying to call a C++ code using import “C” in VS Code (not vs studio). That worked for a simple “hello world” which only have standard libraries required, nevertheless when I tried to call an opencv code which requires its own libraries the compiler could not find them:

# command-line-arguments
.\cam.go:4:10: fatal error: opencv2/opencv.hpp: No such file or directory
    4 | //#include <opencv2/opencv.hpp>
      |          ^~~~~~~~~~~~~~~~~~~~
compilation terminated.

Below is my code. It is a code to open an jpg image from a specified directory.

package main

//#include <stdio.h>
//#include <opencv2/opencv.hpp>
//
//using namespace cv;
//int main(int argc, char** argv )
//{
//
//Mat image;
//image = imread("C:\\Users\\Desktop\\profile-picture.jpg");
//if ( !image.data )
//{
//printf("No image data \n");
//return -1;
//}
//namedWindow("Display Image", WINDOW_AUTOSIZE );
//imshow("Display Image", image);
//waitKey(0);
//return 0;
//}
import "C"

func main() {

}

How can I show to the compiler where is the library? I think that is the problem.

You need to tell cgo where to look. Here’s a snippet extracted from some of my code to give you an idea:

/*
#cgo windows CFLAGS: -DTAG_WINDOWS
#cgo linux freebsd CFLAGS: -DTAG_POSIX
#cgo linux freebsd LDFLAGS: -ldl
#cgo CFLAGS: -I ${SRCDIR}/sdk/samples/common/inc

CFLAGS, CPPFLAGS, CXXFLAGS, FFLAGS and LDFLAGS may be defined with pseudo #cgo directives within these comments to tweak the behavior of the C, C++ or Fortran compiler.

1 Like

Unfortunately I did not understand this documentation. This do not look to be very friendly as go used to. First, I do not know which directory I should referrer. I write below a short version of the opencv folder structure:

C:Opencv/
├─ build/
│  ├─ bin/
│  ├─ etc/
│  ├─ x64/
│      ├─ vc14/
│      ├─ vc15/
│          ├─ bin/
│          ├─ lib/
│  ├─ python/
│  ├─ java/
│  ├─ include/
│          ├─ opencv2/
│              ├─ opencv.hpp
├─ sources/
│  ├─ 3rdparty/
│  ├─ apps/
│  ├─ cmake/
│  ├─ data/
│  ├─ doc/
│  ├─ include/
│      ├─ opencv2/
│          ├─ opencv.hpp
│  ├─ modules/
│  ├─ platforms/
│  ├─ samples/

In the flag // #cgo LDFLAGS: -L${SRCDIR}/libs -lfoo , what would be the directory that should I use to call the #include <opencv2/opencv.hpp>, the source or the build? How write it inside the -L${SRCDIR}? As it is possible see on the structure above, the libs directory is not the same directory of the includes. Please help me, this even looks like my loved go.

This has more to do with C than it does with Go. cgo lets Go call C code. -I, -L, and -l are flags to the C compiler and linker. You really need to understand how those work to be able to specify anything, but in short, -I points to directories that contain files to be #included. -L points to directories containing libraries, and -l indicates a library that should be linked.

You might do well to extract your C code to a .c file and see if you can get the C compiler to build it. Then you’ll know what you need to specify for cgo.

1 Like