opencv - Linking C++ with Go -


i trying bind c++ opencv programs go. using concept how statically link c library in go using cgo?[1]. myimgcom.cpp file is:

/** * @file surf_flannmatcher * @brief surf detector + descriptor + flann matcher * @author a. huaman  edited: nnnnn */  #include <stdio.h> #include <string> #include <iostream> #include "opencv2/core/core.hpp" #include "opencv2/features2d/features2d.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/nonfree/features2d.hpp"  using namespace cv;  void readme();  int mysurf( int myarg) {   string option1= "./lenna.png";  string option2= "./lenna.png";   if( myarg != 3 )  { readme(); return -1; }   std::cout << option1 << option2;   mat img_1 = imread( option1, cv_load_image_grayscale );  mat img_2 = imread( option2, cv_load_image_grayscale );   if( !img_1.data || !img_2.data )  { std::cout<< " --(!) error reading images " << std::endl; return -1; }   //-- step 1: detect keypoints using surf detector  int minhessian = 400;   surffeaturedetector detector( minhessian );   std::vector<keypoint> keypoints_1, keypoints_2;   detector.detect( img_1, keypoints_1 );  detector.detect( img_2, keypoints_2 );   //-- step 2: calculate descriptors (feature vectors)  surfdescriptorextractor extractor;   mat descriptors_1, descriptors_2;   extractor.compute( img_1, keypoints_1, descriptors_1 );  extractor.compute( img_2, keypoints_2, descriptors_2 );   //-- step 3: matching descriptor vectors using flann matcher  flannbasedmatcher matcher;  std::vector< dmatch > matches;  matcher.match( descriptors_1, descriptors_2, matches );   double max_dist = 0; double min_dist = 100;   //-- quick calculation of max , min distances between keypoints  for( int = 0; < descriptors_1.rows; i++ )  { double dist = matches[i].distance;     if( dist < min_dist ) min_dist = dist;     if( dist > max_dist ) max_dist = dist;  }    printf("-- max dist : %f \n", max_dist );   printf("-- min dist : %f \n", min_dist );    std::vector< dmatch > good_matches;    for( int = 0; < descriptors_1.rows; i++ )   { if( matches[i].distance <= max(4*min_dist, 0.02) )   { good_matches.push_back( matches[i]); }   }    //-- draw "good" matches   mat img_matches;   drawmatches( img_1, keypoints_1, img_2, keypoints_2,            good_matches, img_matches, scalar::all(-1), scalar::all(-1),            vector<char>(), drawmatchesflags::not_draw_single_points );    //-- show detected matches   imshow( "good matches", img_matches );     for( int = 0; < (int)good_matches.size(); i++ )   { printf( "-- match [%d] keypoint 1: %d  -- keypoint 2: %d  \n", i,    good_matches[i].queryidx, good_matches[i].trainidx ); }    waitkey(0);    return 0;  }    void readme()  { std::cout << " usage: ./surf_flannmatcher <img1> <img2>" << std::endl; } 

next, header file myimgcom.h file is:

int mysurf( int); void readme(); 

next, imagetest.go file is:

package main  import "fmt"  // #include "myimgcom.h" import "c"  func main() {    fmt.printf("invoking c library...\n")   c.mysurf( 3)  } 

i doing following commands:

 gcc -o2 -c myimgcom.cpp  -> runs  ar q libgb.a myimgcom.o  -> runs  go build imagetest.go    -> here following error:   # command-line-arguments  undefined symbols architecture x86_64:  "_mysurf", referenced from:   __cgo_8db0969da00f_cfunc_mysurf in imagetest.cgo2.o  (maybe meant: __cgo_8db0969da00f_cfunc_mysurf)  ld: symbol(s) not found architecture x86_64  clang: error: linker command failed exit code 1 (use -v see invocation) 

i think problem linking pkg config , cflags cgo. can suggest me how can solve issue? working mac os.

c++ isn't c, , call conventions different. it's impossible call c++ directly. if want call go, first c wrapper. in case adding "extern c" function declarations should work.

extern "c" int mysurf(int myarg) {  // code here }  

Comments